"use client" import { useState } from "react"; import { Edit2, Trash2, Save, X, Loader2, UserPlus, UserGroup } 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 { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/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(null); const [draftData, setDraftData] = useState>({}); const [isSaving, setIsSaving] = useState(false); const [showAddTeamDialog, setShowAddTeamDialog] = useState(false); const [deletedId, setDeletedId] = useState(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 ? dayjs(draftData.start_date).format("YYYY-MM-DD") : '', "end_date": draftData.end_date ? dayjs(draftData.end_date).format("YYYY-MM-DD") : '', } 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 ( <> { if (!open) onClose(); }}>
Manage Team Add team members and manage who is working on each project assignment.
Name Start Date End Date Status Action {isLoading ? : existUserAssign.length === 0 ?
No team members have been added yet.
Create a team to get started with project assignments
: existUserAssign.map((row: IProjectAssignmentAssignUserDetail) => { const isEditing = editingId === row.id; const isOtherRowEditing = editingId !== null && editingId !== row.id; return (
{row.user.fullname?.charAt(0)}
{row.user.fullname} {row.user.position?.name}
{row.user.email}
{isEditing ? 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; }} /> : {row.start_date ? dayjs(row.start_date).format('DD MMM YYYY') : ''} } {isEditing ? 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; }} /> : {row.end_date ? dayjs(row.end_date).format('DD MMM YYYY') : ''} } {isEditing ? : {row.status} }
{isEditing ? <> : !isOtherRowEditing && ( <> ) }
); }) }
{showAddTeamDialog ? setShowAddTeamDialog(false)} onSuccess={handleSuccess} /> : null } {deletedId ? : null } ); }