mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 11:46:27 +00:00
3eabe762bf
Add new admin Project Assignment area with Client, End User, and Project Management pages and components (tables, Export buttons, lazy-loaded pages). Introduce FormProject for creating/editing projects and export utilities to download Excel for each module. Update task types: IEndUser now includes code and a status object. Clean up FormClient by removing unused imports and inline end-user modal usage. Make BadgeStatus robust to missing status values. Add "Project Assignment" entry to the sidebar and import FolderGit2.
231 lines
14 KiB
TypeScript
231 lines
14 KiB
TypeScript
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<typeof validationSchema>;
|
|
|
|
type Props = {
|
|
data?: IProjectAssignment
|
|
onCancel?: VoidFunction
|
|
onSuccess?: VoidFunction;
|
|
};
|
|
|
|
const FormProject: FC<Props> = ({ data, onSuccess, onCancel }) => {
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
const { data: clientList } = useFetchWithAxios('/client')
|
|
const { data: endUserList } = useFetchWithAxios('/end-user')
|
|
|
|
const form = useForm<FormDataType>({
|
|
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 (
|
|
<>
|
|
<AlertDialog open={true}>
|
|
<AlertDialogContent className='max-h-[90vh] overflow-y-auto'>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>Project Information</AlertDialogTitle>
|
|
<AlertDialogDescription>
|
|
Enter the basic details of the Project.
|
|
</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
<Form {...form}>
|
|
<form onSubmit={form.handleSubmit(onSubmit)}>
|
|
<div className="grid grid-cols-1 gap-4 mb-6">
|
|
<FormField control={form.control} name='name'
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Project Name <span className='text-red-600 font-bold'>*</span></FormLabel>
|
|
<FormControl>
|
|
<Input {...field} placeholder='e.g. PT ASDP Indonesia Ferry' />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField control={form.control} name='client_id'
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Client</FormLabel>
|
|
<FormControl>
|
|
<ComboboxCustomUI options={clientList && clientList.length > 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 (
|
|
<div className={`flex items-center justify-between w-full py-1 rounded-sm ${isSelected ? 'bg-zinc-800' : ''}`}>
|
|
<div className="flex items-center gap-3">
|
|
<LazyImage className='w-10 h-10 rounded-md border border-border' alt={item.name} src={item.photo ? item.photo : '/assets/ilustration.png'} />
|
|
<div className="flex flex-col">
|
|
<span className={`text-sm ${isSelected ? 'font-bold text-primary' : ''}`}>
|
|
{item.name}
|
|
</span>
|
|
<span className="text-xs text-muted-foreground truncate">
|
|
{item.description}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
{isSelected && <CheckCircle className="h-5 w-5 -translate-x-2" />}
|
|
</div>
|
|
);
|
|
}}
|
|
/>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField control={form.control} name='end_user_id'
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>End User</FormLabel>
|
|
<FormControl>
|
|
<ComboboxCustomUI options={endUserList && endUserList.length > 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 (
|
|
<div className={`flex items-center justify-between w-full py-1 rounded-sm ${isSelected ? 'bg-zinc-800' : ''}`}>
|
|
<div className="flex items-center gap-3">
|
|
<LazyImage className='w-10 h-10 rounded-md border border-border' alt={item.name} src={item.photo ? item.photo : '/assets/ilustration.png'} />
|
|
<div className="flex flex-col">
|
|
<span className={`text-sm ${isSelected ? 'font-bold text-primary' : ''}`}>
|
|
{item.name}
|
|
</span>
|
|
<span className="text-xs text-muted-foreground truncate">
|
|
{item.description}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
{isSelected && <CheckCircle className="h-5 w-5 -translate-x-2" />}
|
|
</div>
|
|
);
|
|
}}
|
|
/>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<div className="grid md:grid-cols-2 gap-4">
|
|
<FormField control={form.control} name="code" render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Code</FormLabel>
|
|
<FormControl><Input placeholder="e.g. 24016" {...field} /></FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)} />
|
|
<FormField control={form.control} name="year" render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Year</FormLabel>
|
|
<FormControl><Input placeholder="e.g. 2024" {...field} /></FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)} />
|
|
</div>
|
|
|
|
<div className="grid md:grid-cols-2 gap-4">
|
|
<FormField control={form.control} name="start_date" render={({ field }) => (
|
|
<FormItem className="flex flex-col">
|
|
<FormLabel>Start Date</FormLabel>
|
|
<DatePicker date={field.value} setDate={field.onChange} />
|
|
<FormMessage />
|
|
</FormItem>
|
|
)} />
|
|
<FormField control={form.control} name="end_date" render={({ field }) => (
|
|
<FormItem className="flex flex-col">
|
|
<FormLabel>End Date</FormLabel>
|
|
<DatePicker date={field.value as Date | undefined} setDate={field.onChange} />
|
|
<FormMessage />
|
|
</FormItem>
|
|
)} />
|
|
</div>
|
|
|
|
<FormField control={form.control} name="description" render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Description</FormLabel>
|
|
<FormControl>
|
|
<Textarea placeholder="Describe the scope..." className="min-h-30" {...field} value={field.value || ''} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)} />
|
|
</div>
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel onClick={onCancel} disabled={isLoading} type='button'>Cancel</AlertDialogCancel>
|
|
<Button type="submit" disabled={isLoading}>
|
|
{isLoading && <Loader2 className='animate-spin' />}
|
|
Save Project
|
|
</Button>
|
|
</AlertDialogFooter>
|
|
</form>
|
|
</Form>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default FormProject; |