import { Controller, useFieldArray, useForm } from 'react-hook-form'; import { Button, IconButton, Input, Select, Switch } from '@chakra-ui/react'; import { IoAdd } from '@react-icons/all-files/io5/IoAdd'; import { IoTrash } from '@react-icons/all-files/io5/IoTrash'; import { HttpSettings } from 'ontime-types'; import { generateId } from 'ontime-utils'; import { maybeAxiosError } from '../../../../common/api/utils'; import { useHttpSettings, usePostHttpSettings } from '../../../../common/hooks-query/useHttpSettings'; import { isKeyEscape } from '../../../../common/utils/keyEvent'; import { startsWithHttp } from '../../../../common/utils/regex'; import * as Panel from '../../panel-utils/PanelUtils'; import { cycles } from './integrationUtils'; import style from './IntegrationsPanel.module.css'; export default function HttpIntegrations() { const { data, status } = useHttpSettings(); const { mutateAsync } = usePostHttpSettings(); const { control, handleSubmit, reset, register, setError, formState: { errors, isSubmitting, isDirty, isValid }, } = useForm({ mode: 'onChange', defaultValues: data, values: data, resetOptions: { keepDirtyValues: true, }, }); const { fields, prepend, remove } = useFieldArray({ name: 'subscriptions', control, }); const onSubmit = async (values: HttpSettings) => { try { await mutateAsync(values); } catch (error) { setError('root', { message: maybeAxiosError(error) }); } }; const preventEscape = (event: React.KeyboardEvent) => { if (isKeyEscape(event)) { event.preventDefault(); event.stopPropagation(); } }; const handleAddNewSubscription = () => { prepend({ id: generateId(), cycle: 'onLoad', message: '', enabled: true, }); }; const handleDeleteSubscription = (index: number) => { remove(index); }; const canSubmit = !isSubmitting && isDirty && isValid; const isLoading = status === 'pending'; return ( HTTP settings
{errors?.root && {errors.root.message}} ( )} /> HTTP Integration {fields.length > 0 && ( Enabled Cycle Message {fields.map((integration, index) => { // @ts-expect-error -- not sure why it is not finding the type, it is ok const maybeError = errors.subscriptions?.[index]?.message?.message; return ( {maybeError && {maybeError}} } aria-label='Delete entry' onClick={() => handleDeleteSubscription(index)} /> ); })} )}
); }