import { useState, type FC } from 'react'; import { AlertDialog, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from "@/components/ui/alert-dialog" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import * as Yup from 'yup' import { yupResolver } from '@hookform/resolvers/yup'; import { useForm } from 'react-hook-form'; import { toast } from 'sonner'; import { Button } from '@/components/ui/button'; import { CheckCircle, Loader2 } from 'lucide-react'; import { Input } from '@/components/ui/input'; import { serverApiCall } from '@/lib/server-api'; import { Textarea } from '@/components/ui/textarea'; import { LazyImage } from '@/components/ui/lazy-image'; import { useFetchWithAxios } from '@/lib/axios/useFetch'; import { ComboboxCustomUI } from '@/components/ui/combobox-custom-ui'; import { IClient, IEndUser } from '@/app/(dashboard)/task/types'; import { DatePicker } from '@/components/ui/datepicker'; import { IProjectAssignment } from '@/app/(dashboard)/task/type.assignment'; const validationSchema = Yup.object().shape({ client_id: Yup.number().required(), end_user_id: Yup.number().required(), name: Yup.string().required(), code: Yup.string().required('Project code is required'), year: Yup.string().required('Year is required'), start_date: Yup.date().required('Start date is required'), end_date: Yup.date().nullable().min(Yup.ref('start_date'), 'End date cannot be before start date').default(null), description: Yup.string().optional().default(''), }); type FormDataType = Yup.InferType; type Props = { data?: IProjectAssignment onCancel?: VoidFunction onSuccess?: VoidFunction; }; const FormProject: FC = ({ data, onSuccess, onCancel }) => { const [isLoading, setIsLoading] = useState(false); const { data: clientList } = useFetchWithAxios('/client') const { data: endUserList } = useFetchWithAxios('/end-user') const form = useForm({ resolver: yupResolver(validationSchema), defaultValues: { "name": data?.project?.name || '', client_id: data?.client.id || undefined, end_user_id: data?.end_user.id || undefined, code: data?.project?.code || '', year: data?.project?.year || '', start_date: data?.project?.start_date ? new Date(data.project.start_date) : undefined, end_date: data?.project?.end_date ? new Date(data.project.end_date) : null, 'description': data?.project?.description || '', }, }); const onSubmit = async (payload: FormDataType) => { setIsLoading(true); let res: any if (data) { res = await serverApiCall('PUT', `/project/${data.id}`, payload) } else { res = await serverApiCall('POST', '/project', payload) } setIsLoading(false) if (res.success) { toast.success('Project saved successfully') onSuccess!() } else { toast.error(`Fail to save project`, { description: `(${res.code || '-'}) ${res.message}` }) } }; return ( <> Project Information Enter the basic details of the Project.
( Project Name * )} /> ( Client 0 ? clientList.map((item: IClient) => ({ label: item.name, value: item.id, originalData: item })) : []} value={field.value} onChange={field.onChange} placeholder='Select client' className='shrink' // --- CUSTOM DROPDOWN OPTIONS --- renderOption={(option, isSelected) => { const item = option.originalData as IClient; return (
{item.name} {item.description}
{isSelected && }
); }} />
)} /> ( End User 0 ? endUserList.map((item: IEndUser) => ({ label: item.name, value: item.id, originalData: item })) : []} value={field.value} onChange={field.onChange} placeholder='Select end user' className='shrink' // --- CUSTOM DROPDOWN OPTIONS --- renderOption={(option, isSelected) => { const item = option.originalData as IEndUser; return (
{item.name} {item.description}
{isSelected && }
); }} />
)} />
( Code )} /> ( Year )} />
( Start Date )} /> ( End Date )} />
( Description