mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:46:29 +00:00
515 lines
32 KiB
TypeScript
515 lines
32 KiB
TypeScript
"use client"
|
|
|
|
import { motion } from 'motion/react';
|
|
import { ArrowLeft, Plus, Briefcase, Clock, Users, MoreVertical, Check, Calendar as CalendarIcon, LayoutGrid, Table as TableIcon } from 'lucide-react';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
|
import { Badge } from '@/components/ui/badge';
|
|
import { Separator } from '@/components/ui/separator';
|
|
import { Label } from '@/components/ui/label';
|
|
import { Input } from '@/components/ui/input';
|
|
import { Textarea } from '@/components/ui/textarea';
|
|
import {
|
|
DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger,
|
|
} from '@/components/ui/dropdown-menu';
|
|
import {
|
|
Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogFooter,
|
|
} from '@/components/ui/dialog';
|
|
import {
|
|
Select, SelectContent, SelectItem, SelectTrigger, SelectValue,
|
|
} from '@/components/ui/select';
|
|
import {
|
|
Tooltip, TooltipContent, TooltipTrigger,
|
|
} from '@/components/ui/tooltip';
|
|
import {
|
|
Table, TableBody, TableCell, TableHead, TableHeader, TableRow,
|
|
} from "@/components/ui/table";
|
|
import {
|
|
Tabs, TabsList, TabsTrigger,
|
|
} from "@/components/ui/tabs";
|
|
import { use, useState } from 'react';
|
|
import { toast } from 'sonner';
|
|
import { cn } from '@/lib/utils';
|
|
import { DUMMY_EMPLOYEES, IProjectAssignment } from '../../../types';
|
|
import { useAppState } from '@/hooks/useDummyTaskData';
|
|
import { useRouter } from 'next/navigation';
|
|
import Link from 'next/link';
|
|
|
|
type Props = {
|
|
params: Promise<{ client_id: string | undefined }>
|
|
};
|
|
|
|
export default function ClientDetailPage({ params }: Props) {
|
|
|
|
const [viewMode, setViewMode] = useState<'grid' | 'table'>('grid');
|
|
const [statusForm, setStatusForm] = useState({
|
|
status: 'Active' as IProjectAssignment['status'],
|
|
note: '',
|
|
date: new Date().toISOString().split('T')[0]
|
|
});
|
|
|
|
// const { id } = await params;
|
|
const { client_id } = use(params);
|
|
const router = useRouter()
|
|
const { clients, assignments, updateAssignment } = useAppState();
|
|
|
|
|
|
const [editingEmployeesAssignment, setEditingEmployeesAssignment] = useState<IProjectAssignment | null>(null);
|
|
const [selectedEmployeeIds, setSelectedEmployeeIds] = useState<string[]>([]);
|
|
|
|
const [updatingStatusAssignment, setUpdatingStatusAssignment] = useState<IProjectAssignment | null>(null);
|
|
|
|
const client = clients.find((c) => c.id === client_id);
|
|
const clientAssignments = assignments.filter((a) => a.clientId === client_id);
|
|
|
|
const handleOpenEmployeeDialog = (assignment: IProjectAssignment) => {
|
|
setEditingEmployeesAssignment(assignment);
|
|
setSelectedEmployeeIds(assignment.assignedEmployees);
|
|
};
|
|
|
|
const handleOpenStatusDialog = (assignment: IProjectAssignment) => {
|
|
setUpdatingStatusAssignment(assignment);
|
|
setStatusForm({
|
|
status: assignment.status,
|
|
note: '',
|
|
date: new Date().toISOString().split('T')[0]
|
|
});
|
|
};
|
|
|
|
const toggleEmployee = (empId: string) => {
|
|
setSelectedEmployeeIds(prev =>
|
|
prev.includes(empId) ? prev.filter(i => i !== empId) : [...prev, empId]
|
|
);
|
|
};
|
|
|
|
const handleSaveEmployees = () => {
|
|
if (editingEmployeesAssignment) {
|
|
updateAssignment(editingEmployeesAssignment.id, {
|
|
assignedEmployees: selectedEmployeeIds
|
|
});
|
|
toast.success('Team updated successfully');
|
|
setEditingEmployeesAssignment(null);
|
|
}
|
|
};
|
|
|
|
const handleSaveStatus = () => {
|
|
if (updatingStatusAssignment) {
|
|
updateAssignment(updatingStatusAssignment.id, {
|
|
status: statusForm.status
|
|
});
|
|
// In a real app we would save the note and date to a history log
|
|
toast.success(`Status updated to ${statusForm.status}`);
|
|
setUpdatingStatusAssignment(null);
|
|
}
|
|
};
|
|
|
|
if (!client) {
|
|
return (
|
|
<div className="flex flex-col items-center justify-center h-[60vh] space-y-4">
|
|
<h2 className="text-2xl font-bold">Client not found</h2>
|
|
<Button onClick={() => router.replace('/task')} variant='outline'>Back to Clients</Button>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<motion.div
|
|
initial={{ opacity: 0 }}
|
|
animate={{ opacity: 1 }}
|
|
exit={{ opacity: 0 }}
|
|
className="flex-1 space-y-8 p-6 sm:p-8 pt-6"
|
|
>
|
|
<div className="flex flex-col md:flex-row md:items-end justify-between gap-6">
|
|
<div className="flex items-start gap-6">
|
|
<motion.div
|
|
initial={{ scale: 0.8, opacity: 0 }}
|
|
animate={{ scale: 1, opacity: 1 }}
|
|
className="w-24 h-24 rounded-2xl overflow-hidden shadow-xl border-4 border-background"
|
|
>
|
|
<img src={client.imageUrl} alt={client.name} className="w-full h-full object-cover" />
|
|
</motion.div>
|
|
<div className="space-y-1">
|
|
<Button variant="ghost" size="sm" className="p-0 h-auto hover:bg-transparent text-muted-foreground mb-2" onClick={() => router.replace('/task')}>
|
|
<ArrowLeft className="w-4 h-4 mr-1" />
|
|
Back to Clients
|
|
</Button>
|
|
<h1 className="text-4xl font-bold tracking-tight">{client.name}</h1>
|
|
<p className="text-lg text-muted-foreground">End User: {client.endUser}</p>
|
|
</div>
|
|
</div>
|
|
<div className="flex gap-3">
|
|
<Link href={`/task/client-detail/${client_id}/project-assignments`} className='w-full sm:w-max'>
|
|
<Button className="gap-2 w-full">
|
|
<Plus className="w-4 h-4" />
|
|
Create Project Assignment
|
|
</Button>
|
|
</Link>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="grid gap-8 lg:grid-cols-3">
|
|
<div className="lg:col-span-2 space-y-6">
|
|
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-2">
|
|
<h2 className="text-2xl font-bold">Project Assignments</h2>
|
|
<div className="flex items-center gap-4">
|
|
<Tabs value={viewMode} onValueChange={(v) => setViewMode(v as 'grid' | 'table')}>
|
|
<TabsList className="grid w-24 grid-cols-2">
|
|
<TabsTrigger value="grid" className="p-1">
|
|
<LayoutGrid className="w-4 h-4" />
|
|
</TabsTrigger>
|
|
<TabsTrigger value="table" className="p-1">
|
|
<TableIcon className="w-4 h-4" />
|
|
</TabsTrigger>
|
|
</TabsList>
|
|
</Tabs>
|
|
<Badge variant="outline" className="px-3 py-1">
|
|
{clientAssignments.length} Total
|
|
</Badge>
|
|
</div>
|
|
</div>
|
|
|
|
{clientAssignments.length === 0 ? (
|
|
<Card className="border-dashed bg-accent/20">
|
|
<CardContent className="flex flex-col items-center justify-center py-16 space-y-4">
|
|
<div className="w-16 h-16 rounded-full bg-accent flex items-center justify-center">
|
|
<Briefcase className="w-8 h-8 text-muted-foreground" />
|
|
</div>
|
|
<div className="text-center">
|
|
<h3 className="text-xl font-semibold">No Project Assignments</h3>
|
|
<p className="text-muted-foreground max-w-xs mx-auto mt-2">
|
|
This client doesn't have any projects or internal memos yet. Create one to get started.
|
|
</p>
|
|
</div>
|
|
<Link href={`/task/client-detail/${client_id}/project-assignments`}>
|
|
<Button variant="outline" className="mt-4">Create Assignment</Button>
|
|
</Link>
|
|
</CardContent>
|
|
</Card>
|
|
) : viewMode === 'grid' ? (
|
|
<div className="grid gap-4">
|
|
{clientAssignments?.map((assignment) => (
|
|
<Card key={assignment.id} className="group hover:border-primary/30 transition-all duration-300">
|
|
<CardContent className="p-6">
|
|
<div className="flex items-start justify-between gap-4">
|
|
<div className="flex items-start gap-4">
|
|
<div className={`w-12 h-12 rounded-xl flex items-center justify-center shrink-0 ${assignment.type === 'Project' ? 'bg-orange-500/10 text-orange-500' : 'bg-purple-500/10 text-purple-500'
|
|
}`}>
|
|
{assignment.type === 'Project' ? <Briefcase className="w-6 h-6" /> : <Clock className="w-6 h-6" />}
|
|
</div>
|
|
<div>
|
|
<div className="flex items-center gap-3 mb-1">
|
|
<h3 className="font-bold text-xl">{assignment.name}</h3>
|
|
<Badge variant={assignment.type === 'Project' ? 'default' : 'secondary'}>
|
|
{assignment.type}
|
|
</Badge>
|
|
<Badge variant="outline" className={
|
|
assignment.status === 'Active' ? 'text-green-600 border-green-200 bg-green-50' :
|
|
assignment.status === 'Completed' ? 'text-blue-600 border-blue-200 bg-blue-50' :
|
|
'text-orange-600 border-orange-200 bg-orange-50'
|
|
}>
|
|
{assignment.status}
|
|
</Badge>
|
|
</div>
|
|
|
|
{assignment.workOrder && (
|
|
<p className="text-sm font-medium text-primary mb-1">
|
|
WO: {assignment.workOrder.name}
|
|
</p>
|
|
)}
|
|
|
|
<p className="text-muted-foreground line-clamp-2 text-sm mb-4">
|
|
{assignment.description}
|
|
</p>
|
|
|
|
<div className="flex flex-wrap items-center gap-6 text-sm text-muted-foreground">
|
|
<div className="flex items-center gap-2">
|
|
<CalendarIcon className="w-4 h-4" />
|
|
<span>
|
|
{assignment.startDate ? new Date(assignment.startDate).toLocaleDateString() : 'N/A'} -
|
|
{assignment.endDate ? new Date(assignment.endDate).toLocaleDateString() : 'N/A'}
|
|
</span>
|
|
</div>
|
|
<div className="flex items-center gap-2">
|
|
<Users className="w-4 h-4" />
|
|
<div className="flex -space-x-2">
|
|
{assignment.assignedEmployees.slice(0, 3).map(empId => {
|
|
const emp = DUMMY_EMPLOYEES.find(e => e.id === empId);
|
|
return (
|
|
<div key={empId}>
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<img
|
|
src={emp?.avatar}
|
|
alt={emp?.name}
|
|
className="w-6 h-6 rounded-full border-2 border-background cursor-help"
|
|
/>
|
|
</TooltipTrigger>
|
|
<TooltipContent>
|
|
<div className="text-xs">
|
|
<p className="font-bold">{emp?.name}</p>
|
|
<p className="opacity-80">{emp?.role}</p>
|
|
</div>
|
|
</TooltipContent>
|
|
</Tooltip>
|
|
</div>
|
|
);
|
|
})}
|
|
{assignment.assignedEmployees.length > 3 && (
|
|
<div className="w-6 h-6 rounded-full bg-accent border-2 border-background flex items-center justify-center text-[10px] font-bold">
|
|
+{assignment.assignedEmployees.length - 3}
|
|
</div>
|
|
)}
|
|
{assignment.assignedEmployees.length === 0 && <span>No employees assigned</span>}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button variant="ghost" size="icon">
|
|
<MoreVertical className="w-5 h-5" />
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="end">
|
|
<DropdownMenuItem asChild>
|
|
<Link href={`/task/client-detail/${client_id}/${assignment.id}`}>View Details</Link>
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem>Edit Assignment</DropdownMenuItem>
|
|
<DropdownMenuItem onClick={() => handleOpenEmployeeDialog(assignment)}>
|
|
Manage Team
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem className="text-destructive">Archive</DropdownMenuItem>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
))}
|
|
</div>
|
|
) : (
|
|
<Card className="overflow-hidden">
|
|
<div className="overflow-x-auto">
|
|
<Table>
|
|
<TableHeader className="bg-accent/50">
|
|
<TableRow>
|
|
<TableHead className="min-w-[200px]">Project Name</TableHead>
|
|
<TableHead>Project Code</TableHead>
|
|
<TableHead className="min-w-[200px]">Description</TableHead>
|
|
<TableHead>Year</TableHead>
|
|
<TableHead>Proj Start</TableHead>
|
|
<TableHead>Proj End</TableHead>
|
|
<TableHead className="min-w-[150px]">Work Order</TableHead>
|
|
<TableHead>WO Code</TableHead>
|
|
<TableHead>WO Start</TableHead>
|
|
<TableHead>WO End</TableHead>
|
|
<TableHead>Status</TableHead>
|
|
<TableHead className="w-[50px]"></TableHead>
|
|
</TableRow>
|
|
</TableHeader>
|
|
<TableBody>
|
|
{clientAssignments.map((assignment) => {
|
|
const wo = assignment.workOrder;
|
|
return (
|
|
<TableRow key={assignment.id}>
|
|
<TableCell className="font-medium">{assignment.name}</TableCell>
|
|
<TableCell>{assignment.code || '-'}</TableCell>
|
|
<TableCell className="max-w-[300px] truncate" title={assignment.description}>
|
|
{assignment.description}
|
|
</TableCell>
|
|
<TableCell>{assignment.year || '-'}</TableCell>
|
|
<TableCell className="whitespace-nowrap">
|
|
{assignment.startDate ? new Date(assignment.startDate).toLocaleDateString() : '-'}
|
|
</TableCell>
|
|
<TableCell className="whitespace-nowrap">
|
|
{assignment.endDate ? new Date(assignment.endDate).toLocaleDateString() : '-'}
|
|
</TableCell>
|
|
<TableCell>{wo?.name || '-'}</TableCell>
|
|
<TableCell>{wo?.code || '-'}</TableCell>
|
|
<TableCell className="whitespace-nowrap">
|
|
{wo?.startDate ? new Date(wo.startDate).toLocaleDateString() : '-'}
|
|
</TableCell>
|
|
<TableCell className="whitespace-nowrap">
|
|
{wo?.endDate ? new Date(wo.endDate).toLocaleDateString() : '-'}
|
|
</TableCell>
|
|
<TableCell>
|
|
<Badge variant="outline" className={
|
|
assignment.status === 'Active' ? 'text-green-600 border-green-200 bg-green-50' :
|
|
assignment.status === 'Completed' ? 'text-blue-600 border-blue-200 bg-blue-50' :
|
|
'text-orange-600 border-orange-200 bg-orange-50'
|
|
}>
|
|
{assignment.status}
|
|
</Badge>
|
|
</TableCell>
|
|
<TableCell>
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button variant="ghost" size="icon" className="h-8 w-8">
|
|
<MoreVertical className="w-4 h-4" />
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="end">
|
|
<DropdownMenuItem asChild>
|
|
<Link href={`/task/client-detail/${client_id}/${assignment.id}`}>View Details</Link>
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem>Edit Assignment</DropdownMenuItem>
|
|
<DropdownMenuItem onClick={() => handleOpenEmployeeDialog(assignment)}>
|
|
Manage Team
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem className="text-destructive">Archive</DropdownMenuItem>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
</TableCell>
|
|
</TableRow>
|
|
);
|
|
})}
|
|
</TableBody>
|
|
</Table>
|
|
</div>
|
|
</Card>
|
|
)}
|
|
</div>
|
|
|
|
<div className="space-y-6">
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>Client Info</CardTitle>
|
|
</CardHeader>
|
|
<CardContent className="space-y-4">
|
|
<div>
|
|
<Label className="text-xs text-muted-foreground uppercase tracking-wider">Client Name</Label>
|
|
<p className="font-medium">{client.name}</p>
|
|
</div>
|
|
<div>
|
|
<Label className="text-xs text-muted-foreground uppercase tracking-wider">End User</Label>
|
|
<p className="font-medium">{client.endUser}</p>
|
|
</div>
|
|
<Separator />
|
|
<div>
|
|
<Label className="text-xs text-muted-foreground uppercase tracking-wider">Created At</Label>
|
|
<p className="font-medium">{new Date(client?.createdAt)?.toLocaleDateString()}</p>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>Quick Stats</CardTitle>
|
|
</CardHeader>
|
|
<CardContent className="space-y-4">
|
|
<div className="flex items-center justify-between">
|
|
<span className="text-sm text-muted-foreground">Total Projects</span>
|
|
<span className="font-bold">{clientAssignments.filter(a => a.type === 'Project').length}</span>
|
|
</div>
|
|
<div className="flex items-center justify-between">
|
|
<span className="text-sm text-muted-foreground">Total IMs</span>
|
|
<span className="font-bold">{clientAssignments.filter(a => a.type === 'IM').length}</span>
|
|
</div>
|
|
<div className="flex items-center justify-between">
|
|
<span className="text-sm text-muted-foreground">Total WOs</span>
|
|
<span className="font-bold">
|
|
{clientAssignments.reduce((acc, curr) => acc + curr.workOrders?.length, 0)}
|
|
</span>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Employee Management Dialog */}
|
|
<Dialog open={!!editingEmployeesAssignment} onOpenChange={(open) => !open && setEditingEmployeesAssignment(null)}>
|
|
<DialogContent className="max-w-md">
|
|
<DialogHeader>
|
|
<DialogTitle>Manage Team</DialogTitle>
|
|
<DialogDescription>
|
|
Assign or remove employees for <strong>{editingEmployeesAssignment?.name}</strong>
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<div className="grid gap-4 py-4 max-h-[400px] overflow-y-auto pr-2">
|
|
{DUMMY_EMPLOYEES.map((emp) => (
|
|
<div
|
|
key={emp.id}
|
|
className={cn(
|
|
"flex items-center gap-4 p-3 rounded-xl border-2 transition-all cursor-pointer",
|
|
selectedEmployeeIds.includes(emp.id) ? "border-primary bg-primary/5" : "border-transparent hover:bg-accent"
|
|
)}
|
|
onClick={() => toggleEmployee(emp.id)}
|
|
>
|
|
<div className="relative">
|
|
<img src={emp.avatar} alt={emp.name} className="w-10 h-10 rounded-full object-cover" />
|
|
{selectedEmployeeIds.includes(emp.id) && (
|
|
<div className="absolute -top-1 -right-1 w-4 h-4 bg-primary rounded-full flex items-center justify-center text-white border-2 border-background">
|
|
<Check className="w-2 h-2" />
|
|
</div>
|
|
)}
|
|
</div>
|
|
<div className="flex-1 min-w-0">
|
|
<p className="font-bold text-sm truncate">{emp.name}</p>
|
|
<p className="text-xs text-muted-foreground truncate">{emp.role}</p>
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
<DialogFooter>
|
|
<Button variant="ghost" onClick={() => setEditingEmployeesAssignment(null)}>Cancel</Button>
|
|
<Button onClick={handleSaveEmployees}>Save Changes</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
|
|
{/* Status Update Dialog */}
|
|
<Dialog open={!!updatingStatusAssignment} onOpenChange={(open) => !open && setUpdatingStatusAssignment(null)}>
|
|
<DialogContent className="max-w-md">
|
|
<DialogHeader>
|
|
<DialogTitle>Update Status</DialogTitle>
|
|
<DialogDescription>
|
|
Update the current status for <strong>{updatingStatusAssignment?.name}</strong>
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<div className="grid gap-6 py-4">
|
|
<div className="space-y-2">
|
|
<Label>New Status</Label>
|
|
<Select
|
|
value={statusForm.status}
|
|
onValueChange={(v) => setStatusForm({ ...statusForm, status: v as IProjectAssignment['status'] })}
|
|
>
|
|
<SelectTrigger>
|
|
<SelectValue placeholder="Select status" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="Active">Active</SelectItem>
|
|
<SelectItem value="Completed">Completed</SelectItem>
|
|
<SelectItem value="On Hold">On Hold</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
<div className="space-y-2">
|
|
<Label>Date</Label>
|
|
<div className="relative">
|
|
<Input
|
|
type="date"
|
|
value={statusForm.date}
|
|
onChange={(e) => setStatusForm({ ...statusForm, date: e.target.value })}
|
|
/>
|
|
</div>
|
|
</div>
|
|
<div className="space-y-2">
|
|
<Label>Note / Remarks</Label>
|
|
<Textarea
|
|
placeholder="Add a note about this status change..."
|
|
value={statusForm.note}
|
|
onChange={(e) => setStatusForm({ ...statusForm, note: e.target.value })}
|
|
className="min-h-[100px]"
|
|
/>
|
|
</div>
|
|
</div>
|
|
<DialogFooter>
|
|
<Button variant="ghost" onClick={() => setUpdatingStatusAssignment(null)}>Cancel</Button>
|
|
<Button onClick={handleSaveStatus}>Update Status</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
</motion.div>
|
|
);
|
|
}
|