Skip to content
Permalink
Newer
Older
100644 143 lines (134 sloc) 3.7 KB
October 19, 2023 16:49
1
import {
2
Box,
October 19, 2023 16:49
3
Button,
October 19, 2023 16:49
4
Card,
5
CardBody,
6
CardFooter,
7
CardHeader,
8
Heading,
October 19, 2023 16:49
9
Modal,
10
ModalBody,
11
ModalContent,
12
ModalFooter,
13
ModalHeader,
14
ModalOverlay,
October 19, 2023 16:49
15
Stack,
16
StackDivider,
17
Text,
October 19, 2023 16:49
18
Textarea,
19
useDisclosure,
20
useToast,
October 19, 2023 16:49
21
} from "@chakra-ui/react";
22
import { useEffect, useState } from "react";
October 19, 2023 16:49
23
October 19, 2023 16:49
24
export default function (props: AppealCardProps) {
25
const [dateString, setDateString] = useState(
October 19, 2023 16:50
26
new Date(props.created_at).toUTCString(),
27
);
October 19, 2023 16:49
28
const [action, setAction] = useState("");
29
const [feedback, setFeedback] = useState("");
October 19, 2023 16:50
30
const [loading, setLoading] = useState(false);
October 19, 2023 16:50
31
const toast = useToast();
32
33
useEffect(() => {
34
setDateString(new Date(props.created_at).toLocaleString());
35
}, [props.created_at]);
36
October 19, 2023 16:49
37
const { isOpen, onClose, onOpen } = useDisclosure();
38
39
function showModal(action: "Accept" | "Deny") {
40
setAction(action);
41
onOpen();
42
}
43
44
async function takeAction(action: string) {
October 19, 2023 16:50
45
setLoading(true);
October 19, 2023 16:49
46
const actionReq = await fetch(`/api/appeals/${props.id}/${action}`, {
47
body: feedback ? JSON.stringify({ feedback }) : "{}",
48
headers: {
49
"content-type": "application/json",
50
},
51
method: "POST",
52
});
53
54
if (actionReq.ok) {
October 19, 2023 16:50
55
setLoading(false);
October 19, 2023 16:50
56
toast({
October 19, 2023 16:49
57
description: `Appeal ${action === "accept" ? "accepted" : "denied"}`,
58
duration: 5000,
59
status: "success",
60
title: "Success",
61
});
62
63
document.getElementById(`appeal_${props.id}`)?.remove();
October 19, 2023 16:50
64
} else {
October 19, 2023 16:50
65
setLoading(false);
October 19, 2023 16:50
66
toast({
67
description: ((await actionReq.json()) as { error: string }).error,
68
duration: 10000,
69
status: "error",
70
title: "Oops!",
71
});
October 19, 2023 16:49
72
}
73
October 19, 2023 16:50
74
onClose();
October 19, 2023 16:50
75
setLoading(false);
October 19, 2023 16:49
76
}
77
October 19, 2023 16:49
78
return (
October 19, 2023 16:50
79
<Card id={`appeal_${props.user.id}`} key={props.id} w="100%">
October 19, 2023 16:49
80
<Modal isOpen={isOpen} onClose={onClose}>
81
<ModalOverlay />
82
<ModalContent>
83
<ModalHeader>{action} this appeal?</ModalHeader>
84
<ModalBody>
85
<Text size="xs">Optionally provide feedback</Text>
86
<Textarea
87
onChange={(e) => setFeedback(e.target.value)}
88
placeholder="Your feedback"
89
/>
90
</ModalBody>
91
<ModalFooter>
92
<Button
93
onClick={async () => await takeAction(action.toLowerCase())}
October 19, 2023 16:50
94
isLoading={loading}
October 19, 2023 16:50
95
loadingText="Submitting..."
October 19, 2023 16:49
96
>
97
Submit
98
</Button>
99
</ModalFooter>
100
</ModalContent>
101
</Modal>
October 19, 2023 16:49
102
<CardHeader>
October 19, 2023 16:50
103
<Heading size="md">Appeal for {props.user.username}</Heading>
104
<Text fontSize="xs">ID: {props.user.id}</Text>
October 19, 2023 16:49
105
</CardHeader>
106
<CardBody>
107
<Stack divider={<StackDivider />}>
October 19, 2023 16:49
108
<Box>
October 19, 2023 16:49
109
<Heading size="xs">Response: Why were you banned?</Heading>
110
<Text>{props.ban_reason}</Text>
October 19, 2023 16:49
111
</Box>
October 19, 2023 16:49
112
<Box>
113
<Heading size="xs">Response: Why should we unban you?</Heading>
114
<Text>{props.reason_for_unban}</Text>
115
</Box>
116
<Box>
117
<Heading size="xs">
118
Response: What have you learned from your mistake?
119
</Heading>
120
<Text>{props.learned}</Text>
121
</Box>
122
</Stack>
123
</CardBody>
124
<CardFooter pb="4px">
125
<Box>
October 19, 2023 16:49
126
<Button colorScheme="red" onClick={() => showModal("Deny")}>
127
Deny
128
</Button>
129
<Button
130
colorScheme="blue"
131
ml="8px"
132
onClick={() => showModal("Accept")}
133
>
October 19, 2023 16:49
134
Accept
135
</Button>
136
</Box>
137
</CardFooter>
138
<CardFooter py="4px">
139
<Text fontSize="xs">Submitted at: {dateString}</Text>
140
</CardFooter>
141
</Card>
October 19, 2023 16:49
142
);
143
}