import { ChangeEvent, useEffect, useRef } from 'react'; import { useForm } from 'react-hook-form'; import { Button, Input, Textarea } from '@chakra-ui/react'; import { IoDownloadOutline } from '@react-icons/all-files/io5/IoDownloadOutline'; import { IoTrash } from '@react-icons/all-files/io5/IoTrash'; import { type ProjectData } from 'ontime-types'; import { projectLogoPath } from '../../../../common/api/constants'; import { postProjectData, uploadProjectLogo } from '../../../../common/api/project'; import { maybeAxiosError } from '../../../../common/api/utils'; import useProjectData from '../../../../common/hooks-query/useProjectData'; import { validateLogo } from '../../../../common/utils/uploadUtils'; import { documentationUrl, websiteUrl } from '../../../../externals'; import * as Panel from '../../panel-utils/PanelUtils'; import style from './ProjectPanel.module.scss'; export default function ProjectData() { const { data, status, refetch } = useProjectData(); const { handleSubmit, register, reset, formState: { isSubmitting, isValid, isDirty, errors }, setError, watch, setValue, } = useForm({ defaultValues: data, values: data, resetOptions: { keepDirtyValues: true, }, }); // reset form values if data changes useEffect(() => { if (data) { reset(data); } }, [data, reset]); const handleUploadProjectLogo = async (event: ChangeEvent) => { const file = event.target.files?.[0]; if (!file) { return; } try { validateLogo(file); const response = await uploadProjectLogo(file); setValue('projectLogo', response.data.logoFilename, { shouldDirty: true, }); } catch (error) { const message = maybeAxiosError(error); setError('projectLogo', { message }); } }; const { ref, ...projectLogoRest } = register('projectLogo'); const uploadInputRef = useRef(null); const handleClickUpload = () => { uploadInputRef.current?.click(); }; const handleDeleteLogo = (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); setValue('projectLogo', null, { shouldDirty: true, }); }; const onSubmit = async (formData: ProjectData) => { try { await postProjectData(formData); } catch (error) { const message = maybeAxiosError(error); setError('root', { message }); } finally { await refetch(); } }; // populate with new data if we get an update const onReset = () => { reset(data); }; const isLoading = status === 'pending'; return ( Project data