-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.tsx
43 lines (39 loc) · 1.12 KB
/
utils.tsx
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
import { format } from "date-fns";
import type { FormEvent, ReactElement } from "react";
import React from "react";
import Modal from "@/components/shared/Modal";
export function submitVote(
e: FormEvent<HTMLFormElement>,
setModal: (v: ReactElement | null) => void,
setDisabled: (v: boolean) => void,
setHasVoted: (v: boolean) => void,
payload: { pollId: string, selection: number[] },
) {
e.preventDefault();
e.stopPropagation();
const error = () => {
setModal(
<Modal closeButtonText="Got it">
An error occurred while sending your request. Please try again later.
</Modal>,
);
setDisabled(false);
};
setDisabled(true);
fetch(process.env.NEXT_PUBLIC_API_ORIGIN + "/vote", {
method: "POST",
body: JSON.stringify(payload),
}).then((res) => {
if (!res.ok) {
error();
return;
}
setDisabled(false);
setHasVoted(true);
}).catch(() => {
error();
});
}
export function formatDate(date: Date) {
return format(date, "dd LLLL yyyy hh:mm aa (OOOO)");
}