feat: validate webhook URL (#4144)

* feat: validate webhook URL

* fix: use existing util method

* Add return statement

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
rostaklein
2024-02-25 09:33:38 +01:00
committed by GitHub
parent a9b0f88521
commit 52b33b5450

View File

@ -13,6 +13,7 @@ import { TextInput } from '@/ui/input/components/TextInput';
import { SubMenuTopBarContainer } from '@/ui/layout/page/SubMenuTopBarContainer'; import { SubMenuTopBarContainer } from '@/ui/layout/page/SubMenuTopBarContainer';
import { Section } from '@/ui/layout/section/components/Section'; import { Section } from '@/ui/layout/section/components/Section';
import { Breadcrumb } from '@/ui/navigation/bread-crumb/components/Breadcrumb'; import { Breadcrumb } from '@/ui/navigation/bread-crumb/components/Breadcrumb';
import { isURL } from '~/utils/is-url';
export const SettingsDevelopersWebhooksNew = () => { export const SettingsDevelopersWebhooksNew = () => {
const navigate = useNavigate(); const navigate = useNavigate();
@ -23,11 +24,20 @@ export const SettingsDevelopersWebhooksNew = () => {
targetUrl: '', targetUrl: '',
operation: '*.*', operation: '*.*',
}); });
const [errorMessage, setErrorMessage] = useState<string | undefined>();
const { createOneRecord: createOneWebhook } = useCreateOneRecord<Webhook>({ const { createOneRecord: createOneWebhook } = useCreateOneRecord<Webhook>({
objectNameSingular: CoreObjectNameSingular.Webhook, objectNameSingular: CoreObjectNameSingular.Webhook,
}); });
const handleSave = async () => { const handleSave = async () => {
setErrorMessage(undefined);
if (!isURL(formValues.targetUrl)) {
setErrorMessage('Invalid webhook URL');
return;
}
const newWebhook = await createOneWebhook?.(formValues); const newWebhook = await createOneWebhook?.(formValues);
if (!newWebhook) { if (!newWebhook) {
return; return;
} }
@ -71,6 +81,7 @@ export const SettingsDevelopersWebhooksNew = () => {
targetUrl: value, targetUrl: value,
})); }));
}} }}
error={errorMessage}
fullWidth fullWidth
/> />
</Section> </Section>