|
| 1 | +import React from 'react'; |
| 2 | +import { |
| 3 | + Button, |
| 4 | + Center, |
| 5 | + Flex, |
| 6 | + MenuItem, |
| 7 | + Modal, |
| 8 | + ModalBody, |
| 9 | + ModalCloseButton, |
| 10 | + ModalContent, |
| 11 | + ModalFooter, |
| 12 | + ModalHeader, |
| 13 | + ModalOverlay, |
| 14 | + useDisclosure, |
| 15 | + Text, |
| 16 | + useToast, |
| 17 | +} from '@chakra-ui/react'; |
| 18 | +import { useClient } from 'urql'; |
| 19 | +import { FaRegTrashAlt } from 'react-icons/fa'; |
| 20 | +import { DeleteWebhook } from '../graphql/mutation'; |
| 21 | +import { capitalizeFirstLetter } from '../utils'; |
| 22 | + |
| 23 | +interface deleteWebhookModalInputPropTypes { |
| 24 | + webhookId: string; |
| 25 | + eventName: string; |
| 26 | + fetchWebookData: Function; |
| 27 | +} |
| 28 | + |
| 29 | +const DeleteWebhookModal = ({ |
| 30 | + webhookId, |
| 31 | + eventName, |
| 32 | + fetchWebookData, |
| 33 | +}: deleteWebhookModalInputPropTypes) => { |
| 34 | + const client = useClient(); |
| 35 | + const toast = useToast(); |
| 36 | + const { isOpen, onOpen, onClose } = useDisclosure(); |
| 37 | + |
| 38 | + const deleteHandler = async () => { |
| 39 | + const res = await client |
| 40 | + .mutation(DeleteWebhook, { params: { id: webhookId } }) |
| 41 | + .toPromise(); |
| 42 | + if (res.error) { |
| 43 | + toast({ |
| 44 | + title: capitalizeFirstLetter(res.error.message), |
| 45 | + isClosable: true, |
| 46 | + status: 'error', |
| 47 | + position: 'bottom-right', |
| 48 | + }); |
| 49 | + |
| 50 | + return; |
| 51 | + } else if (res.data?._delete_webhook) { |
| 52 | + toast({ |
| 53 | + title: capitalizeFirstLetter(res.data?._delete_webhook.message), |
| 54 | + isClosable: true, |
| 55 | + status: 'success', |
| 56 | + position: 'bottom-right', |
| 57 | + }); |
| 58 | + } |
| 59 | + onClose(); |
| 60 | + fetchWebookData(); |
| 61 | + }; |
| 62 | + return ( |
| 63 | + <> |
| 64 | + <MenuItem onClick={onOpen}>Delete</MenuItem> |
| 65 | + <Modal isOpen={isOpen} onClose={onClose}> |
| 66 | + <ModalOverlay /> |
| 67 | + <ModalContent> |
| 68 | + <ModalHeader>Delete Webhook</ModalHeader> |
| 69 | + <ModalCloseButton /> |
| 70 | + <ModalBody> |
| 71 | + <Text fontSize="md">Are you sure?</Text> |
| 72 | + <Flex |
| 73 | + padding="5%" |
| 74 | + marginTop="5%" |
| 75 | + marginBottom="2%" |
| 76 | + border="1px solid #ff7875" |
| 77 | + borderRadius="5px" |
| 78 | + flexDirection="column" |
| 79 | + > |
| 80 | + <Text fontSize="sm"> |
| 81 | + Webhook for event <b>{eventName}</b> will be deleted |
| 82 | + permanently! |
| 83 | + </Text> |
| 84 | + </Flex> |
| 85 | + </ModalBody> |
| 86 | + |
| 87 | + <ModalFooter> |
| 88 | + <Button |
| 89 | + leftIcon={<FaRegTrashAlt />} |
| 90 | + colorScheme="red" |
| 91 | + variant="solid" |
| 92 | + onClick={deleteHandler} |
| 93 | + isDisabled={false} |
| 94 | + > |
| 95 | + <Center h="100%" pt="5%"> |
| 96 | + Delete |
| 97 | + </Center> |
| 98 | + </Button> |
| 99 | + </ModalFooter> |
| 100 | + </ModalContent> |
| 101 | + </Modal> |
| 102 | + </> |
| 103 | + ); |
| 104 | +}; |
| 105 | + |
| 106 | +export default DeleteWebhookModal; |
0 commit comments