Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
car-crushers-portal/components/AppealBans.tsx
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
117 lines (105 sloc)
2.83 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
Link, | |
Modal, | |
ModalBody, | |
ModalCloseButton, | |
ModalContent, | |
ModalFooter, | |
ModalHeader, | |
ModalOverlay, | |
Table, | |
TableContainer, | |
Tbody, | |
Td, | |
Th, | |
Thead, | |
Tr, | |
useDisclosure, | |
useToast, | |
} from "@chakra-ui/react"; | |
import { useEffect, useState } from "react"; | |
import NewAppealBan from "./NewAppealBan.js"; | |
export default function (props: { isOpen: boolean; onClose: () => void }) { | |
const [entries, setEntries] = useState( | |
[] as { created_at: number | string; created_by: string; user: string }[], | |
); | |
const toast = useToast(); | |
const { isOpen, onClose, onOpen } = useDisclosure(); | |
async function removeBan(user: string) { | |
const removeResp = await fetch(`/api/appeals/${user}/ban`, { | |
method: "DELETE", | |
}); | |
if (!removeResp.ok) { | |
let error; | |
try { | |
error = ((await removeResp.json()) as { error: string }).error; | |
} catch { | |
error = "Unknown error"; | |
} | |
toast({ | |
description: error, | |
isClosable: true, | |
status: "error", | |
title: "Failed to remove member", | |
}); | |
return; | |
} | |
toast({ | |
description: `User ${user} removed`, | |
isClosable: true, | |
status: "success", | |
title: "Success", | |
}); | |
setEntries([...entries].filter((entry) => entry.user !== user)); | |
} | |
useEffect(() => { | |
(async function () { | |
const banData = await fetch("/api/appeals/bans"); | |
if (!banData.ok) return; | |
setEntries(await banData.json()); | |
}); | |
}, []); | |
return ( | |
<> | |
<Modal isCentered isOpen={props.isOpen} onClose={props.onClose}> | |
<ModalOverlay /> | |
<ModalContent> | |
<ModalHeader>Appeal Bans</ModalHeader> | |
<ModalCloseButton /> | |
<ModalBody> | |
<TableContainer> | |
<Table variant="simple"> | |
<Thead> | |
<Tr> | |
<Th>User</Th> | |
<Th>Moderator</Th> | |
<Th>Time</Th> | |
<Th>Remove</Th> | |
</Tr> | |
</Thead> | |
<Tbody> | |
{entries.map((entry) => ( | |
<Tr> | |
<Td>{entry.user}</Td> | |
<Td>{entry.created_by}</Td> | |
<Td>{new Date(entry.created_at).toUTCString()}</Td> | |
<Td> | |
<Link onClick={async () => await removeBan(entry.user)}> | |
Remove | |
</Link> | |
</Td> | |
</Tr> | |
))} | |
</Tbody> | |
</Table> | |
</TableContainer> | |
</ModalBody> | |
<ModalFooter> | |
<Link onClick={onOpen}>Click here to ban someone</Link> | |
</ModalFooter> | |
</ModalContent> | |
</Modal> | |
<NewAppealBan isOpen={isOpen} onClose={onClose} /> | |
</> | |
); | |
} |