mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:46:29 +00:00
78f48d783f
Introduce client and assignment detail pages and client-side components for the Task dashboard. Adds interactive ClientDetailComponent, AssignmentDetailComponent, page entries (server + dummy variants), project-assignment form, various UI components (AssignmentTable, ClientGrid, ManageEmployee, FormUpdateStatus, ConfirmDelete, LazyImage helper), types and a dummy data hook. Update app/(dashboard)/task/page.tsx to use the new components. Update .env.development to point to dev API endpoints, bump NEXT_PUBLIC_APP_VERSION to v1.2.0 and add NEXT_PUBLIC_S3_IMAGE_URL.
504 lines
28 KiB
TypeScript
504 lines
28 KiB
TypeScript
"use client"
|
|
|
|
import { motion, AnimatePresence } from 'motion/react';
|
|
import {
|
|
ArrowLeft, ArrowRight, Save, Briefcase, Clock, Search, Check
|
|
} from 'lucide-react';
|
|
import { useState, useMemo, use } from 'react';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Input } from '@/components/ui/input';
|
|
import { Label } from '@/components/ui/label';
|
|
import { Textarea } from '@/components/ui/textarea';
|
|
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card';
|
|
import { Badge } from '@/components/ui/badge';
|
|
import { Separator } from '@/components/ui/separator';
|
|
import {
|
|
Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList,
|
|
} from '@/components/ui/command';
|
|
import {
|
|
Popover, PopoverContent, PopoverTrigger,
|
|
} from '@/components/ui/popover';
|
|
import { toast } from 'sonner';
|
|
import { cn } from '@/lib/utils';
|
|
import { useAppState } from '@/hooks/useDummyTaskData';
|
|
import { useRouter } from 'next/navigation';
|
|
import { IProjectType } from '../../../types';
|
|
import { DatePicker } from '@/components/ui/datepicker';
|
|
|
|
type IStep = 'type' | 'details' | 'work-orders' | 'review';
|
|
|
|
type Props = {
|
|
params: Promise<{ client_id: string | undefined }>
|
|
};
|
|
|
|
export default function CreateAssignment({ params }: Props) {
|
|
const { clients, assignments, addAssignment } = useAppState();
|
|
const router = useRouter()
|
|
// const { id } = await params;
|
|
const { client_id: clientId } = use(params);
|
|
|
|
// Form State
|
|
const [type, setType] = useState<IProjectType>('Project');
|
|
const [name, setName] = useState('');
|
|
const [description, setDescription] = useState('');
|
|
const [code, setCode] = useState('');
|
|
const [year, setYear] = useState(new Date().getFullYear().toString());
|
|
const [startDate, setStartDate] = useState<Date | undefined>();
|
|
const [endDate, setEndDate] = useState<Date | undefined>();
|
|
const [workOrder, setWorkOrder] = useState({
|
|
name: '',
|
|
description: '',
|
|
code: '',
|
|
startDate: new Date(),
|
|
endDate: new Date()
|
|
});
|
|
|
|
// Autocomplete state for existing projects
|
|
const [openProjectSearch, setOpenProjectSearch] = useState(false);
|
|
|
|
|
|
const [step, setStep] = useState<IStep>('type');
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
|
|
const existingProjects = useMemo(() => {
|
|
const projects = assignments.filter(a => a.type === 'Project');
|
|
// Unique projects by name
|
|
const unique: Record<string, typeof projects[0]> = {};
|
|
projects.forEach(p => {
|
|
if (!unique[p.name]) unique[p.name] = p;
|
|
});
|
|
return Object.values(unique);
|
|
}, [assignments]);
|
|
|
|
const currentClient = clients.find(c => c.id === clientId);
|
|
|
|
const handleNext = () => {
|
|
if (step === 'type') setStep('details');
|
|
else if (step === 'details') {
|
|
if (type === 'Project') setStep('work-orders');
|
|
else setStep('review');
|
|
}
|
|
else if (step === 'work-orders') setStep('review');
|
|
};
|
|
|
|
const handleBack = () => {
|
|
if (step === 'details') setStep('type');
|
|
else if (step === 'work-orders') setStep('details');
|
|
else if (step === 'review') {
|
|
if (type === 'Project') setStep('work-orders');
|
|
else setStep('details');
|
|
}
|
|
};
|
|
|
|
const handleSave = async () => {
|
|
setLoading(true);
|
|
const assignmentId = Math.random().toString(36).substr(2, 9);
|
|
|
|
const newAssignment = {
|
|
id: assignmentId,
|
|
clientId,
|
|
type,
|
|
name,
|
|
description,
|
|
code,
|
|
year,
|
|
startDate,
|
|
endDate,
|
|
status: 'Active' as const,
|
|
statusHistory: [{
|
|
id: Math.random().toString(36).substr(2, 9),
|
|
status: 'Active',
|
|
date: new Date().toISOString().split('T')[0],
|
|
note: 'Initial assignment creation',
|
|
createdAt: new Date().toISOString()
|
|
}],
|
|
createdAt: new Date().toISOString(),
|
|
workOrders: type === 'Project' ? [{
|
|
id: Math.random().toString(36).substr(2, 9),
|
|
projectId: assignmentId,
|
|
name: workOrder.name,
|
|
description: workOrder.description,
|
|
code: workOrder.code,
|
|
startDate: workOrder.startDate,
|
|
endDate: workOrder.endDate,
|
|
status: 'Active' as const,
|
|
statusHistory: [{
|
|
id: Math.random().toString(36).substr(2, 9),
|
|
status: 'Active',
|
|
date: new Date().toISOString().split('T')[0],
|
|
note: 'Initial work order creation',
|
|
createdAt: new Date().toISOString()
|
|
}],
|
|
createdAt: new Date().toISOString(),
|
|
}] : [],
|
|
assignedEmployees: [], // Initialized as empty
|
|
};
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
addAssignment(newAssignment);
|
|
|
|
setLoading(false);
|
|
toast.success(`${type} created successfully`);
|
|
router.replace(`/task/client-detail/${clientId}`);
|
|
};
|
|
|
|
const steps: { id: IStep; label: string }[] = [
|
|
{ id: 'type', label: 'Type' },
|
|
{ id: 'details', label: 'Details' },
|
|
...(type === 'Project' ? [{ id: 'work-orders' as IStep, label: 'Work Orders' }] : []),
|
|
{ id: 'review', label: 'Review' },
|
|
];
|
|
|
|
const currentStepIndex = steps.findIndex(s => s.id === step);
|
|
|
|
return (
|
|
<>
|
|
<div className="max-w-4xl mx-auto space-y-8 p-6 sm:p-8 pt-6 pb-20">
|
|
<div className="flex items-center justify-between">
|
|
<div className="flex items-center gap-4">
|
|
<Button variant="ghost" size="icon" onClick={() => router.back()}>
|
|
<ArrowLeft className="w-5 h-5" />
|
|
</Button>
|
|
<div>
|
|
<h1 className="text-3xl font-bold">Create Project Assignment</h1>
|
|
<p className="text-muted-foreground">
|
|
{currentClient ? `For ${currentClient.name}` : 'Assign a new project or IM to a client'}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Stepper */}
|
|
<div className="flex items-center justify-between px-2">
|
|
{steps.map((s, i) => (
|
|
<div key={s.id} className="flex items-center flex-1 last:flex-none">
|
|
<div className="flex flex-col items-center gap-2">
|
|
<div className={cn(
|
|
"w-10 h-10 rounded-full flex items-center justify-center border-2 transition-all duration-300",
|
|
i <= currentStepIndex ? "bg-zinc-900 dark:bg-zinc-100 border-primary text-secondary" : "border-border text-muted-foreground"
|
|
)}>
|
|
{i < currentStepIndex ? <Check className="w-5 h-5" /> : <span>{i + 1}</span>}
|
|
</div>
|
|
<span className={cn(
|
|
"text-xs font-medium hidden md:block",
|
|
i <= currentStepIndex ? "text-primary" : "text-muted-foreground"
|
|
)}>{s.label}</span>
|
|
</div>
|
|
{i < steps.length - 1 && (
|
|
<div className={cn(
|
|
"h-0.5 flex-1 mx-4 transition-all duration-500",
|
|
i < currentStepIndex ? "bg-primary" : "bg-border"
|
|
)} />
|
|
)}
|
|
</div>
|
|
))}
|
|
</div>
|
|
|
|
<AnimatePresence mode="wait">
|
|
<motion.div
|
|
key={step}
|
|
initial={{ opacity: 0, x: 20 }}
|
|
animate={{ opacity: 1, x: 0 }}
|
|
exit={{ opacity: 0, x: -20 }}
|
|
transition={{ duration: 0.3 }}
|
|
>
|
|
{step === 'type' && (
|
|
<div className="grid md:grid-cols-2 gap-6">
|
|
<Card
|
|
className={cn(
|
|
"cursor-pointer transition-all hover:border-his dark:hover:border-his duration-400",
|
|
type === 'Project' ? "border-his ring-2 ring-his/10 dark:ring-his" : ""
|
|
)}
|
|
onClick={() => setType('Project')}
|
|
>
|
|
<CardContent className="p-8 flex flex-col items-center text-center space-y-4">
|
|
<div className="w-16 h-16 rounded-2xl bg-orange-500/10 text-orange-500 flex items-center justify-center">
|
|
<Briefcase className="w-8 h-8" />
|
|
</div>
|
|
<div>
|
|
<h3 className="text-xl font-bold">Project</h3>
|
|
<p className="text-muted-foreground text-sm mt-2">
|
|
Standard project assignment. Requires Work Orders and detailed planning.
|
|
</p>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
|
|
<Card
|
|
className={cn(
|
|
"cursor-pointer transition-all hover:border-his dark:hover:border-his duration-400",
|
|
type === 'IM' ? "border-his ring-2 ring-his/10 dark:ring-his" : ""
|
|
)}
|
|
onClick={() => setType('IM')}
|
|
>
|
|
<CardContent className="p-8 flex flex-col items-center text-center space-y-4">
|
|
<div className="w-16 h-16 rounded-2xl bg-purple-500/10 text-purple-500 flex items-center justify-center">
|
|
<Clock className="w-8 h-8" />
|
|
</div>
|
|
<div>
|
|
<h3 className="text-xl font-bold">Internal Memo (IM)</h3>
|
|
<p className="text-muted-foreground text-sm mt-2">
|
|
Used for Proof of Concept (PoC) stages. Can be converted to a Project later.
|
|
</p>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
)}
|
|
|
|
{step === 'details' && (
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>{type} Details</CardTitle>
|
|
<CardDescription>Provide the basic information for this assignment.</CardDescription>
|
|
</CardHeader>
|
|
<CardContent className="space-y-6">
|
|
<div className="space-y-2">
|
|
<Label>Assignment Name</Label>
|
|
<div className="flex gap-2">
|
|
<div className="flex-1 relative">
|
|
<Input
|
|
placeholder={type === 'Project' ? "Project Name" : "IM Name"}
|
|
value={name}
|
|
onChange={(e) => setName(e.target.value)}
|
|
/>
|
|
{type === 'Project' && (
|
|
<div className="absolute right-2 top-1/2 -translate-y-1/2">
|
|
<Popover open={openProjectSearch} onOpenChange={setOpenProjectSearch}>
|
|
<PopoverTrigger asChild>
|
|
<Button variant="ghost" size="sm" className="h-7 px-2 text-xs">
|
|
<Search className="w-3 h-3 mr-1" />
|
|
Existing
|
|
</Button>
|
|
</PopoverTrigger>
|
|
<PopoverContent className="p-0 w-[300px]" align="end">
|
|
<Command>
|
|
<CommandInput placeholder="Search existing projects..." />
|
|
<CommandList>
|
|
<CommandEmpty>No projects found.</CommandEmpty>
|
|
<CommandGroup heading="Recent Projects">
|
|
{existingProjects.map(p => (
|
|
<CommandItem
|
|
key={p.id}
|
|
onSelect={() => {
|
|
setName(p.name);
|
|
setDescription(p.description);
|
|
setOpenProjectSearch(false);
|
|
toast.info(`Autofilled details from ${p.name}`);
|
|
}}
|
|
>
|
|
<Check className={cn("mr-2 h-4 w-4", name === p.name ? "opacity-100" : "opacity-0")} />
|
|
{p.name}
|
|
</CommandItem>
|
|
))}
|
|
</CommandGroup>
|
|
</CommandList>
|
|
</Command>
|
|
</PopoverContent>
|
|
</Popover>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="grid md:grid-cols-2 gap-4">
|
|
<div className="space-y-2">
|
|
<Label>Project Code</Label>
|
|
<Input
|
|
placeholder="e.g. 24016"
|
|
value={code}
|
|
onChange={(e) => setCode(e.target.value)}
|
|
/>
|
|
</div>
|
|
<div className="space-y-2">
|
|
<Label>Project Year</Label>
|
|
<Input
|
|
placeholder="e.g. 2024"
|
|
value={year}
|
|
onChange={(e) => setYear(e.target.value)}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="grid md:grid-cols-2 gap-4">
|
|
<div className="space-y-2">
|
|
<Label>Start Date</Label>
|
|
<DatePicker date={startDate} setDate={setStartDate} />
|
|
</div>
|
|
<div className="space-y-2">
|
|
<Label>End Date</Label>
|
|
<DatePicker date={endDate} setDate={setEndDate} />
|
|
</div>
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<Label>Description</Label>
|
|
<Textarea
|
|
placeholder="Describe the scope of this assignment..."
|
|
className="min-h-[120px]"
|
|
value={description}
|
|
onChange={(e) => setDescription(e.target.value)}
|
|
/>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
)}
|
|
|
|
{step === 'work-orders' && (
|
|
<div className="space-y-6">
|
|
<div>
|
|
<h2 className="text-2xl font-bold">Work Order</h2>
|
|
<p className="text-muted-foreground">Define the initial task for this project.</p>
|
|
</div>
|
|
|
|
<Card>
|
|
<CardContent className="p-6 space-y-4">
|
|
<div className="grid gap-4 md:grid-cols-2">
|
|
<div className="space-y-2">
|
|
<Label>WO Name</Label>
|
|
<Input
|
|
placeholder="e.g. Initial Site Survey"
|
|
value={workOrder.name}
|
|
onChange={(e) => setWorkOrder({ ...workOrder, name: e.target.value })}
|
|
/>
|
|
</div>
|
|
<div className="space-y-2">
|
|
<Label>WO Code</Label>
|
|
<Input
|
|
placeholder="e.g. WO-24016"
|
|
value={workOrder.code}
|
|
onChange={(e) => setWorkOrder({ ...workOrder, code: e.target.value })}
|
|
/>
|
|
</div>
|
|
</div>
|
|
<div className="grid gap-4 md:grid-cols-2">
|
|
<div className="space-y-2">
|
|
<Label>WO Start Date</Label>
|
|
<DatePicker date={workOrder.startDate} setDate={(val) => setWorkOrder({ ...workOrder, startDate: val! })} />
|
|
</div>
|
|
<div className="space-y-2">
|
|
<Label>WO End Date</Label>
|
|
<DatePicker date={workOrder.endDate} setDate={(val) => setWorkOrder({ ...workOrder, endDate: val! })} />
|
|
</div>
|
|
</div>
|
|
<div className="space-y-2">
|
|
<Label>WO Description</Label>
|
|
<Input
|
|
placeholder="Briefly describe the task..."
|
|
value={workOrder.description}
|
|
onChange={(e) => setWorkOrder({ ...workOrder, description: e.target.value })}
|
|
/>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
)}
|
|
|
|
{step === 'review' && (
|
|
<div className="space-y-6">
|
|
<div>
|
|
<h2 className="text-2xl font-bold">Review Assignment</h2>
|
|
<p className="text-muted-foreground">Double check everything before saving.</p>
|
|
</div>
|
|
|
|
<div className="grid gap-6">
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>Overview</CardTitle>
|
|
</CardHeader>
|
|
<CardContent className="space-y-6">
|
|
<div className="grid grid-cols-2 gap-4">
|
|
<div>
|
|
<Label className="text-xs text-muted-foreground uppercase">Type</Label>
|
|
<div className="mt-1">
|
|
<Badge variant={type === 'Project' ? 'default' : 'secondary'}>{type}</Badge>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<Label className="text-xs text-muted-foreground uppercase">Client</Label>
|
|
<p className="font-medium mt-1">{currentClient?.name}</p>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<Label className="text-xs text-muted-foreground uppercase">Name</Label>
|
|
<p className="text-xl font-bold mt-1">{name || 'Untitled Assignment'}</p>
|
|
</div>
|
|
<div>
|
|
<Label className="text-xs text-muted-foreground uppercase">Description</Label>
|
|
<p className="text-muted-foreground mt-1">{description || 'No description provided.'}</p>
|
|
</div>
|
|
|
|
{type === 'Project' && workOrder.name && (
|
|
<>
|
|
<Separator />
|
|
<div>
|
|
<Label className="text-xs text-muted-foreground uppercase mb-3 block">Work Order</Label>
|
|
<div className="flex items-center gap-3 p-2 bg-accent/30 rounded-md">
|
|
<div className="w-6 h-6 rounded-full bg-primary/10 text-primary flex items-center justify-center text-[10px] font-bold">
|
|
1
|
|
</div>
|
|
<div>
|
|
<p className="text-sm font-medium">{workOrder.name}</p>
|
|
<p className="text-xs text-muted-foreground">{workOrder.description}</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</>
|
|
)}
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
<p className="text-sm text-muted-foreground italic text-center">
|
|
Note: Employees can be assigned after the assignment is created.
|
|
</p>
|
|
</div>
|
|
)}
|
|
</motion.div>
|
|
</AnimatePresence>
|
|
|
|
{/* Navigation Buttons */}
|
|
<div className="fixed bottom-0 left-0 right-0 p-6 bg-background/80 backdrop-blur-md border-t border-border flex justify-between items-center z-50">
|
|
<Button
|
|
variant="ghost"
|
|
onClick={handleBack}
|
|
disabled={step === 'type'}
|
|
>
|
|
Back
|
|
</Button>
|
|
|
|
<div className="flex gap-4">
|
|
{step !== 'review' ?
|
|
<Button
|
|
className="gap-2"
|
|
onClick={handleNext}
|
|
disabled={
|
|
(step === 'details' && (!name || !clientId)) ||
|
|
(step === 'type' && !type)
|
|
}
|
|
>
|
|
Next Step
|
|
<ArrowRight className="w-4 h-4" />
|
|
</Button>
|
|
:
|
|
<Button
|
|
className="gap-2"
|
|
onClick={handleSave}
|
|
disabled={loading}
|
|
>
|
|
{loading ? 'Saving...' : (
|
|
<>
|
|
<Save className="w-4 h-4" />
|
|
Save Assignment
|
|
</>
|
|
)}
|
|
</Button>
|
|
}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</>
|
|
);
|
|
}
|