2024-02-12 16:46:51 -05:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
|
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-02-26 09:39:09 -05:00
|
|
|
import useCustomToast from '../../hooks/useCustomToast';
|
|
|
|
import { useItemsStore } from '../../store/items-store';
|
|
|
|
import { useUsersStore } from '../../store/users-store';
|
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-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-02-28 18:30:59 -05:00
|
|
|
const { handleSubmit, formState: {isSubmitting} } = useForm();
|
2024-02-12 16:46:51 -05:00
|
|
|
const { deleteItem } = useItemsStore();
|
2024-02-15 17:17:26 -05:00
|
|
|
const { deleteUser } = useUsersStore();
|
2024-02-12 16:46:51 -05:00
|
|
|
|
|
|
|
const onSubmit = async () => {
|
|
|
|
try {
|
2024-02-15 17:17:26 -05:00
|
|
|
type === 'Item' ? await deleteItem(id) : await deleteUser(id);
|
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();
|
|
|
|
} catch (err) {
|
2024-02-26 09:39:09 -05:00
|
|
|
showToast('An error occurred.', `An error occurred while deleting the ${type.toLowerCase()}.`, 'error');
|
2024-02-12 16:46:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<AlertDialog
|
|
|
|
isOpen={isOpen}
|
|
|
|
onClose={onClose}
|
|
|
|
leastDestructiveRef={cancelRef}
|
|
|
|
size={{ base: "sm", md: "md" }}
|
|
|
|
isCentered
|
|
|
|
>
|
|
|
|
<AlertDialogOverlay>
|
|
|
|
<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-02-28 18:30:59 -05: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;
|