import { useState } from 'react'; import { useForm } from 'react-hook-form'; import { Button, Switch } from '@chakra-ui/react'; import { useQueryClient } from '@tanstack/react-query'; import { PROJECT_DATA } from '../../../../common/api/constants'; import { getDb, patchData } from '../../../../common/api/db'; import { maybeAxiosError } from '../../../../common/api/utils'; import { cx } from '../../../../common/utils/styleUtils'; import * as Panel from '../PanelUtils'; import { makeProjectPatch } from './project.utils'; import style from './ProjectPanel.module.scss'; interface ProjectMergeFromProps { onClose: () => void; fileName: string; } type ProjectMergeFormValues = { project: boolean; rundown: boolean; viewSettings: boolean; urlPresets: boolean; osc: boolean; http: boolean; }; export default function ProjectMergeForm(props: ProjectMergeFromProps) { const { onClose, fileName } = props; const [error, setError] = useState(null); const queryClient = useQueryClient(); const { handleSubmit, register, formState: { isSubmitting, isValid, isDirty }, } = useForm({ defaultValues: { project: false, rundown: false, viewSettings: false, urlPresets: false, osc: false, http: false, }, resetOptions: { keepDirtyValues: true, }, }); const handleSubmitCreate = async (values: ProjectMergeFormValues) => { const allFalse = Object.values(values).every((value) => !value); if (allFalse) { setError('At least one option must be selected'); return; } try { setError(null); // make patch object const { data } = await getDb(fileName); const patch = await makeProjectPatch(data, values); // request patch await patchData(patch); await queryClient.invalidateQueries({ queryKey: PROJECT_DATA }); onClose(); } catch (error) { setError(maybeAxiosError(error)); } }; return ( Merge {`"${fileName}"`}
{error && {error}} Select partial data from {`"${fileName}"`} to merge into the current project.
This process is irreversible.
); }