App0.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import React, { Component } from 'react'
  2. import PayrollContract from '../build/contracts/Payroll.json'
  3. import getWeb3 from './utils/getWeb3'
  4. import Common from "./components/Common";
  5. import Accounts from "./components/Accounts";
  6. import Employer from "./components/Employer";
  7. import Employee from "./components/Employee";
  8. import './css/oswald.css'
  9. import './css/open-sans.css'
  10. import './css/pure-min.css'
  11. import './App.css'
  12. class App extends Component {
  13. constructor(props) {
  14. super(props)
  15. this.state = {
  16. web3: null
  17. }
  18. }
  19. componentWillMount() {
  20. // Get network provider and web3 instance.
  21. // See utils/getWeb3 for more info.
  22. getWeb3.then(results => {
  23. this.setState({
  24. web3: results.web3
  25. })
  26. // Instantiate contract once web3 provided.
  27. this.instantiateContract()
  28. })
  29. .catch(() => {
  30. console.log('Error finding web3.')
  31. })
  32. }
  33. instantiateContract() {
  34. const contract = require('truffle-contract')
  35. const payroll = contract(PayrollContract)
  36. payroll.setProvider(this.state.web3.currentProvider)
  37. // Declaring this for later so we can chain functions on Payroll.
  38. //var payrollInstance
  39. // Get accounts.
  40. this.state.web3.eth.getAccounts((error, accounts) => {
  41. //console.log(accounts);
  42. this.setState({
  43. accounts,
  44. selectedAccount:accounts && accounts[0]
  45. });
  46. payroll.deployed().then((instance) => {
  47. this.setState({
  48. payroll:instance
  49. });
  50. });
  51. })
  52. }
  53. onSelectAccount=(employee)=>{
  54. this.setState({
  55. selectedAccount:employee.target.text
  56. });
  57. }
  58. render() {
  59. //console.log('call render');
  60. //console.log(this.state);
  61. const {selectedAccount,accounts,payroll,web3}=this.state;
  62. if(!accounts){
  63. return <div>Loading</div>;
  64. }
  65. return (
  66. <div className="App">
  67. <nav className="navbar pure-menu pure-menu-horizontal">
  68. <a href="#" className="pure-menu-heading pure-menu-link">Ted智能合约学习Demo</a>
  69. </nav>
  70. <main className="container">
  71. <div className="pure-g">
  72. <div className="pure-u-1-3">
  73. <Accounts accounts={accounts} onSelectAccount={this.onSelectAccount} />
  74. </div>
  75. <div className="pure-u-2-1">
  76. {
  77. selectedAccount === accounts[0]?
  78. <Employer employer={selectedAccount} payroll={payroll} web3={web3} /> :
  79. <Employee employee={selectedAccount} payroll={payroll} web3={web3} />
  80. }
  81. {
  82. payroll && <Common account={selectedAccount} payroll={payroll} web3={web3} />
  83. }
  84. </div>
  85. </div>
  86. </main>
  87. </div>
  88. );
  89. }
  90. }
  91. export default App