Repository navigation
Expand file tree
/
Copy pathDeleteModal.tsx
More file actions
86 lines (77 loc) · 1.95 KB
/
Copy pathDeleteModal.tsx
File metadata and controls
86 lines (77 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import {
Box,
Button,
Modal,
ModalOverlay,
ModalContent,
ModalFooter,
ModalHeader,
ModalBody,
ModalCloseButton,
} from '@chakra-ui/react';
import { useMutation } from '@tanstack/react-query';
import React, { useCallback, useState } from 'react';
import useIsMobile from 'lib/hooks/useIsMobile';
import FormSubmitAlert from 'ui/shared/FormSubmitAlert';
type Props = {
isOpen: boolean;
onClose: () => void;
title: string;
renderContent: () => React.JSX.Element;
mutationFn: () => Promise<unknown>;
onSuccess: () => Promise<void>;
};
const DeleteModal: React.FC<Props> = ({
isOpen,
onClose,
title,
renderContent,
mutationFn,
onSuccess,
}) => {
const [ isAlertVisible, setAlertVisible ] = useState(false);
const onModalClose = useCallback(() => {
setAlertVisible(false);
onClose();
}, [ onClose, setAlertVisible ]);
const { mutate, isPending } = useMutation({
mutationFn,
onSuccess: async() => {
onSuccess();
onClose();
},
onError: () => {
setAlertVisible(true);
},
});
const onDeleteClick = useCallback(() => {
setAlertVisible(false);
mutate();
}, [ setAlertVisible, mutate ]);
const isMobile = useIsMobile();
return (
<Modal isOpen={ isOpen } onClose={ onModalClose } size={ isMobile ? 'full' : 'md' }>
<ModalOverlay/>
<ModalContent>
<ModalHeader fontWeight="500" textStyle="h3">{ title }</ModalHeader>
<ModalCloseButton/>
<ModalBody>
{ isAlertVisible && <Box mb={ 4 }><FormSubmitAlert/></Box> }
{ renderContent() }
</ModalBody>
<ModalFooter>
<Button
size="lg"
onClick={ onDeleteClick }
isLoading={ isPending }
// FIXME: chackra's button is disabled when isLoading
isDisabled={ false }
>
Delete
</Button>
</ModalFooter>
</ModalContent>
</Modal>
);
};
export default DeleteModal;