generated from jimmychu0807/substrate-front-end-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathChainState.js
108 lines (99 loc) · 2.67 KB
/
ChainState.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import React, { useEffect, useState } from 'react';
import { Grid, Form, Dropdown, Input } from 'semantic-ui-react';
import { useSubstrate } from './substrate-lib';
import { TxButton } from './substrate-lib/components';
export default function ChainState (props) {
const { api } = useSubstrate();
const [modulesList, setModulesList] = useState([]);
const [status, setStatus] = useState(null);
const [storageItemsList, setStorageItemsList] = useState([]);
const initialState = {
module: '',
storageItem: '',
input: ''
};
const [formState, setFormState] = useState(initialState);
const { module, storageItem, input } = formState;
useEffect(() => {
const modules = Object.keys(api.query).sort().map(module => ({
key: module,
value: module,
text: module
}));
setModulesList(modules);
}, [api]);
useEffect(() => {
if (module !== '') {
const storageItems = Object.keys(api.query[module]).sort().map(storage => ({
key: storage,
value: storage,
text: storage
}));
setStorageItemsList(storageItems);
}
}, [api, module]);
const onChange = (_, data) => {
setFormState(formState => {
return {
...formState,
[data.state]: data.value
};
});
};
return (
<Grid.Column>
<h1>Chain State</h1>
<Form>
<Form.Field>
<Dropdown
placeholder='Select a module to query'
fluid
label='Module'
onChange={onChange}
search
selection
state='module'
options={modulesList}
value={module}
/>
</Form.Field>
<Form.Field>
<Dropdown
placeholder='Select a storage item to query'
fluid
label='Storage Item'
onChange={onChange}
search
selection
state='storageItem'
options={storageItemsList}
value={storageItem}
/>
</Form.Field>
<Form.Field>
<Input
onChange={onChange}
label='Input'
fluid
placeholder='May not be needed'
state='input'
type='text'
value={input}
/>
</Form.Field>
<Form.Field>
<TxButton
label='Query'
setStatus={setStatus}
type='QUERY'
attrs={{
params: [input],
tx: (api.query[module] && api.query[module][storageItem])
}}
/>
</Form.Field>
<div style={{ overflowWrap: 'break-word' }}>{status}</div>
</Form>
</Grid.Column>
);
}