forked from rajun/ar-test
75 lines
3.9 KiB
TypeScript
75 lines
3.9 KiB
TypeScript
import { FormProvider, useForm, useWatch } from 'react-hook-form';
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
import { useFetcher } from 'react-router';
|
|
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '~/components/ui/dialog';
|
|
import { DialogActions } from '~/components/ui/dialog-actions';
|
|
import { ProjectFormFields } from '~/components/projects/form/project-form-fields';
|
|
import { ProjectPreview } from '~/components/projects/form/project-preview';
|
|
import { PROJECT_INTENT, type ProjectIntent } from '~/data/projects/constants';
|
|
import { ROUTES } from '~/data/routes';
|
|
import { useActionToast } from '~/hooks/use-action-toast';
|
|
import { useAdminData } from '~/hooks/admin/use-admin-data';
|
|
import { useProjectFormBrand } from '~/hooks/projects/use-project-form-brand';
|
|
import { toProjectFormData } from '~/lib/projects/form-data';
|
|
import { toProjectFormValues, withLogoRule, type ProjectFormValues } from '~/schemas/projects/project-form';
|
|
import type { ActionResult } from '~/types/action';
|
|
import type { Project } from '~/types/project';
|
|
|
|
interface ProjectFormDialogProps {
|
|
open: boolean;
|
|
project: Project | null;
|
|
onOpenChange: (open: boolean) => void;
|
|
onCreated: (id: string) => void;
|
|
}
|
|
|
|
export function ProjectFormDialog({ open, project, onOpenChange, onCreated }: ProjectFormDialogProps) {
|
|
const { settings } = useAdminData();
|
|
const fetcher = useFetcher<ActionResult<ProjectIntent>>();
|
|
const form = useForm<ProjectFormValues>({ resolver: zodResolver(withLogoRule(settings.logo_required)), defaultValues: toProjectFormValues(project) });
|
|
const { brand, button, layout } = useProjectFormBrand(form.control, project);
|
|
const [name, logo] = useWatch({ control: form.control, name: ['name', 'logo'] });
|
|
const canSave = !!name.trim() && (!settings.logo_required || !!logo.file || !!logo.existing);
|
|
const intent = project ? PROJECT_INTENT.update : PROJECT_INTENT.create;
|
|
|
|
useActionToast(fetcher.data, (result) => {
|
|
onOpenChange(false);
|
|
if (result.intent === PROJECT_INTENT.create && result.id) onCreated(result.id);
|
|
});
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className='max-w-[920px]'>
|
|
<DialogHeader>
|
|
<DialogTitle>{project ? 'Edit project' : 'New project'}</DialogTitle>
|
|
<DialogDescription>A project is a client company. Its logo appears on every AR page in it.</DialogDescription>
|
|
</DialogHeader>
|
|
<FormProvider {...form}>
|
|
<form
|
|
noValidate
|
|
className='flex min-h-0 flex-1 flex-col'
|
|
onSubmit={form.handleSubmit((values) =>
|
|
fetcher.submit(toProjectFormData(intent, project?.id, values), {
|
|
method: 'post',
|
|
action: ROUTES.projects,
|
|
encType: 'multipart/form-data',
|
|
}),
|
|
)}
|
|
>
|
|
{/* Only the form column scrolls; the title, preview and buttons stay in place */}
|
|
<div className='grid min-h-0 flex-1 grid-rows-[minmax(0,1fr)] md:grid-cols-[minmax(300px,1fr)_auto]'>
|
|
<ProjectFormFields isEdit={!!project} brand={brand} />
|
|
<ProjectPreview brand={brand} button={button} layout={layout} />
|
|
</div>
|
|
<DialogActions
|
|
confirmLabel={project ? 'Save changes' : 'Create project'}
|
|
pendingLabel='Saving…'
|
|
pending={fetcher.state !== 'idle'}
|
|
disabled={!canSave}
|
|
/>
|
|
</form>
|
|
</FormProvider>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|