-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave-client-button.jsx
More file actions
194 lines (191 loc) · 5.73 KB
/
save-client-button.jsx
File metadata and controls
194 lines (191 loc) · 5.73 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
State.init({
clientName: props.clientName ? props.clientName : "",
clientId: props.clientId ? props.clientId : null,
clientContract: props.clientContract ? props.clientContract : "",
clientList: [],
abi: props.abi ? props.abi : null,
displayModal: false,
error,
clicked: false,
});
const onInputChangeClientName = ({ target }) => {
State.update({ clientName: target.value });
State.update({ clicked: false });
};
const onInputChangeClientContract = ({ target }) => {
State.update({ error: null, clicked: false });
State.update({ clientContract: target.value });
};
const showModal = (e, type) => {
if (type == "show") {
State.update({ displayModal: true, clicked: false });
}
if (type == "close") {
State.update({ displayModal: false });
}
};
const loadData = () => {
const clientList = Social.get(`${context.accountId}/magicbuild/clientList`);
if (clientList) {
const clientListData = JSON.parse(clientList);
State.update({ clientList: clientListData });
}
};
loadData();
const saveClient = (e) => {
if (!state.clicked) {
State.update({ clicked: true });
if (state.clientName.length < 5) {
State.update({
error: "Name requires more than 5 characters",
});
} else {
asyncFetch("https://rpc.near.org/", {
body: JSON.stringify({
method: "query",
params: {
request_type: "view_code",
account_id: state.clientContract,
finality: "final",
},
id: 154,
jsonrpc: "2.0",
}),
headers: {
"Content-Type": "application/json",
},
method: "POST",
}).then((res) => {
if (res.body.result.code_base64) {
const data = state.clientList;
if (state.clientId) {
data.forEach((item, index) => {
if (item.clientId == state.clientId) {
data[index].abi = state.abi;
data[index].clientName = state.clientName;
}
});
} else {
const clientId = Date.now();
const clientData = {
clientId: clientId,
clientName: state.clientName,
address: state.clientContract,
archived: false,
abi: state.abi,
};
data.push(clientData);
}
const saveData = {
magicbuild: {
clientList: data,
},
};
Social.set(saveData, {
force: true,
onCommit: () => {
State.update({ displayModal: false });
},
onCancel: () => {},
});
} else {
State.update({
error:
"Unable to save Account ID because the contract has not been deployed yet!",
});
}
});
}
}
};
return (
<div>
<label></label>
<button
class="btn btn-primary form-control "
onClick={(e) => showModal(e, "show")}
>
{state.clientId ? "Save Client" : "Create Client"}
</button>
{state.displayModal && (
<>
<div
style={{ display: "block" }}
className={`modal fade show`}
id="createClient"
tabindex="-1"
aria-labelledby="createClientLabel"
>
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="createClientLabel">
Create Client
</h1>
<button
type="button"
class="btn-close"
onClick={(e) => showModal(e, "close")}
></button>
</div>
<div class="modal-body">
<div class="form-group">
<label>Name</label>
<input
class="form-control"
value={state.clientName}
onChange={(e) => onInputChangeClientName(e)}
/>
</div>
<div class="form-group">
<label>Address</label>
<input
class="form-control"
value={state.clientContract}
onChange={(e) => onInputChangeClientContract(e)}
/>
</div>
<div class="form-group">
<label>Chain</label>
<select class="form-control">
<option selected>Near Chain</option>
<option disabled>Ethereum Chain</option>
<option disabled>AVAX Chain</option>
</select>
</div>
{!state.error && (
<small class="form-text text-muted">
A new Client will be created.
</small>
)}
{state.error && (
<p class="text-danger" role="alert">
{state.error}
</p>
)}
</div>
<div class="modal-footer">
<button
type="button"
class="btn btn-secondary"
onClick={(e) => showModal(e, "close")}
>
Close
</button>
<button
type="button"
disabled={state.clicked}
onClick={(e) => saveClient(e)}
class="btn btn-primary"
>
{state.clientId ? "Save" : "Create"}
</button>
</div>
</div>
</div>
</div>
<div class="modal-backdrop fade show"></div>
</>
)}
</div>
);