mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 20:36:29 +00:00
7b1541c567
Rename ManageEmployee to AddTeam and introduce a new ManageTeam component for listing/editing assignment team members. Update imports to use AddTeam and wire AddTeam into WorkOrder/Client pages. Enhance project assignment form payload to include end_user_id. Improve filtering by adding new props to FilterComponent/FilterModal (hideClient, hideEndUser, hideProjectDateFilter, hideWoDateFilter, hideImDateFilter, hideFilterModal) and implement client/end-user multi-selects and per-category date pickers. Adjust FilterModal layout widths and initial/clear/apply logic to set URL params for client/end_user. UX and API tweaks: switch user fetch to /user/active, change status endpoint to /project-assignment-status, replace multi-select with a Combobox in AddTeam, update textarea and popover sizing classes, and refine toast messages. Add ManageTeam modal with add/edit/delete flows and confirm-delete integration.
149 lines
7.5 KiB
TypeScript
149 lines
7.5 KiB
TypeScript
"use client"
|
|
|
|
import { useMemo, useState, type FC } from 'react';
|
|
import {
|
|
AlertDialog, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle
|
|
} from "@/components/ui/alert-dialog"
|
|
import { Button } from '@/components/ui/button';
|
|
import { CheckCircle, Loader2 } from 'lucide-react';
|
|
import { useFetchWithAxios } from '@/lib/axios/useFetch';
|
|
import { IUser } from '@/app/admin/administration/user/component/type';
|
|
import { LazyImage } from '@/components/ui/lazy-image';
|
|
import { Skeleton } from '@/components/ui/skeleton';
|
|
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 { serverApiCall } from '@/lib/server-api';
|
|
import { toast } from 'sonner';
|
|
import { Combobox } from '@/components/ui/combobox-custom-ui';
|
|
|
|
type Props = {
|
|
assignmentId: number;
|
|
existingTeam?: number[];
|
|
onCancel?: VoidFunction
|
|
onSuccess?: VoidFunction;
|
|
};
|
|
|
|
const validationSchema = Yup.object().shape({
|
|
user: Yup.number().required('Please select at least one user'),
|
|
});
|
|
|
|
type FormDataType = Yup.InferType<typeof validationSchema>;
|
|
|
|
|
|
const AddTeam: FC<Props> = ({ assignmentId, existingTeam, onCancel, onSuccess }) => {
|
|
const [isLoading, setIsLoading] = useState(false)
|
|
|
|
const { data: userList, isLoading: isLoadingFetchUser } = useFetchWithAxios('/user/active')
|
|
|
|
const form = useForm<FormDataType>({
|
|
resolver: yupResolver(validationSchema),
|
|
defaultValues: {},
|
|
});
|
|
|
|
const onSubmit = async (payload: FormDataType) => {
|
|
setIsLoading(true);
|
|
|
|
const submitPayload = {
|
|
data: [{
|
|
"project_assignment_id": assignmentId,
|
|
"user_id": payload.user,
|
|
"start_date": '',
|
|
"end_date": ''
|
|
}]
|
|
}
|
|
|
|
console.log(submitPayload)
|
|
const res = await serverApiCall('POST', '/project-assignment/assign-user', submitPayload)
|
|
|
|
setIsLoading(false)
|
|
if (res.success) {
|
|
toast.success('New team added successfully')
|
|
onSuccess!()
|
|
} else {
|
|
toast.error(`Fail to save team`, { description: `(${res.code || '-'}) ${res.message}` })
|
|
}
|
|
};
|
|
|
|
const mappedUsers = useMemo(() => {
|
|
return userList?.user?.map((item: IUser) => ({
|
|
label: item.fullname,
|
|
value: item.id,
|
|
originalData: item
|
|
})) || []
|
|
}, [userList])
|
|
|
|
|
|
return (
|
|
<>
|
|
<AlertDialog open={true}>
|
|
<AlertDialogContent className='max-h-[90vh] overflow-y-auto overflow-x-hidden'>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>Add Team</AlertDialogTitle>
|
|
<AlertDialogDescription>
|
|
Assign new employees for project assignment.
|
|
</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
<div className="grid gap-4 py-4 max-h-100 overflow-y-auto">
|
|
{isLoadingFetchUser ?
|
|
<div className='space-y-2'>
|
|
<Skeleton className='h-10 w-full' />
|
|
<Skeleton className='h-10 w-full' />
|
|
<Skeleton className='h-10 w-full' />
|
|
</div>
|
|
:
|
|
<Form {...form}>
|
|
<form onSubmit={form.handleSubmit(onSubmit)}>
|
|
<div className="grid grid-cols-1 gap-4 mb-6">
|
|
<FormField control={form.control} name='user'
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Employee to Assign <span className='text-red-600 font-bold'>*</span></FormLabel>
|
|
<FormControl>
|
|
<Combobox options={mappedUsers} value={field.value} onChange={field.onChange} placeholder='Select user to assign'
|
|
// --- CUSTOM DROPDOWN OPTIONS ---
|
|
renderOption={(option, isSelected) => {
|
|
const user = option.originalData as IUser;
|
|
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-full border border-border' alt={user.fullname} src={user.avatar ? `${process.env.BASE_URL_IMG}/${user.avatar}` : '/assets/no-avatar.jpg'} />
|
|
<div className="flex flex-col">
|
|
<span className={`text-sm ${isSelected ? 'font-bold text-primary' : ''}`}>
|
|
{user.fullname}
|
|
</span>
|
|
<span className="text-xs text-muted-foreground">
|
|
{user.position?.name}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
{isSelected && <CheckCircle className="h-5 w-5 -translate-x-2" />}
|
|
</div>
|
|
);
|
|
}}
|
|
/>
|
|
</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 Changes
|
|
</Button>
|
|
</AlertDialogFooter>
|
|
</form>
|
|
</Form>
|
|
}
|
|
</div>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default AddTeam; |