-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.ts
219 lines (187 loc) · 5.38 KB
/
hooks.ts
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
import { useCallback, useContext, useState } from "react";
import { useEffect } from "react";
import { getSubmissions } from "./lib/formSubmission";
import { useSignTypedData, useProvider, useAccount } from "wagmi";
import {
Form,
FormSubmission,
WagmiEIP712TypedMessage,
FormSubmissionInput,
FormUploadInput,
PageInfo
} from "./types";
import { SIGNATURE_DATA_TYPES, SIGNATURE_DOMAIN } from "./config";
import axios from "./lib/axios";
import { getForm, getForms, getUserFormCount } from "./lib/form";
import { submitAnswer } from "./lib/formSubmission";
import ConnectWalletModalContext from "./contexts/ConnectWalletModalContext";
import { getFormUrl } from "./utils";
export const useForm = (formId: string | undefined) => {
const [form, setForm] = useState<Form | null>();
const [formNotFound, setFormNotFound] = useState<boolean>(false);
useEffect(() => {
(async () => {
if (formId) {
const _form = await getForm(formId);
if (!_form) {
setFormNotFound(true);
} else {
setForm(_form);
}
}
})();
}, [formId]);
return { form, formNotFound };
};
const first = 1;
export const useSubmissions = (
formId: string | undefined
): {
submissions: FormSubmission[];
getNext: () => void;
getPrevious: () => void;
hasNextPage: boolean;
hasPreviousPage: boolean;
} => {
const [submissions, setSubmissions] = useState<FormSubmission[]>([]);
const [cursors, setCursors] = useState<string[]>([]);
const [currentPage, setCurrentPage] = useState<number>(0);
const [pageInfo, setPageInfo] = useState<PageInfo>({
hasNextPage: false
});
useEffect(() => {
(async () => {
if (formId) {
const result = await getSubmissions({
formId,
first
});
setSubmissions(result.submissions);
setPageInfo(result.pageInfo);
setCursors(result.cursors);
}
})();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [first, formId]);
const getNext = useCallback(() => {
(async () => {
if (formId) {
const result = await getSubmissions({
formId,
first,
after: cursors[cursors.length - 1]
});
setCursors([...cursors, ...result.cursors]);
setCurrentPage(currentPage + 1);
setPageInfo(result.pageInfo);
setSubmissions(result.submissions);
}
})();
}, [currentPage, cursors, formId]);
const getPrevious = useCallback(() => {
(async () => {
if (formId) {
const result = await getSubmissions({
formId,
first,
after: currentPage === 1 ? "" : cursors[currentPage * first - 1]
});
setCursors([...cursors, ...result.cursors]);
setCurrentPage(currentPage - 1);
setPageInfo(result.pageInfo);
setSubmissions(result.submissions);
}
})();
}, [currentPage, cursors, formId]);
return {
submissions,
getNext,
getPrevious,
hasNextPage: pageInfo.hasNextPage,
hasPreviousPage: currentPage !== 0
};
};
export const useUploadForm = () => {
const [uploading, setUploading] = useState<boolean>(false);
const [uploadComplete, setUploadComplete] = useState<boolean>(false);
const provider = useProvider({
// @ts-ignore
// NEXT_PUBLIC_CHAIN_ID is set to 5 (Goerli) in env.development
chainId: parseInt(process.env.NEXT_PUBLIC_CHAIN_ID)
});
const { signTypedDataAsync } = useSignTypedData();
const uploadForm = async (form: FormUploadInput) => {
setUploading(true);
const eip712TypedMessage: WagmiEIP712TypedMessage = {
domain: SIGNATURE_DOMAIN[provider.network.name],
types: SIGNATURE_DATA_TYPES,
value: form,
primaryType: "Form"
};
let signature;
try {
signature = await signTypedDataAsync(eip712TypedMessage);
} catch (err) {
setUploading(false);
}
if (!signature) {
return;
}
const formInput = {
signature,
eip712TypedMessage
};
await axios.post("/forms", formInput);
setUploading(false);
setUploadComplete(true); // TODO Change the name
};
return { uploading, uploadForm, uploadComplete };
};
export const useUserForms = (): Form[] | undefined => {
const { address } = useAccount();
const [forms, setForms] = useState<Form[] | undefined>();
useEffect(() => {
(async () => {
if (address) {
setForms(await getForms({ owner: address }));
}
})();
}, [address]);
return forms;
};
export const useSubmitForm = () => {
const [submittingForm, setSubmittingForm] = useState<boolean>(false);
const [submissionComplete, setSubmissionComplete] = useState<boolean>(false);
const submitForm = async (formSubmission: FormSubmissionInput) => {
setSubmittingForm(true);
await submitAnswer(formSubmission);
setSubmittingForm(false);
setSubmissionComplete(true);
};
return {
submittingForm,
submitForm,
submissionComplete
};
};
export const useConnectWallet = () => {
const { open } = useContext(ConnectWalletModalContext);
const connect = () => {
open();
};
return connect;
};
export const useUserFormCount = () => {
const [formCount, setFormCount] = useState<number>();
const { address } = useAccount();
useEffect(() => {
(async () => {
if (address) {
setFormCount(await getUserFormCount(address));
}
})();
}, [address]);
return {
formCount
};
};