import { useEffect, useState } from 'react'; import { useForm } from 'react-hook-form'; import type { Subscription } from 'ontime-types'; import { TimerLifeCycle } from 'ontime-types'; import useHttpSettings, { usePostHttpSubscriptions } from '../../../common/hooks-query/useHttpSettings'; import { useEmitLog } from '../../../common/stores/logger'; import ModalLoader from '../modal-loader/ModalLoader'; import OntimeModalFooter from '../OntimeModalFooter'; import SubscriptionRow from './SubscriptionRow'; import styles from '../Modal.module.scss'; type OntimeCycle = keyof typeof TimerLifeCycle; const sectionText: { [key in TimerLifeCycle]: { title: string; subtitle: string } } = { onLoad: { title: 'On Load', subtitle: 'Triggers when a timer is loaded', }, onStart: { title: 'On Start', subtitle: 'Triggers when a timer starts', }, onPause: { title: 'On Pause', subtitle: 'Triggers when a running timer is paused', }, onStop: { title: 'On Stop', subtitle: 'Triggers when a running timer is stopped', }, onUpdate: { title: 'On Every Second', subtitle: 'Triggers when a running timer is updated (at least once a second, can be more)', }, onFinish: { title: 'On Finish', subtitle: 'Triggers when a running reaches 0', }, }; export default function HttpIntegration() { const { data, isFetching } = useHttpSettings(); const { mutateAsync } = usePostHttpSubscriptions(); const { emitError } = useEmitLog(); const { control, handleSubmit, register, reset, formState: { isSubmitting, isDirty, isValid }, } = useForm({ defaultValues: data.subscriptions, values: data.subscriptions, resetOptions: { keepDirtyValues: true, }, }); const [showSection, setShowSection] = useState(TimerLifeCycle.onLoad); useEffect(() => { if (data) { reset(data.subscriptions); } }, [data, reset]); const resetForm = () => { reset(data.subscriptions); }; const onSubmit = async (values: Subscription) => { try { const subscriptions = { onLoad: values.onLoad ?? [], onStart: values.onStart ?? [], onPause: values.onPause ?? [], onStop: values.onStop ?? [], onUpdate: values.onUpdate ?? [], onFinish: values.onFinish ?? [], }; await mutateAsync(subscriptions); } catch (error) { emitError(`Error setting HTML: ${error}`); } }; if (isFetching) { return ; } const placeholder = 'http://x.x.x.x:xxxx/api/path'; //TODO: add golobal off return (
{/*
HTTP Output Ontime data feedback
*/} ); }