mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 23:16:29 +00:00
dcb74c0f2b
Introduce a reusable HoverCard component (components/ui/hover-card.tsx) and integrate it into the client grid to show a status summary with badges. Import the cn helper and BadgeStatus in ClientGrid, add a status pill trigger with animated indicator, and switch displayed client reference from end_user_name to client.name. In ManageTeam, include id in draftData, replace BadgeStatus rendering with a Badge variant and adjust the edit button colors to teal; the same teal edit-button color change was applied to UpdateTimelineStatus. Remove an unnecessary comment line in the task page. These changes improve status visibility and unify UI affordances.
263 lines
14 KiB
TypeScript
263 lines
14 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 BadgeStatus from "@/components/ui/badge-status";
|
|
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";
|
|
|
|
|
|
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 }: { assignmentId: number, onClose: VoidFunction }) {
|
|
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)} />
|
|
:
|
|
<span className="text-sm">{row.start_date}</span>
|
|
}
|
|
</TableCell>
|
|
|
|
<TableCell>
|
|
{isEditing ?
|
|
<DatePicker date={draftData.end_date as Date | undefined} setDate={(date) => handleDraftChange("end_date", date as any)} />
|
|
:
|
|
<span className="text-sm">{row.end_date}</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
|
|
}
|
|
</>
|
|
);
|
|
} |