Skip to content

Commit dd348e6

Browse files
authored
Merge pull request #59 from quantanet/issue58
issue fix #58
2 parents e2014e7 + 8d73232 commit dd348e6

File tree

7 files changed

+108
-11
lines changed

7 files changed

+108
-11
lines changed

src/actions/index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import {
44
CLEAR_FINAL_RESULT,
55
SET_DEPLOYED_RESULT,
66
CLEAR_DEPLOYED_RESULT,
7-
SET_CALL_RESULT
7+
SET_CALL_RESULT,
8+
SET_ACC_BALANCE,
9+
SET_UPDATE_BALANCE
810
} from "./types";
911

1012
export const addTestResults = (data: any) => {
@@ -42,3 +44,15 @@ export const setCallResult = (data: any) => {
4244
dispatch({ type: SET_CALL_RESULT, payload: data })
4345
}
4446
}
47+
48+
export const setAccountBalance = (data: any) => {
49+
return (dispatch: Function) => {
50+
dispatch({ type: SET_ACC_BALANCE, payload: data })
51+
}
52+
}
53+
54+
export const setCurrAccChange = (data: any) => {
55+
return (dispatch: Function) => {
56+
dispatch({ type: SET_UPDATE_BALANCE, payload: data })
57+
}
58+
}

src/actions/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ export const CLEAR_FINAL_RESULT = "CLEAR_FINAL_RESULT"
44
export const SET_DEPLOYED_RESULT = "SET_DEPLOYED_RESULT"
55
export const CLEAR_DEPLOYED_RESULT = "CLEAR_DEPLOYED_RESULT"
66
export const SET_CALL_RESULT = "SET_CALL_RESULT"
7+
export const SET_ACC_BALANCE = "SET_ACC_BALANCE"
8+
export const SET_UPDATE_BALANCE = "SET_UPDATE_BALANCE"

src/components/AccountsSelector/AccountsSelector.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ class AccountsSelector extends Component<IProps, IState> {
5858
this._handleAccountSelector = this._handleAccountSelector.bind(this);
5959
}
6060

61+
componentDidMount() {
62+
const options: any = [];
63+
const { availableAccounts } = this.props
64+
65+
availableAccounts.map((name: string) => {
66+
const optItm: IOpt = {
67+
value: name,
68+
label: name
69+
};
70+
return options.push(optItm);
71+
});
72+
this.setState({ options })
73+
}
74+
6175
componentDidUpdate(prevProps: any) {
6276
const options: any = [];
6377
const { availableAccounts } = this.props

src/components/App.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@
157157
margin: 20px auto;
158158
}
159159

160+
.account_balance {
161+
margin: 10px 0;
162+
}
163+
160164
.account_form {
161165
display: flex;
162166
flex-direction: column;

src/components/App.tsx

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {
77
clearFinalResult,
88
setDeployedResult,
99
clearDeployedResult,
10-
setCallResult
10+
setCallResult,
11+
setAccountBalance,
12+
setCurrAccChange
1113
} from "../actions";
1214
import "./App.css";
1315
import 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

3542
interface 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

388421
export default connect(
389422
mapStateToProps,
390-
{ addTestResults, addFinalResultCallback, clearFinalResult, setDeployedResult, clearDeployedResult, setCallResult }
423+
{ addTestResults, addFinalResultCallback, clearFinalResult, setDeployedResult, setAccountBalance, setCurrAccChange, clearDeployedResult, setCallResult }
391424
)(App);

src/reducers/accountReducer.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { SET_ACC_BALANCE, SET_UPDATE_BALANCE } from "../actions/types"
2+
3+
const initialState = {
4+
accountBalance: 0,
5+
accounts: [],
6+
currAccount: ''
7+
};
8+
9+
export default (state: any = initialState, action: any) => {
10+
11+
switch (action.type) {
12+
case SET_ACC_BALANCE:
13+
return {
14+
...state,
15+
accountBalance: action.payload.balance,
16+
accounts: action.payload.accounts,
17+
currAccount: action.payload.currAccount
18+
}
19+
case SET_UPDATE_BALANCE:
20+
return {
21+
...state,
22+
accountBalance: +action.payload.balance,
23+
currAccount: action.payload.currAccount
24+
}
25+
default:
26+
return state;
27+
}
28+
}

src/reducers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { combineReducers } from "redux";
22
import testReducer from "./testReducer";
33
import compiledReducer from "./compiledReducer";
4+
import accountReducer from "./accountReducer"
45

56
export default combineReducers({
67
test: testReducer,
78
compiledStore: compiledReducer,
9+
accountStore: accountReducer
810
});

0 commit comments

Comments
 (0)