mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 22:56:27 +00:00
49f766ad27
Pass assignment start/end dates into ManageTeam and enforce them in the UI. Updated components (ClientDetailComponent, AssignmentDetailComponent(.old), WorkOrderCard) to provide startDate/endDate props. Modified ManageTeam signature to accept startDate/endDate, added dayjs import to format displayed dates, and implemented disabledDate logic on the DatePicker controls to prevent selecting dates outside the assignment boundaries (normalizing dates to midnight and checking against assignment and user-selected dates). Also removed a stray console.log in AssignmentDetailComponent.old and adjusted ProjectDetailPage to fetch project assignments via the filtered query string (added a console.log(project) for debugging). Files changed: ClientDetailComponent.tsx, AssignmentDetailComponent.old.tsx, AssignmentDetailComponent.tsx, WorkOrderCard.tsx, page.tsx, ManageTeam.tsx.
309 lines
19 KiB
TypeScript
309 lines
19 KiB
TypeScript
"use client"
|
|
|
|
import { useState } from "react";
|
|
import {
|
|
Edit2, Trash2, Save, X, Loader2, UserPlus
|
|
} from "lucide-react";
|
|
import {
|
|
Table, TableBody, TableCell, TableHead, TableHeader, TableRow
|
|
} from "@/components/ui/table";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Badge } from "@/components/ui/badge";
|
|
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
|
|
import {
|
|
Select, SelectContent, SelectItem, SelectTrigger, SelectValue
|
|
} from "@/components/ui/select";
|
|
import { DatePicker } from "@/components/ui/datepicker";
|
|
import {
|
|
AlertDialog, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle
|
|
} from "@/components/ui/alert-dialog"
|
|
import AddTeam from "./AddTeam";
|
|
import { useFetchWithAxios } from "@/lib/axios/useFetch";
|
|
import { Skeleton } from "@/components/ui/skeleton";
|
|
import { ConfirmDelete } from "@/components/modal/ConfirmDelete";
|
|
import { useRouter } from "next/navigation";
|
|
import { toast } from "sonner";
|
|
import { serverApiCall } from "@/lib/server-api";
|
|
import dayjs from "dayjs";
|
|
|
|
|
|
export interface IProjectAssignmentAssignUserDetail {
|
|
id: number
|
|
end_date: any
|
|
start_date: any
|
|
status: string
|
|
user: User
|
|
}
|
|
|
|
export interface User {
|
|
id: number
|
|
fullname: string
|
|
position: Position
|
|
email: string
|
|
avatar: string
|
|
}
|
|
|
|
export interface Position {
|
|
name: string
|
|
}
|
|
|
|
|
|
|
|
export function ManageTeam({ assignmentId, onClose, startDate, endDate }: { assignmentId: number, onClose: VoidFunction, startDate?: any, endDate?: any }) {
|
|
const [editingId, setEditingId] = useState<number | null>(null);
|
|
const [draftData, setDraftData] = useState<Partial<any>>({});
|
|
const [isSaving, setIsSaving] = useState(false);
|
|
const [showAddTeamDialog, setShowAddTeamDialog] = useState(false);
|
|
const [deletedId, setDeletedId] = useState<number | null>(null);
|
|
|
|
const { data: existUserAssign, isLoading, mutate } = useFetchWithAxios(`/project-assignment/assign-user/${assignmentId}`)
|
|
|
|
const router = useRouter()
|
|
|
|
const handleEdit = (row: IProjectAssignmentAssignUserDetail) => {
|
|
setEditingId(row.id);
|
|
|
|
setDraftData({
|
|
id: row.id,
|
|
start_date: row.start_date,
|
|
end_date: row.end_date,
|
|
status: row.status,
|
|
});
|
|
};
|
|
|
|
const handleSave = async () => {
|
|
setIsSaving(true);
|
|
|
|
const submitPayload = {
|
|
"project_assignment_id": assignmentId,
|
|
"status": draftData.status,
|
|
"start_date": draftData.start_date ? new Date(draftData.start_date).toISOString() : '',
|
|
"end_date": draftData.end_date ? new Date(draftData.end_date).toISOString() : '',
|
|
}
|
|
|
|
console.log(submitPayload)
|
|
const res = await serverApiCall('PUT', `/project-assignment/assign-user/update/${draftData.id}`, submitPayload)
|
|
|
|
setIsSaving(false);
|
|
if (res.success) {
|
|
toast.success('Team updated successfully')
|
|
handleSuccess!()
|
|
} else {
|
|
toast.error(`Fail to save team`, { description: `(${res.code || '-'}) ${res.message}` })
|
|
}
|
|
};
|
|
|
|
const handleDraftChange = (field: any, value: string) => {
|
|
setDraftData((prev) => ({ ...prev, [field]: value }));
|
|
};
|
|
|
|
const handleCancel = () => {
|
|
setEditingId(null);
|
|
setDraftData({});
|
|
setShowAddTeamDialog(false);
|
|
setDeletedId(null)
|
|
};
|
|
|
|
|
|
const handleSuccess = () => {
|
|
handleCancel()
|
|
mutate()
|
|
router.refresh()
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<AlertDialog open={true} >
|
|
<AlertDialogContent className="min-w-full md:min-w-max py-7">
|
|
<div className="flex flex-col sm:flex-row justify-between gap-3">
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>Manage Team</AlertDialogTitle>
|
|
<AlertDialogDescription>
|
|
Add team members and manage who is working on each project assignment.
|
|
</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
<Button size='sm' variant='outline' onClick={() => setShowAddTeamDialog(true)}><UserPlus /> Add Team</Button>
|
|
</div>
|
|
<div className="rounded-md border bg-card overflow-x-auto my-5">
|
|
<Table>
|
|
<TableHeader>
|
|
<TableRow>
|
|
<TableHead className="w-75">Name</TableHead>
|
|
<TableHead>Start Date</TableHead>
|
|
<TableHead>End Date</TableHead>
|
|
<TableHead>Status</TableHead>
|
|
<TableHead className="text-right">Action</TableHead>
|
|
</TableRow>
|
|
</TableHeader>
|
|
<TableBody>
|
|
{isLoading ?
|
|
<TableRow>
|
|
<TableCell colSpan={5} className="w-full space-y-3">
|
|
<Skeleton className='h-10 w-full' />
|
|
<Skeleton className='h-10 w-full' />
|
|
<Skeleton className='h-10 w-full' />
|
|
</TableCell>
|
|
</TableRow>
|
|
:
|
|
existUserAssign.length === 0 ?
|
|
<TableRow>
|
|
<TableCell colSpan={5} className="w-full py-6">
|
|
<div className="text-center text-muted-foreground flex flex-col items-center justify-center gap-4">
|
|
No team members have been added yet.<br />Create a team to get started with project assignments
|
|
<Button variant='outline' className="w-max" onClick={() => setShowAddTeamDialog(true)}><UserPlus /> Add Team</Button>
|
|
</div>
|
|
</TableCell>
|
|
</TableRow>
|
|
:
|
|
existUserAssign.map((row: IProjectAssignmentAssignUserDetail) => {
|
|
const isEditing = editingId === row.id;
|
|
const isOtherRowEditing = editingId !== null && editingId !== row.id;
|
|
|
|
return (
|
|
<TableRow key={row.id}>
|
|
<TableCell>
|
|
<div className="flex items-center gap-3">
|
|
<Avatar className="h-9 w-9">
|
|
<AvatarImage src={`${process.env.BASE_URL_IMG}/${row.user.avatar}`} alt={row.user.fullname} />
|
|
<AvatarFallback>{row.user.fullname?.charAt(0)}</AvatarFallback>
|
|
</Avatar>
|
|
<div className="flex flex-col">
|
|
<div className="flex items-center gap-1">
|
|
<span className="font-medium text-sm leading-none">{row.user.fullname}</span>
|
|
<Badge variant="secondary" className="ml-auto">{row.user.position?.name}</Badge>
|
|
</div>
|
|
<span className="text-xs text-muted-foreground mt-1">{row.user.email}</span>
|
|
</div>
|
|
</div>
|
|
</TableCell>
|
|
|
|
<TableCell>
|
|
{isEditing ?
|
|
<DatePicker date={draftData.start_date as Date | undefined} setDate={(date) => handleDraftChange("start_date", date as any)}
|
|
disabledDate={(date: Date) => {
|
|
// const assStartDate = startDate
|
|
// const assEndDate = endDate
|
|
|
|
// return assStartDate ? (date <= new Date(assStartDate) || date > new Date(assEndDate)) : false;
|
|
if (!startDate || !endDate) return false;
|
|
|
|
// 1. Normalize all dates to midnight (00:00:00) to strip out time differences
|
|
const targetDate = new Date(date).setHours(0, 0, 0, 0);
|
|
const minDate = new Date(startDate).setHours(0, 0, 0, 0);
|
|
const maxDate = new Date(endDate).setHours(0, 0, 0, 0);
|
|
|
|
// 2. Disable if strictly BEFORE (<) the start date, or strictly AFTER (>) the end date
|
|
return targetDate < minDate || targetDate > maxDate;
|
|
}}
|
|
/>
|
|
:
|
|
<span className="text-sm">{row.start_date ? dayjs(row.start_date).format('DD MMM YYYY') : ''}</span>
|
|
}
|
|
</TableCell>
|
|
|
|
<TableCell>
|
|
{isEditing ?
|
|
<DatePicker date={draftData.end_date as Date | undefined} setDate={(date) => handleDraftChange("end_date", date as any)}
|
|
disabledDate={(date: Date) => {
|
|
// 1. Normalize the calendar date we are currently checking
|
|
const targetDate = new Date(date).setHours(0, 0, 0, 0);
|
|
|
|
const assStartDate = startDate;
|
|
const assEndDate = endDate;
|
|
const userStartDate = draftData.start_date;
|
|
|
|
// 2. Check against the User's selected Start Date (if it exists)
|
|
if (userStartDate) {
|
|
const minUserDate = new Date(userStartDate).setHours(0, 0, 0, 0);
|
|
if (targetDate < minUserDate) return true; // Disable if before user's start date
|
|
}
|
|
|
|
// 3. Check against the overall Assignment Start boundary (if it exists)
|
|
if (assStartDate) {
|
|
const minAssDate = new Date(assStartDate).setHours(0, 0, 0, 0);
|
|
if (targetDate < minAssDate) return true; // Disable if before assignment start limit
|
|
}
|
|
|
|
// 4. Check against the overall Assignment End boundary (if it exists)
|
|
if (assEndDate) {
|
|
const maxAssDate = new Date(assEndDate).setHours(0, 0, 0, 0);
|
|
if (targetDate > maxAssDate) return true; // Disable if AFTER assignment end limit
|
|
}
|
|
|
|
// If it passes all existing checks, it is a valid date!
|
|
return false;
|
|
}}
|
|
/>
|
|
:
|
|
<span className="text-sm">{row.end_date ? dayjs(row.end_date).format('DD MMM YYYY') : ''}</span>
|
|
}
|
|
</TableCell>
|
|
|
|
<TableCell>
|
|
{isEditing ?
|
|
<Select value={draftData.status} onValueChange={(value) => handleDraftChange("status", value)}>
|
|
<SelectTrigger className="h-8 w-32.5 dark:bg-zinc-950">
|
|
<SelectValue placeholder="Select status" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="ACTIVE">Active</SelectItem>
|
|
<SelectItem value="INACTIVE">In Active</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
:
|
|
<Badge variant={row.status === "ACTIVE" ? 'statusActive' : "destructive"} className='inline-block lowercase first-letter:uppercase' >{row.status}</Badge>
|
|
}
|
|
</TableCell>
|
|
|
|
<TableCell className="text-right">
|
|
<div className="flex justify-end gap-0.5">
|
|
{isEditing ?
|
|
<>
|
|
<Button variant="outline" className="h-10 w-10 text-green-600 hover:text-green-700 hover:bg-green-100" onClick={handleSave} disabled={isSaving}>
|
|
{isSaving ? <Loader2 className=" animate-spin" /> : <Save className="" />}
|
|
</Button>
|
|
<Button variant="outline" className="h-10 w-10" onClick={handleCancel} disabled={isSaving}>
|
|
<X className="" />
|
|
</Button>
|
|
</>
|
|
:
|
|
!isOtherRowEditing && (
|
|
<>
|
|
<Button variant="outline" className="h-8 w-8 text-teal-600 hover:text-teal-700 hover:bg-teal-100" onClick={() => handleEdit(row)}>
|
|
<Edit2 className="size-3.5" />
|
|
</Button>
|
|
<Button variant="outline" className="h-8 w-8 text-red-600 hover:text-red-700 hover:bg-red-100" onClick={() => setDeletedId(row.id)}>
|
|
<Trash2 className="size-3.5" />
|
|
</Button>
|
|
</>
|
|
)
|
|
}
|
|
</div>
|
|
</TableCell>
|
|
</TableRow>
|
|
);
|
|
})
|
|
}
|
|
</TableBody>
|
|
</Table>
|
|
</div>
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel onClick={onClose} disabled={isSaving}>Close</AlertDialogCancel>
|
|
</AlertDialogFooter>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
|
|
{showAddTeamDialog ?
|
|
<AddTeam assignmentId={assignmentId} existingTeam={existUserAssign} onCancel={() => setShowAddTeamDialog(false)} onSuccess={handleSuccess} />
|
|
: null
|
|
}
|
|
|
|
{deletedId ?
|
|
<ConfirmDelete description="This action cannot be undone. This will permanently delete this user from this assignment." endpoint={`/project-assignment/assign-user/delete/${deletedId}`}
|
|
onCancel={handleCancel} onSuccess={handleSuccess}
|
|
/>
|
|
: null
|
|
}
|
|
</>
|
|
);
|
|
} |