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