Files
fast_api_template/frontend/src/components/Common/DeleteAlert.tsx
T

111 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-03-08 14:58:36 +01:00
import {
AlertDialog,
AlertDialogBody,
AlertDialogContent,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogOverlay,
Button,
2024-03-17 17:28:45 +01:00
} from "@chakra-ui/react"
import React from "react"
import { useForm } from "react-hook-form"
import { useMutation, useQueryClient } from "react-query"
2024-03-17 17:28:45 +01:00
import { ItemsService, UsersService } from "../../client"
import useCustomToast from "../../hooks/useCustomToast"
interface DeleteProps {
2024-03-08 14:58:36 +01:00
type: string
id: number
isOpen: boolean
onClose: () => void
}
2024-03-28 20:22:28 -05:00
const Delete = ({ type, id, isOpen, onClose }: DeleteProps) => {
2024-03-08 14:58:36 +01:00
const queryClient = useQueryClient()
const showToast = useCustomToast()
const cancelRef = React.useRef<HTMLButtonElement | null>(null)
const {
handleSubmit,
formState: { isSubmitting },
} = useForm()
2024-03-08 14:58:36 +01:00
const deleteEntity = async (id: number) => {
2024-03-17 17:28:45 +01:00
if (type === "Item") {
2024-03-08 14:58:36 +01:00
await ItemsService.deleteItem({ id: id })
2024-03-17 17:28:45 +01:00
} else if (type === "User") {
2024-03-08 14:58:36 +01:00
await UsersService.deleteUser({ userId: id })
} else {
throw new Error(`Unexpected type: ${type}`)
}
2024-03-08 14:58:36 +01:00
}
2024-03-08 14:58:36 +01:00
const mutation = useMutation(deleteEntity, {
onSuccess: () => {
showToast(
2024-03-17 17:28:45 +01:00
"Success",
2024-03-08 14:58:36 +01:00
`The ${type.toLowerCase()} was deleted successfully.`,
2024-03-17 17:28:45 +01:00
"success",
2024-03-08 14:58:36 +01:00
)
onClose()
},
onError: () => {
showToast(
2024-03-17 17:28:45 +01:00
"An error occurred.",
2024-03-08 14:58:36 +01:00
`An error occurred while deleting the ${type.toLowerCase()}.`,
2024-03-17 17:28:45 +01:00
"error",
2024-03-08 14:58:36 +01:00
)
},
onSettled: () => {
2024-03-17 17:28:45 +01:00
queryClient.invalidateQueries(type === "Item" ? "items" : "users")
2024-03-08 14:58:36 +01:00
},
})
2024-03-08 14:58:36 +01:00
const onSubmit = async () => {
mutation.mutate(id)
}
2024-03-08 14:58:36 +01:00
return (
<>
<AlertDialog
isOpen={isOpen}
onClose={onClose}
leastDestructiveRef={cancelRef}
2024-03-17 17:28:45 +01:00
size={{ base: "sm", md: "md" }}
2024-03-08 14:58:36 +01:00
isCentered
>
<AlertDialogOverlay>
<AlertDialogContent as="form" onSubmit={handleSubmit(onSubmit)}>
<AlertDialogHeader>Delete {type}</AlertDialogHeader>
2024-03-08 14:58:36 +01:00
<AlertDialogBody>
2024-03-17 17:28:45 +01:00
{type === "User" && (
2024-03-08 14:58:36 +01:00
<span>
2024-03-17 17:28:45 +01:00
All items associated with this user will also be{" "}
2024-03-08 14:58:36 +01:00
<strong>permantly deleted. </strong>
</span>
)}
Are you sure? You will not be able to undo this action.
</AlertDialogBody>
2024-03-08 14:58:36 +01:00
<AlertDialogFooter gap={3}>
2024-03-11 16:50:46 +01:00
<Button variant="danger" type="submit" isLoading={isSubmitting}>
2024-03-08 14:58:36 +01:00
Delete
</Button>
<Button
ref={cancelRef}
onClick={onClose}
isDisabled={isSubmitting}
>
Cancel
</Button>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialogOverlay>
</AlertDialog>
</>
)
}
2024-03-08 14:58:36 +01:00
export default Delete