# Client-side 작성

pet-shop의 코드를 약간 수정해서, election dapp을 위한 client-side를 작성합니다.

1. **src/index.html** 을 수정합니다.

```
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Election Results</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>
    <div class="container" style="width: 650px;">
      <div class="row">
        <div class="col-lg-12">
          <h1 class="text-center">Election Results</h1>
          <hr/>
          <br/>
          <div id="loader">
            <p class="text-center">Loading...</p>
          </div>
          <div id="content" style="display: none;">
            <table class="table">
              <thead>
                <tr>
                  <th scope="col">#</th>
                  <th scope="col">Name</th>
                  <th scope="col">Votes</th>
                </tr>
              </thead>
              <tbody id="candidatesResults">
              </tbody>
            </table>
            <hr/>
            <p id="accountAddress" class="text-center"></p>
            <form onSubmit="App.castVote(); return false;">
            <div class="form-group">
              <label for="candidatesSelect">Select Candidate</label>
              <select class="form-control" id="candidatesSelect">
              </select>
            </div>
            <button type="submit" class="btn btn-primary">Vote</button>
            <hr />
          </form>
          </div>
        </div>
      </div>
    </div>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
    <script src="js/web3.min.js"></script>
    <script src="js/truffle-contract.js"></script>
    <script src="js/app.js"></script>
  </body>
</html>
```

2\. **src/js/app.js** 을 수정합니다.

```
App = {
  web3Provider: null,
  contracts: {},
  account: '0x0',

  init: function() {
    return App.initWeb3();
  },

  initWeb3: function() {
    if (typeof web3 !== 'undefined') {
      // If a web3 instance is already provided by Meta Mask.
      App.web3Provider = web3.currentProvider;
      web3 = new Web3(web3.currentProvider);
    } else {
      // Specify default instance if no web3 instance provided
      App.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
      web3 = new Web3(App.web3Provider);
    }
    return App.initContract();
  },

  initContract: function() {
    $.getJSON("Election.json", function(election) {
      // Instantiate a new truffle contract from the artifact
      App.contracts.Election = TruffleContract(election);
      // Connect provider to interact with contract
      App.contracts.Election.setProvider(App.web3Provider);
      
      App.listenForEvents();


      return App.render();
    });
  },
  listenForEvents: function() {
      App.contracts.Election.deployed().then(function(instance) {
        instance.votedEvent({}, {
          fromBlock: 0,
          toBlock: 'latest'
        }).watch(function(error, event) {
          console.log("event triggered", event)
          // Reload when a new vote is recorded
          App.render();
        });
      });
    },
 
  render: function() {
      var electionInstance;
      var loader = $("#loader");
      var content = $("#content");
    
      loader.show();
      content.hide();
    
      // Load account data
      web3.eth.getCoinbase(function(err, account) {
        if (err === null) {
          App.account = account;
          $("#accountAddress").html("Your Account: " + account);
        }
      });

      // Load contract data
      App.contracts.Election.deployed().then(function(instance) {
        electionInstance = instance;
        return electionInstance.candidatesCount();
      }).then(function(candidatesCount) {
        var candidatesResults = $("#candidatesResults");
        candidatesResults.empty();
    
        var candidatesSelect = $('#candidatesSelect');
        candidatesSelect.empty();
    
        for (var i = 1; i <= candidatesCount; i++) {
          electionInstance.candidates(i).then(function(candidate) {
            var id = candidate[0];
            var name = candidate[1];
            var voteCount = candidate[2];
    
            // Render candidate Result
            var candidateTemplate = "<tr><td>" + name + "</td><td>" + voteCount + "</td></tr>"
            candidatesResults.append(candidateTemplate);
    
            // Render candidate ballot option
            var candidateOption = "<option value='" + id + "' >" + name + "</ option>"
            candidatesSelect.append(candidateOption);
          });
        }
        return electionInstance.voters(App.account);
      }).then(function(hasVoted) {
        // Do not allow a user to vote
//        if(hasVoted) {
//          $('form').hide();
//        }
        loader.hide();
        content.show();
      }).catch(function(error) {
        console.warn(error);
      });
    },
castVote: function() {
    var candidateId = $('#candidatesSelect').val();
    App.contracts.Election.deployed().then(function(instance) {
      return instance.vote(candidateId, { from: App.account });
    }).then(function(result) {
      // Wait for votes to update
      $("#content").hide();
      $("#loader").show();
    }).catch(function(err) {
      console.error(err);
    });
  }
};

$(function() {
  $(window).load(function() {
    App.init();
  });
});
```

3\. Client 서버를 실행합니다.

```
npm run dev
```

4\. Server에서 node 서버의 url을 누르면, client page를 볼 수 있습니다.

![](https://265339664-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LR_DTb3o8Hw06rXwN1L%2F-LRzgQ6_oIAM6TR9SJg-%2F-LRzh4x9RM3fUqQUzQ0X%2F%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202018-11-23%20%E1%84%8B%E1%85%A9%E1%84%92%E1%85%AE%205.31.53.png?alt=media\&token=07f04bbb-d5c4-4170-86b7-3b690d6b5763)

{% embed url="<https://server6iq1vjua-truffle-dev-machine-server-3000.morpheuslabs.io/>" %}

![](https://265339664-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LR_DTb3o8Hw06rXwN1L%2F-LRzgQ6_oIAM6TR9SJg-%2F-LRzhKJ2Ud6WkWN_l5xK%2F%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202018-11-23%20%E1%84%8B%E1%85%A9%E1%84%92%E1%85%AE%205.34.50.png?alt=media\&token=b35c3808-fa37-4cec-a3de-d6b663b0436b)

아직 Metamask를 통해 private network을 지정하지 않았기 때문에, data를 가져오지 못한 모습입니다.
