77 clearFinalResult ,
88 setDeployedResult ,
99 clearDeployedResult ,
10- setCallResult
10+ setCallResult ,
11+ setAccountBalance ,
12+ setCurrAccChange
1113} from "../actions" ;
1214import "./App.css" ;
1315import ContractCompiled from "./ContractCompiled" ;
@@ -29,7 +31,12 @@ interface IProps {
2931 setDeployedResult : ( result : any ) => void ;
3032 clearDeployedResult : ( ) => void ;
3133 setCallResult : ( result : any ) => void ;
34+ setAccountBalance : ( accData : any ) => void ;
35+ setCurrAccChange : ( accData : any ) => void ;
3236 test : any ;
37+ accountBalance : number ,
38+ accounts : string [ ] ,
39+ currAccount : string
3340}
3441
3542interface IState {
@@ -79,7 +86,7 @@ class App extends Component<IProps, IState> {
7986 this . handleTransactionSubmit = this . handleTransactionSubmit . bind ( this ) ;
8087 }
8188 public componentDidMount ( ) {
82- window . addEventListener ( "message" , event => {
89+ window . addEventListener ( "message" , async event => {
8390 const { data } = event ;
8491
8592 if ( data . compiled ) {
@@ -95,7 +102,7 @@ class App extends Component<IProps, IState> {
95102 if ( compiled . errors && compiled . errors . length > 0 ) {
96103 this . setState ( { message : compiled . errors } ) ;
97104 }
98- this . setState ( { compiled, fileName, processMessage : "" } ) ;
105+ this . setState ( { compiled, fileName, processMessage : "" , contractName : Object . keys ( compiled . contracts [ fileName ] ) [ 0 ] } ) ;
99106 }
100107
101108 if ( data . processMessage ) {
@@ -161,17 +168,40 @@ class App extends Component<IProps, IState> {
161168 this . props . setCallResult ( result ) ;
162169 }
163170 if ( data . fetchAccounts ) {
164- this . setState ( { accounts : data . fetchAccounts . accounts , currAccount : data . fetchAccounts . accounts [ 0 ] , balance : data . fetchAccounts . balance } ) ;
171+ const balance = data . fetchAccounts . balance
172+ const currAccount = data . fetchAccounts . accounts [ 0 ]
173+ const accounts = data . fetchAccounts . accounts
174+ const accData = {
175+ balance,
176+ currAccount,
177+ accounts
178+ }
179+ await this . props . setAccountBalance ( accData )
180+ this . setState ( { accounts : this . props . accounts , currAccount : this . props . currAccount , balance : this . props . accountBalance } ) ;
165181 }
166182 if ( data . transactionResult ) {
167183 this . setState ( { transactionResult : data . transactionResult } ) ;
168184 }
169185 if ( data . balance ) {
170- this . setState ( { balance : + data . balance } ) ;
186+ const accData = {
187+ balance : data . balance ,
188+ currAccount : this . state . currAccount
189+ }
190+ this . props . setCurrAccChange ( accData )
191+ this . setState ( { balance : this . props . accountBalance } ) ;
171192 }
172193 // TODO: handle error message
173194 } ) ;
174195 }
196+
197+ componentDidUpdate ( ) {
198+ if ( this . props . accounts !== this . state . accounts ) {
199+ this . setState ( {
200+ accounts : this . props . accounts
201+ } )
202+ }
203+ }
204+
175205 private handleTransactionSubmit ( event : any ) {
176206 event . preventDefault ( ) ;
177207 const { currAccount } = this . state ;
@@ -288,8 +318,7 @@ class App extends Component<IProps, IState> {
288318 getSelectedAccount = { this . getSelectedAccount }
289319 />
290320 < div className = "account_balance" >
291- < label > Account Balance:</ label >
292- < pre > { balance } </ pre >
321+ < b > Account Balance: </ b > { balance }
293322 </ div >
294323 </ div >
295324 ) }
@@ -312,7 +341,7 @@ class App extends Component<IProps, IState> {
312341 </ div >
313342 </ div >
314343 }
315- { contractName &&
344+ { ( compiled && contractName ) &&
316345 < div className = "compiledOutput" >
317346 < div id = { contractName } className = "contract-container" >
318347 {
@@ -379,13 +408,17 @@ class App extends Component<IProps, IState> {
379408 }
380409}
381410
382- function mapStateToProps ( { test } : any ) {
411+ function mapStateToProps ( { test, accountStore } : any ) {
412+ const { accountBalance, accounts, currAccount } = accountStore
383413 return {
414+ accountBalance,
415+ accounts,
416+ currAccount,
384417 test,
385418 } ;
386419}
387420
388421export default connect (
389422 mapStateToProps ,
390- { addTestResults, addFinalResultCallback, clearFinalResult, setDeployedResult, clearDeployedResult, setCallResult }
423+ { addTestResults, addFinalResultCallback, clearFinalResult, setDeployedResult, setAccountBalance , setCurrAccChange , clearDeployedResult, setCallResult }
391424) ( App ) ;
0 commit comments