mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 11:06:29 +00:00
7892eda589
Refactor Assignment detail view and related team components to improve layout, UX and behaviour. Key changes:
- Added a backup of the previous AssignmentDetailComponent as AssignmentDetailComponent.old.tsx.
- Reworked AssignmentDetailComponent.tsx: simplified assignment fields, added LazyImage, formatDate helper, visual redesign (MetaTile, PartyCard), improved project context card and conditional badges/UI for IM vs Project, and minor status-list rendering tweaks.
- AssignedPeople.tsx: made team list collapsible with motion animations, added toggle button, defaultOpen prop, and preserved stacked avatars UI.
- page.tsx: adjusted project assignment fetch to use /project/{id} (commented previous query-based call).
- AddTeam.tsx: updated existingTeam type and filter logic to exclude already-assigned users when mapping candidates.
- ManageTeam.tsx: removed an unused import.
- UpdateTimelineStatus.tsx: removed some form libs, updated success/error toast messages and sort timeline rows before rendering.
- globals.css: added --type-memo and --type-wo CSS variables and minor variable tweaks.
These changes aim to improve clarity, consistency and interactivity of the assignment and team management UI.
153 lines
7.8 KiB
TypeScript
153 lines
7.8 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 { ComboboxCustomUI } from '@/components/ui/combobox-custom-ui';
|
|
import type { ProjectAssignmentWorkOrderDetail } from '../type.assignment';
|
|
|
|
type Props = {
|
|
assignmentId: number;
|
|
existingTeam?: ProjectAssignmentWorkOrderDetail[];
|
|
onCancel?: VoidFunction
|
|
onSuccess?: VoidFunction;
|
|
};
|
|
|
|
const validationSchema = Yup.object().shape({
|
|
user_id: 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_id,
|
|
"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(() => {
|
|
if (!userList?.user) return []
|
|
|
|
const filterUser = userList?.user.filter((item: IUser) => existingTeam?.every(team => team.user.id !== item.id)) || []
|
|
return filterUser?.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_id'
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Employee to Assign <span className='text-red-600 font-bold'>*</span></FormLabel>
|
|
<FormControl>
|
|
<ComboboxCustomUI 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; |