update: webhooks

This commit is contained in:
anik-ghosh-au7 2022-07-15 12:20:51 +05:30
parent 8e655daa71
commit d837b1590a

View File

@ -21,7 +21,12 @@ import {
import { FaMinusCircle, FaPlus } from 'react-icons/fa'; import { FaMinusCircle, FaPlus } from 'react-icons/fa';
import { useClient } from 'urql'; import { useClient } from 'urql';
import { ArrayInputOperations } from '../constants'; import { ArrayInputOperations } from '../constants';
import { validateEventName, validateURI } from '../utils'; import {
capitalizeFirstLetter,
validateEventName,
validateURI,
} from '../utils';
import { AddWebhook } from '../graphql/mutation';
enum INPUT_FIELDS { enum INPUT_FIELDS {
EVENT_NAME = 'event_name', EVENT_NAME = 'event_name',
@ -144,7 +149,7 @@ const AddWebhookModal = () => {
break; break;
} }
}; };
const updateHeadersHandler = (operation: string, index: number = 0) => { const updateHeaders = (operation: string, index: number = 0) => {
switch (operation) { switch (operation) {
case ArrayInputOperations.APPEND: case ArrayInputOperations.APPEND:
setWebhook({ setWebhook({
@ -184,6 +189,52 @@ const AddWebhookModal = () => {
break; break;
} }
}; };
const validateData = () => {
return (
!loading &&
webhook[INPUT_FIELDS.EVENT_NAME].length > 0 &&
webhook[INPUT_FIELDS.ENDPOINT].length > 0 &&
validator[INPUT_FIELDS.EVENT_NAME] &&
validator[INPUT_FIELDS.ENDPOINT] &&
!validator[INPUT_FIELDS.HEADERS].some(
(headerData: headersValidatorDataType) =>
!headerData.key || !headerData.value
)
);
};
const saveData = async () => {
if (!validateData()) return;
let params: any = {
[INPUT_FIELDS.EVENT_NAME]: webhook[INPUT_FIELDS.EVENT_NAME],
[INPUT_FIELDS.ENDPOINT]: webhook[INPUT_FIELDS.ENDPOINT],
[INPUT_FIELDS.ENABLED]: webhook[INPUT_FIELDS.ENABLED],
};
if (webhook[INPUT_FIELDS.HEADERS].length > 0) {
const headers = webhook[INPUT_FIELDS.HEADERS].reduce((acc, data) => {
return { ...acc, [data.key]: data.value };
}, {});
params[INPUT_FIELDS.HEADERS] = headers;
}
const res = await client.mutation(AddWebhook, { params }).toPromise();
if (res.error) {
toast({
title: capitalizeFirstLetter(res.error.message),
isClosable: true,
status: 'error',
position: 'bottom-right',
});
return;
} else if (res.data?._add_webhook) {
toast({
title: capitalizeFirstLetter(res.data?._add_webhook.message),
isClosable: true,
status: 'success',
position: 'bottom-right',
});
setWebhook({ ...initWebhookData });
onClose();
}
};
return ( return (
<> <>
<Button <Button
@ -300,9 +351,7 @@ const AddWebhookModal = () => {
size="sm" size="sm"
variant="ghost" variant="ghost"
paddingRight="0" paddingRight="0"
onClick={() => onClick={() => updateHeaders(ArrayInputOperations.APPEND)}
updateHeadersHandler(ArrayInputOperations.APPEND)
}
> >
Add more Headers Add more Headers
</Button> </Button>
@ -365,10 +414,7 @@ const AddWebhookModal = () => {
variant="ghost" variant="ghost"
padding="0" padding="0"
onClick={() => onClick={() =>
updateHeadersHandler( updateHeaders(ArrayInputOperations.REMOVE, index)
ArrayInputOperations.REMOVE,
index
)
} }
> >
<FaMinusCircle /> <FaMinusCircle />
@ -384,8 +430,8 @@ const AddWebhookModal = () => {
<Button <Button
colorScheme="blue" colorScheme="blue"
variant="solid" variant="solid"
onClick={() => {}} onClick={saveData}
isDisabled={loading} isDisabled={!validateData()}
> >
<Center h="100%" pt="5%"> <Center h="100%" pt="5%">
Save Save