-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathuseListForm.ts
More file actions
237 lines (204 loc) · 6.95 KB
/
useListForm.ts
File metadata and controls
237 lines (204 loc) · 6.95 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { ChangeEvent, useState } from "react";
import { buildTransaction } from "@wpdas/naxios";
import { useRouter } from "next/router";
import { prop } from "remeda";
import { LISTS_CONTRACT_ACCOUNT_ID } from "@/common/_config";
import { syncApi } from "@/common/api/indexer";
import { contractApi } from "@/common/blockchains/near-protocol/client";
import { listsContractClient } from "@/common/contracts/core/lists";
import { floatToYoctoNear } from "@/common/lib";
import { AccountId } from "@/common/types";
import { useWalletUserSession } from "@/common/wallet";
import { AccountGroupItem, validateAccountId } from "@/entities/_shared/account";
import { useDispatch } from "@/store/hooks";
import { ListFormModalType } from "../types";
export const useListForm = () => {
const { push, query } = useRouter();
const dispatch = useDispatch();
const viewer = useWalletUserSession();
const [transferAccountField, setTransferAccountField] = useState<string>("");
const [transferAccountError, setTransferAccountError] = useState<string | undefined>("");
const [finishModal, setFinishModal] = useState<{
open: boolean;
type: ListFormModalType;
}>({ open: false, type: ListFormModalType.NONE });
const [admins, setAdmins] = useState<AccountId[]>([]);
const [accounts, setAccounts] = useState<AccountId[]>([]);
const id = query.id;
const description = "You may now close this modal";
const handleDeleteList = (id: number) => {
if (!id) return;
listsContractClient
.delete_list({ list_id: id })
.then(async ({ txHash }) => {
// Sync deletion to indexer
if (txHash && viewer.accountId) {
await syncApi.listDelete(id, txHash, viewer.accountId).catch(() => {});
}
push("/lists");
})
.catch((error) => {
console.error("Error deleting list", error);
});
dispatch.listEditor.updateListModalState({
header: "List Deleted Successfully",
description,
type: ListFormModalType.DELETE_LIST,
});
};
const handleRegisterBatch = (registrants: string[]) => {
const listId = parseInt(id as any);
listsContractClient
.register_batch({
list_id: listId as any,
registrations: registrants.map((data: string) => ({
registrant_id: data,
status: "Approved",
submitted_ms: Date.now(),
updated_ms: Date.now(),
notes: "",
})),
})
.then(async () => {
// Sync registrations to indexer
await syncApi.listRegistrations(listId).catch(() => {});
setFinishModal({ open: true, type: ListFormModalType.BATCH_REGISTER });
})
.catch((error) => console.error(error));
dispatch.listEditor.updateListModalState({
header: "Account(s) Registered Successfully",
description,
type: ListFormModalType.BATCH_REGISTER,
});
};
const handleUnRegisterAccount = (registrants: AccountGroupItem[]) => {
if (!id) return;
const listId = Number(id);
const allTransactions: any = [];
registrants.map((registrant: AccountGroupItem) => {
allTransactions.push(
buildTransaction("unregister", {
receiverId: LISTS_CONTRACT_ACCOUNT_ID,
args: {
list_id: listId,
registration_id: Number(registrant.registrationId),
},
deposit: floatToYoctoNear(0.015),
gas: "300000000000000",
}),
);
});
contractApi({
contractId: LISTS_CONTRACT_ACCOUNT_ID,
})
.callMultiple(allTransactions)
.then(async (_res) => {
// Sync registrations to indexer after unregister
await syncApi.listRegistrations(listId).catch(() => {});
dispatch.listEditor.updateListModalState({
header: "Account(s) Deleted From List Successfully",
description,
type: ListFormModalType.UNREGISTER,
});
})
.catch((err) => console.error(err));
};
const handleRemoveAdmin = (accounts: AccountGroupItem[]) => {
const accountIds = accounts.map(prop("accountId"));
const listId = Number(id);
listsContractClient
.remove_admins_from_list({
list_id: listId,
admins: accountIds,
})
.then(async () => {
// Sync list to indexer after admin removal
await syncApi.list(listId).catch(() => {});
setFinishModal({ open: true, type: ListFormModalType.REMOVE_ADMINS });
})
.catch((error) => {
console.error("Error adding admins to list", error);
});
dispatch.listEditor.updateListModalState({
header: "Admin(s) Removed Successfully",
description,
type: ListFormModalType.REMOVE_ADMINS,
});
};
const handleSaveAdminsSettings = (admins: AccountId[]) => {
if (!id) return;
const listId = Number(id);
listsContractClient
.add_admins_to_list({
list_id: listId,
admins,
})
.then(async () => {
// Sync list to indexer after admin addition
await syncApi.list(listId).catch(() => {});
setFinishModal({ open: true, type: ListFormModalType.ADD_ADMINS });
})
.catch((error) => {
console.error("Error adding admins to list", error);
});
dispatch.listEditor.updateListModalState({
header: "Admin(s) Added Successfully",
description,
type: ListFormModalType.ADD_ADMINS,
});
};
const handleChangeTransferOwnerField = async (event: ChangeEvent<HTMLInputElement>) => {
const { value } = event.target;
setTransferAccountField(value);
// FIXME: //! Create a form with a field validated by nearProtocolSchemas.validAccountId instead!
const data = await validateAccountId(value);
setTransferAccountError(data);
};
const handleTransferOwner = () => {
if (transferAccountError && !transferAccountField) return;
if (!id) return; // Ensure id is available
const listId = parseInt(id as string);
listsContractClient
.transfer_list_ownership({
list_id: listId,
new_owner_id: transferAccountField,
})
.then(async (data) => {
if (data) {
// Sync list to indexer after ownership transfer
await syncApi.list(listId).catch(() => {});
setFinishModal({
open: true,
type: ListFormModalType.TRANSFER_OWNER,
});
}
})
.catch((error) => {
console.error("Error Transferring Owner", error);
});
dispatch.listEditor.updateListModalState({
header: "Transfer of Ownership Successfully",
description,
type: ListFormModalType.TRANSFER_OWNER,
});
};
return {
handleDeleteList,
handleSaveAdminsSettings,
handleChangeTransferOwnerField,
handleTransferOwner,
transferAccountField,
transferAccountError,
setTransferAccountField,
handleRegisterBatch,
finishModal,
setFinishModal,
handleRemoveAdmin,
accounts,
setAccounts,
setTransferAccountError,
handleUnRegisterAccount,
admins,
setAdmins,
};
};