feat: add asset form dialog
This commit is contained in:
parent
d795140b1b
commit
e6483968a1
79
app/components/assets/form/asset-form-dialog.tsx
Normal file
79
app/components/assets/form/asset-form-dialog.tsx
Normal file
@ -0,0 +1,79 @@
|
||||
import { FormProvider, useForm } from 'react-hook-form';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { useFetcher } from 'react-router';
|
||||
import { Dialog, DialogBody, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '~/components/ui/dialog';
|
||||
import { DialogActions } from '~/components/ui/dialog-actions';
|
||||
import { Field } from '~/components/ui/field';
|
||||
import { Input } from '~/components/ui/input';
|
||||
import { BrandedQrField } from '~/components/assets/form/branded-qr-field';
|
||||
import { ModelSourceField } from '~/components/assets/form/model-source-field';
|
||||
import { ProjectSelectField } from '~/components/assets/form/project-select-field';
|
||||
import { ASSET_INTENT, type AssetIntent } from '~/data/assets/constants';
|
||||
import { ROUTES } from '~/data/routes';
|
||||
import { useActionToast } from '~/hooks/use-action-toast';
|
||||
import { useAdminData } from '~/hooks/admin/use-admin-data';
|
||||
import { toAssetFormData } from '~/lib/assets/form-data';
|
||||
import { assetFormSchema, toAssetFormValues, type AssetFormValues } from '~/schemas/assets/asset-form';
|
||||
import type { ActionResult } from '~/types/action';
|
||||
import type { ArAsset } from '~/types/asset';
|
||||
|
||||
interface AssetFormDialogProps {
|
||||
open: boolean;
|
||||
asset: ArAsset | null;
|
||||
projectId?: string;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
}
|
||||
|
||||
export function AssetFormDialog({ open, asset, projectId, onOpenChange }: AssetFormDialogProps) {
|
||||
const { settings } = useAdminData();
|
||||
const fetcher = useFetcher<ActionResult<AssetIntent>>();
|
||||
const form = useForm<AssetFormValues>({
|
||||
resolver: zodResolver(assetFormSchema),
|
||||
defaultValues: toAssetFormValues(asset, { projectId, brandedQr: settings.branded_qr_default }),
|
||||
});
|
||||
const { errors } = form.formState;
|
||||
const intent = asset ? ASSET_INTENT.update : ASSET_INTENT.create;
|
||||
|
||||
useActionToast(fetcher.data, () => onOpenChange(false));
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className='max-w-xl'>
|
||||
<DialogHeader>
|
||||
<DialogTitle>{asset ? 'Edit asset' : 'New asset'}</DialogTitle>
|
||||
<DialogDescription>{asset ? asset.name : 'Add a 3D model and get a QR code for it.'}</DialogDescription>
|
||||
</DialogHeader>
|
||||
<FormProvider {...form}>
|
||||
<form
|
||||
noValidate
|
||||
className='flex flex-col'
|
||||
onSubmit={form.handleSubmit((values) =>
|
||||
fetcher.submit(toAssetFormData(intent, values), { method: 'post', action: ROUTES.assets, encType: 'multipart/form-data' }),
|
||||
)}
|
||||
>
|
||||
<DialogBody>
|
||||
<Field label='Name' htmlFor='asset-name' required error={errors.name?.message}>
|
||||
<Input id='asset-name' placeholder='e.g. Lounge chair' aria-invalid={!!errors.name} {...form.register('name')} />
|
||||
</Field>
|
||||
<ProjectSelectField />
|
||||
<ModelSourceField name='glb' required />
|
||||
<ModelSourceField name='usdz' />
|
||||
<BrandedQrField />
|
||||
{!asset && (
|
||||
<Field
|
||||
label='Reuse QR code ID'
|
||||
htmlFor='asset-id'
|
||||
hint='Optional — paste the 15-character ID from an existing QR link.'
|
||||
error={errors.id?.message}
|
||||
>
|
||||
<Input id='asset-id' maxLength={15} placeholder='e.g. 0gsk2w8h236474u' aria-invalid={!!errors.id} {...form.register('id')} />
|
||||
</Field>
|
||||
)}
|
||||
</DialogBody>
|
||||
<DialogActions confirmLabel={asset ? 'Save changes' : 'Create asset'} pendingLabel='Saving…' pending={fetcher.state !== 'idle'} />
|
||||
</form>
|
||||
</FormProvider>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user