From 6cb6f67efb5d7af68ae30c3f867e2acd7cd0aad0 Mon Sep 17 00:00:00 2001 From: myh <95896306+Anchor-x@users.noreply.github.com> Date: Wed, 5 Jun 2024 16:12:39 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E8=BE=91=E5=AE=9E=E4=BD=93=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/components/EditEntity.js | 135 ++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 frontend/src/components/EditEntity.js diff --git a/frontend/src/components/EditEntity.js b/frontend/src/components/EditEntity.js new file mode 100644 index 0000000..06c4a96 --- /dev/null +++ b/frontend/src/components/EditEntity.js @@ -0,0 +1,135 @@ +import getWeb3 from '../utils/web3'; +import DeviceManager, { getDefaultAccount } from '../DeviceManager'; + +import React, { Component } from 'react'; +import { Card, Input, Button, Icon, message, notification } from 'antd'; + +const { TextArea } = Input; + +const openNotificationWithIcon = (type, message, description) => { + notification[type]({ + message, + description + }); +}; + +class EditEntity extends Component { + constructor(props) { + super(props); + + this.state = { + myData: null, + showEdit: false, + web3: null, + instance: null, + loading: true, + filter: null + } + + this.toggleEdit = this.toggleEdit.bind(this); + this.commonChange = this.commonChange.bind(this); + this.saveMyData = this.saveMyData.bind(this); + this.updateMyData = this.updateMyData.bind(this); + this.watchForChanges = this.watchForChanges.bind(this); + } + + async componentWillMount() { + try { + let results = await getWeb3; + let instance = await DeviceManager; + + this.setState({ + web3: results.web3, + instance + }); + + await this.updateMyData(); + + } catch (error) { + console.log(error); + message.error(error.message); + } + } + + watchForChanges() { + let filter = this.state.web3.eth.filter('latest', (error, result) => { + if (!error) { + openNotificationWithIcon('success', 'Transaction mined', 'Your entity data has been updated.'); + this.state.filter.stopWatching(); + this.updateMyData(); + } else { + console.error(error); + } + }); + + this.setState({ + filter + }) + } + + async updateMyData() { + try { + let result = await this.state.instance.ownerToEntity(getDefaultAccount()); + this.setState({ + myData: result, + myDataNew: result, + loading: false + }) + } catch (error) { + console.log(error); + message.error(error.message); + } + } + + toggleEdit() { + this.setState(prevState => ({ + showEdit: !prevState.showEdit + })); + } + + commonChange(e) { + this.setState({ + [e.target.name]: e.target.value + }); + } + + async saveMyData() { + try { + if (this.state.myDataNew !== this.state.myData) { + let instance = await DeviceManager; + await instance.updateEntityData(this.state.myDataNew, { from: getDefaultAccount() }); + this.watchForChanges(); + openNotificationWithIcon('info', 'Transaction sent', 'Once mined, your entity data will be updated.'); + this.setState({ + loading: true, + }); + } + this.toggleEdit(); + } catch (error) { + console.log(error); + message.error(error.message); + } + } + + render() { + return ( +
+

+ Edit your entity details. +

+ + {this.state.showEdit ? +
+