From 124e0bca0d27e83733c97b82bf32550d58c53511 Mon Sep 17 00:00:00 2001 From: Xiao Yijun Date: Fri, 12 May 2023 11:58:50 +0800 Subject: [PATCH] feat(console): support editing webhook custom headers in the console (#3823) --- .../CustomHeaderField/index.module.scss | 25 ++++ .../components/CustomHeaderField/index.tsx | 121 ++++++++++++++++++ .../WebhookDetails/WebhookSettings/index.tsx | 7 +- .../console/src/pages/WebhookDetails/types.ts | 9 +- .../console/src/pages/WebhookDetails/utils.ts | 15 ++- .../admin-console/webhook-details.ts | 3 + .../admin-console/webhook-details.ts | 3 + .../admin-console/webhook-details.ts | 3 + .../admin-console/webhook-details.ts | 3 + .../admin-console/webhook-details.ts | 3 + .../admin-console/webhook-details.ts | 3 + .../admin-console/webhook-details.ts | 3 + .../admin-console/webhook-details.ts | 3 + .../admin-console/webhook-details.ts | 3 + .../admin-console/webhook-details.ts | 3 + .../admin-console/webhook-details.ts | 3 + .../admin-console/webhook-details.ts | 3 + .../admin-console/webhook-details.ts | 3 + .../admin-console/webhook-details.ts | 3 + .../admin-console/webhook-details.ts | 3 + 20 files changed, 217 insertions(+), 5 deletions(-) create mode 100644 packages/console/src/pages/WebhookDetails/WebhookSettings/components/CustomHeaderField/index.module.scss create mode 100644 packages/console/src/pages/WebhookDetails/WebhookSettings/components/CustomHeaderField/index.tsx diff --git a/packages/console/src/pages/WebhookDetails/WebhookSettings/components/CustomHeaderField/index.module.scss b/packages/console/src/pages/WebhookDetails/WebhookSettings/components/CustomHeaderField/index.module.scss new file mode 100644 index 000000000..47b9bee09 --- /dev/null +++ b/packages/console/src/pages/WebhookDetails/WebhookSettings/components/CustomHeaderField/index.module.scss @@ -0,0 +1,25 @@ +@use '@/scss/underscore' as _; + +.field { + margin-bottom: _.unit(3); + + .input { + display: flex; + gap: _.unit(2); + align-items: center; + + .keyInput { + flex: 1; + } + + .valueInput { + flex: 2; + } + } + + .error { + font: var(--font-body-2); + color: var(--color-error); + margin-top: _.unit(1); + } +} diff --git a/packages/console/src/pages/WebhookDetails/WebhookSettings/components/CustomHeaderField/index.tsx b/packages/console/src/pages/WebhookDetails/WebhookSettings/components/CustomHeaderField/index.tsx new file mode 100644 index 000000000..2bf9614ae --- /dev/null +++ b/packages/console/src/pages/WebhookDetails/WebhookSettings/components/CustomHeaderField/index.tsx @@ -0,0 +1,121 @@ +import { useFieldArray, useFormContext } from 'react-hook-form'; +import { useTranslation } from 'react-i18next'; + +import CirclePlus from '@/assets/images/circle-plus.svg'; +import Minus from '@/assets/images/minus.svg'; +import Button from '@/components/Button'; +import FormField from '@/components/FormField'; +import IconButton from '@/components/IconButton'; +import TextInput from '@/components/TextInput'; +import { type WebhookDetailsFormType } from '@/pages/WebhookDetails/types'; + +import * as styles from './index.module.scss'; + +function CustomHeaderField() { + const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); + const { + control, + register, + getValues, + trigger, + formState: { + errors: { headers: headerErrors }, + submitCount, + }, + } = useFormContext(); + + const { fields, remove, append } = useFieldArray({ + control, + name: 'headers', + }); + + const keyValidator = (key: string, index: number) => { + const headers = getValues('headers'); + if (!headers) { + return true; + } + + if (headers.filter(({ key: _key }) => _key === key).length > 1) { + return t('webhook_details.settings.key_duplicated_error'); + } + + const correspondValue = getValues(`headers.${index}.value`); + if (correspondValue) { + return Boolean(key) || t('webhook_details.settings.key_missing_error'); + } + + return true; + }; + + const revalidate = () => { + for (const [index, _] of fields.entries()) { + void trigger(`headers.${index}.key`); + if (submitCount > 0) { + void trigger(`headers.${index}.value`); + } + } + }; + + return ( + + {fields.map((header, index) => { + return ( +
+
+ keyValidator(key, index), + onChange: revalidate, + })} + /> + + getValues(`headers.${index}.key`) + ? Boolean(value) || t('webhook_details.settings.value_missing_error') + : true, + onChange: revalidate, + })} + /> + {fields.length > 1 && ( + { + remove(index); + }} + > + + + )} +
+ {headerErrors?.[index]?.key?.message && ( +
{headerErrors[index]?.key?.message}
+ )} + {headerErrors?.[index]?.value?.message && ( +
{headerErrors[index]?.value?.message}
+ )} +
+ ); + })} +