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