App.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import React, { Component } from 'react'
  2. import PayrollContract from '../build/contracts/Payroll.json'
  3. import getWeb3 from './utils/getWeb3'
  4. import { Layout, Menu, Spin, Alert } from 'antd';
  5. //import Common from "./components/Common";
  6. //import Accounts from "./components/Accounts";
  7. import Employer from "./components/Employer";
  8. import Employee from "./components/Employee";
  9. // import './css/oswald.css'
  10. // import './css/open-sans.css'
  11. // import './css/pure-min.css'
  12. import 'antd/dist/antd.css';
  13. import './App.css'
  14. const { Header, Content, Footer } = Layout;
  15. class App extends Component {
  16. constructor(props) {
  17. super(props)
  18. this.state = {
  19. web3: null,
  20. mode: 'employer'
  21. }
  22. }
  23. componentWillMount() {
  24. // Get network provider and web3 instance.
  25. // See utils/getWeb3 for more info.
  26. getWeb3.then(results => {
  27. this.setState({
  28. web3: results.web3
  29. })
  30. // Instantiate contract once web3 provided.
  31. this.instantiateContract()
  32. })
  33. .catch(() => {
  34. console.log('Error finding web3.')
  35. })
  36. }
  37. instantiateContract() {
  38. const contract = require('truffle-contract')
  39. const payroll = contract(PayrollContract)
  40. payroll.setProvider(this.state.web3.currentProvider)
  41. // Get accounts.
  42. this.state.web3.eth.getAccounts((error, accounts) => {
  43. console.log(accounts);
  44. this.setState({
  45. account:accounts && accounts[0]
  46. });
  47. payroll.deployed().then((instance) => {
  48. this.setState({
  49. payroll:instance
  50. });
  51. });
  52. })
  53. }
  54. // onSelectAccount=(employee)=>{
  55. //
  56. // this.setState({
  57. // selectedAccount:employee.target.text
  58. // });
  59. //
  60. // }
  61. onSelectTab = ({key}) => {
  62. this.setState({
  63. mode: key
  64. });
  65. }
  66. renderContent = () => {
  67. const {account, payroll, web3, mode } = this.state;
  68. if(!payroll) {
  69. return <Spin tip="Loading..." />
  70. }
  71. switch (mode) {
  72. case 'employee':
  73. return <Employee employee={account} payroll={payroll} web3={web3} />
  74. case 'employer':
  75. return <Employer account={account} payroll={payroll} web3={web3} />
  76. case 'about':
  77. return <Alert message="欢迎使用Ted开发的以太坊薪酬管理系统" type="info"/>
  78. default:
  79. return <Alert message="请选择一个模式" type="info" showIcon />
  80. }
  81. }
  82. render() {
  83. //console.log('call render');
  84. //console.log(this.state);
  85. // const {selectedAccount,accounts,payroll,web3}=this.state;
  86. //
  87. // if(!accounts){
  88. // return <div>Loading</div>;
  89. // }
  90. return (
  91. <Layout>
  92. <Header className='header'>
  93. <div className='logo'>Ted区块链开发</div>
  94. <Menu
  95. theme="dark"
  96. mode="horizontal"
  97. defaultSelectedKeys={['employer']}
  98. style={{lineHeight:'64px'}}
  99. onSelect={this.onSelectTab}
  100. >
  101. <Menu.Item key="employer">雇主</Menu.Item>
  102. <Menu.Item key="employee">雇员</Menu.Item>
  103. <Menu.Item key="about">关于</Menu.Item>
  104. </Menu>
  105. </Header>
  106. <Content style={{padding:'0 50px'}}>
  107. <Layout style={{ padding:'24px 0', background:'#fff', minHeight: '600px'}}>
  108. {this.renderContent()}
  109. </Layout>
  110. </Content>
  111. <Footer style={{ textAlign: 'center' }}>
  112. Payroll ©2018 TedXiong xiong-wei@hotmail.com
  113. </Footer>
  114. </Layout>
  115. );
  116. }
  117. }
  118. export default App