'use client'; import { zodResolver } from '@hookform/resolvers/zod'; import { Button } from '@workspace/ui/components/button'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@workspace/ui/components/form'; import { RadioGroup, RadioGroupItem } from '@workspace/ui/components/radio-group'; import { ScrollArea } from '@workspace/ui/components/scroll-area'; import { Sheet, SheetContent, SheetFooter, SheetHeader, SheetTitle, SheetTrigger, } from '@workspace/ui/components/sheet'; import { EnhancedInput } from '@workspace/ui/custom-components/enhanced-input'; import { Icon } from '@workspace/ui/custom-components/icon'; import { useTranslations } from 'next-intl'; import { useEffect, useState } from 'react'; import { useForm } from 'react-hook-form'; import { z } from 'zod'; const formSchema = z.object({ title: z.string(), type: z.enum(['image', 'video']), content: z.string(), description: z.string(), target_url: z.string().url(), start_time: z.number(), end_time: z.number(), }); interface AdsFormProps { onSubmit: (data: T) => Promise | boolean; initialValues?: T; loading?: boolean; trigger: string; title: string; } export default function AdsForm>({ onSubmit, initialValues, loading, trigger, title, }: AdsFormProps) { const t = useTranslations('ads'); const [open, setOpen] = useState(false); const form = useForm({ resolver: zodResolver(formSchema), defaultValues: { ...initialValues, } as any, }); useEffect(() => { form?.reset(initialValues); }, [form, initialValues]); const type = form.watch('type'); const startTime = form.watch('start_time'); const renderContentField = () => { return ( ( {t('form.content')} { form.setValue('content', value); }} /> )} /> ); }; async function handleSubmit(data: { [x: string]: any }) { const bool = await onSubmit(data as T); if (bool) setOpen(false); } return ( {title}
( {t('form.title')} { form.setValue(field.name, value); }} /> )} /> ( {t('form.type')} { form.setValue(field.name, value); }} className='flex gap-4' > {t('form.typeImage')} {t('form.typeVideo')} )} /> {renderContentField()} ( {t('form.description')} { form.setValue(field.name, value); }} /> )} /> ( {t('form.targetUrl')} { form.setValue(field.name, value); }} /> )} /> ( {t('form.startTime')} { const timestamp = value ? new Date(value).getTime() : 0; form.setValue(field.name, timestamp); const endTime = form.getValues('end_time'); if (endTime && timestamp > endTime) { form.setValue('end_time', ''); } }} /> )} /> { return ( {t('form.endTime')} { const timestamp = value ? new Date(value).getTime() : 0; if (!startTime || timestamp < startTime) return; form.setValue(field.name, timestamp); }} /> ); }} />
); }