mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 22:46:27 +00:00
d61e424012
Consolidate and improve client/task UI and related forms: - Replace/rename modal components: use ManageTeam and UpdateTimelineStatus consistently (replace previous ManageEmployee/FormUpdateStatus imports/usages). - ClientDetail: update grid layout, add End User column in table view, guard status with optional chaining, adjust avatar size, and minor label tweaks. - Project/Assignment forms: add end_user_id to assignment schema and form, fetch end-users, integrate ComboboxCustomUI and lazy image rendering for end-user selection, allow creating end-user inline, and fix autofill mappings for project selection. Improve loading handling and payload construction for create/update flows. - Project assignments page: switch to fetching project list endpoint and pass correct project type to form. - WorkOrderCard & AssignmentDetail: optional chaining for status, use correct selected IDs for modals, and ensure ManageTeam receives real assignment id. - ManageTeam: update data shape (field renames: fullname, position, start_date, end_date, status strings), change PUT endpoint for updating assignment user, improve empty-state copy and avatar/fallback handling, adjust status select values. - UpdateTimelineStatus: remove unused form, adjust payload mapping, make dialog scrollable, tweak layout and textarea sizing. - AssignmentTable & ClientDetail table: adjust column widths, truncation classes, and use assignment.end_user where appropriate. - Small UX/cleanup: hide category filter on HomeTask, comment out end_user in FormClient, minor spacing/formatting cleanups, and a temporary console.log in page.tsx. These changes improve UI consistency, add explicit end-user handling across forms, harden status access with optional chaining, and align API usages and data shapes for team/status operations.
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 { ComboboxCustomUI } from '@/components/ui/combobox-custom-ui';
|
|
|
|
type Props = {
|
|
assignmentId: number;
|
|
existingTeam?: number[];
|
|
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(() => {
|
|
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_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; |