mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 11:06:29 +00:00
Improve project assignment flow and form UX
This adds a clearer project assignment workflow with existing-project import, work-order sync, and stronger validation for project and work-order details. It also introduces reusable checkbox and input-group UI primitives and a dedicated loading state for the project assignment screen.
This commit is contained in:
+527
-148
@@ -5,13 +5,19 @@ import { useRouter } from 'next/navigation';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { yupResolver } from '@hookform/resolvers/yup';
|
||||
import { motion, AnimatePresence } from 'motion/react';
|
||||
import { ArrowLeft, ArrowRight, Save, Briefcase, Clock, Search, Check, CheckCircle, Plus, Building2, CheckCircle2 } from 'lucide-react';
|
||||
import {
|
||||
ArrowLeft, ArrowRight, Save, Briefcase, Clock, Search, Check, CheckCircle,
|
||||
Plus, CheckCircle2, RotateCcw, X, ChevronDown, FolderGit2
|
||||
} from 'lucide-react';
|
||||
|
||||
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Textarea } from '@/components/ui/textarea';
|
||||
import { Card, CardContent, CardHeader, CardTitle, CardDescription, Card900 } from '@/components/ui/card';
|
||||
import { Checkbox } from '@/components/ui/checkbox';
|
||||
import {
|
||||
InputGroup, InputGroupAddon, InputGroupInput, InputGroupText, InputGroupTextarea
|
||||
} from '@/components/ui/input-group';
|
||||
import { CardContent, CardHeader, CardTitle, CardDescription, Card900 } from '@/components/ui/card';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { Separator } from '@/components/ui/separator';
|
||||
import {
|
||||
@@ -50,8 +56,8 @@ export const assignmentSchema = yup.object({
|
||||
code: yup.string().required('Project code is required'),
|
||||
year: yup.string().required('Year is required'),
|
||||
startDate: yup.date().required('Start date is required'),
|
||||
endDate: yup.date().nullable().min(yup.ref('startDate'), 'End date cannot be before start date').default(null),
|
||||
description: yup.string().optional().nullable().default(''),
|
||||
endDate: yup.date().required().min(yup.ref('startDate'), 'End date cannot be before start date'),
|
||||
description: yup.string().optional().nullable().max(500, 'Description cannot exceed 500 characters').default(''),
|
||||
|
||||
// Work Orders Step (Conditional based on 'type')
|
||||
workOrder: yup.object().when('type', {
|
||||
@@ -60,8 +66,8 @@ export const assignmentSchema = yup.object({
|
||||
name: yup.string().required('Work order name is required'),
|
||||
code: yup.string().required('Work order code is required'),
|
||||
startDate: yup.date().required('Start date is required'),
|
||||
endDate: yup.date().nullable().default(null),
|
||||
description: yup.string().optional().nullable().default(''),
|
||||
endDate: yup.date().required().min(yup.ref('startDate'), 'End date cannot be before start date'),
|
||||
description: yup.string().optional().nullable().max(500, 'Description cannot exceed 500 characters').default(''),
|
||||
}),
|
||||
otherwise: (schema) => schema.nullable().notRequired()
|
||||
})
|
||||
@@ -81,6 +87,7 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign
|
||||
const [showClientForm, setShowClientForm] = useState(false)
|
||||
const [selectedClient, setSelectedClient] = useState<IClient | null>(client || null)
|
||||
const [selectedEndUser, setSelectedEndUser] = useState<IEndUser | null>(null)
|
||||
const [isSameAsProject, setIsSameAsProject] = useState(false);
|
||||
|
||||
const router = useRouter();
|
||||
|
||||
@@ -109,6 +116,34 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign
|
||||
const { data: endUserList, mutate: mutateEndUserList } = useFetchWithAxios('/end-user')
|
||||
const { data: projectList, mutate: mutateProjectList } = useFetchWithAxios(`/client/project/${formValues.client_id}`)
|
||||
|
||||
// Sync work order name when "Same as Project Name" is enabled
|
||||
useEffect(() => {
|
||||
if (isSameAsProject) {
|
||||
form.setValue('workOrder.name' as any, formValues.name || '', { shouldValidate: true });
|
||||
}
|
||||
}, [isSameAsProject, formValues.name, form]);
|
||||
|
||||
const handleClearProject = () => {
|
||||
form.setValue('name', '', { shouldValidate: false });
|
||||
form.setValue('code', '', { shouldValidate: false });
|
||||
form.setValue('year', new Date().getFullYear().toString(), { shouldValidate: false });
|
||||
form.setValue('startDate', undefined as any, { shouldValidate: false });
|
||||
form.setValue('endDate', undefined as any, { shouldValidate: false });
|
||||
form.setValue('description', '', { shouldValidate: false });
|
||||
setIsExistProject(false);
|
||||
toast.info('Project form cleared');
|
||||
};
|
||||
|
||||
const handleClearWorkOrder = () => {
|
||||
setIsSameAsProject(false);
|
||||
form.setValue('workOrder.name' as any, '', { shouldValidate: false });
|
||||
form.setValue('workOrder.code' as any, '', { shouldValidate: false });
|
||||
form.setValue('workOrder.startDate' as any, undefined as any, { shouldValidate: false });
|
||||
form.setValue('workOrder.endDate' as any, undefined as any, { shouldValidate: false });
|
||||
form.setValue('workOrder.description' as any, '', { shouldValidate: false });
|
||||
toast.info('Work order form cleared');
|
||||
};
|
||||
|
||||
// 3. Navigation & Validation Logic
|
||||
const steps: { id: IStep; label: string }[] = [
|
||||
{ id: 'type', label: 'Type' },
|
||||
@@ -142,11 +177,20 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign
|
||||
form.setValue('description', data.project?.description || data.im_description)
|
||||
|
||||
form.setValue('workOrder.name' as any, data.wo_name)
|
||||
form.setValue('workOrder.code' as any, data.wo_code)
|
||||
let initialWoCode = data.wo_code || '';
|
||||
const prjCode = data.project?.code || data.im_code || '';
|
||||
if (prjCode && initialWoCode.startsWith(`${prjCode}-`)) {
|
||||
initialWoCode = initialWoCode.slice(prjCode.length + 1);
|
||||
}
|
||||
form.setValue('workOrder.code' as any, initialWoCode)
|
||||
form.setValue('workOrder.startDate' as any, data.wo_start_date)
|
||||
form.setValue('workOrder.endDate' as any, data.wo_end_date)
|
||||
form.setValue('workOrder.description' as any, data.wo_description)
|
||||
|
||||
if (data.wo_name && (data.project?.name || data.im_name) && data.wo_name === (data.project?.name || data.im_name)) {
|
||||
setIsSameAsProject(true);
|
||||
}
|
||||
|
||||
setStep('clients')
|
||||
|
||||
setIsExistAssignment(res.data.data)
|
||||
@@ -193,6 +237,15 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign
|
||||
const onSubmit = async (payload: AssignmentFormValues) => {
|
||||
setIsLoading(true);
|
||||
|
||||
const projectCode = payload.code?.trim() || '';
|
||||
const rawWoCode = ((payload.workOrder as any)?.code || '').trim();
|
||||
let finalWoCode = '';
|
||||
if (rawWoCode) {
|
||||
finalWoCode = rawWoCode.startsWith(`${projectCode}-`)
|
||||
? rawWoCode
|
||||
: `${projectCode}-${rawWoCode}`;
|
||||
}
|
||||
|
||||
const newPayload = {
|
||||
"client_id": Number(payload.client_id),
|
||||
"end_user_id": payload.end_user_id ? Number(payload.end_user_id) : null,
|
||||
@@ -211,8 +264,8 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign
|
||||
"project_end_date": assignmentType === 'Project' ? payload.endDate ? dayjs(payload.endDate).format('YYYY-MM-DD') : '' : '',
|
||||
"project_description": assignmentType === 'Project' ? payload.description : '',
|
||||
|
||||
"wo_code": (payload.workOrder as any).code,
|
||||
"wo_name": (payload.workOrder as any).name,
|
||||
"wo_code": assignmentType === 'Project' ? finalWoCode : '',
|
||||
"wo_name": (payload.workOrder as any)?.name || '',
|
||||
"wo_start_date": (payload.workOrder as any).startDate ? dayjs((payload.workOrder as any).startDate).format('YYYY-MM-DD') : '',
|
||||
"wo_end_date": (payload.workOrder as any).endDate ? dayjs((payload.workOrder as any).endDate).format('YYYY-MM-DD') : '',
|
||||
"wo_description": (payload.workOrder as any).description,
|
||||
@@ -301,7 +354,7 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign
|
||||
<FormField control={form.control} name="type" render={({ field }) => (
|
||||
<FormItem>
|
||||
<div className="grid md:grid-cols-2 gap-6">
|
||||
<Card onClick={() => field.onChange('Project')}
|
||||
<Card900 onClick={() => field.onChange('Project')}
|
||||
className={cn("cursor-pointer transition-all hover:border-his dark:hover:border-his duration-400", field.value === 'Project' && "ring-2 ring-his/10 border-his dark:ring-his/10 dark:border-his")}
|
||||
>
|
||||
<CardContent className="p-8 flex flex-col items-center text-center space-y-4">
|
||||
@@ -315,9 +368,9 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign
|
||||
</p>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</Card900>
|
||||
|
||||
<Card onClick={() => field.onChange('IM')}
|
||||
<Card900 onClick={() => field.onChange('IM')}
|
||||
className={cn("cursor-pointer transition-all hover:border-his dark:hover:border-his duration-400", field.value === 'IM' && "ring-2 ring-his/10 border-his dark:ring-his/10 dark:border-his")}
|
||||
>
|
||||
<CardContent className="p-8 flex flex-col items-center text-center space-y-4">
|
||||
@@ -331,7 +384,7 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign
|
||||
</p>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</Card900>
|
||||
</div>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
@@ -401,7 +454,7 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign
|
||||
renderOption={(option, isSelected) => {
|
||||
const user = option.originalData as IEndUser;
|
||||
return (
|
||||
<div className={`flex items-center justify-between w-full py-1 rounded-sm ${isSelected ? 'bg-zinc-800' : ''}`}>
|
||||
<div className={`flex items-center justify-between w-full py-1 rounded-sm ${isSelected ? 'bg-zinc-200/70 dark:bg-zinc-700/80' : ''}`}>
|
||||
<div className="flex items-center gap-3">
|
||||
<LazyImage className='w-10 h-10 rounded-md border border-border bg-muted/30 p-1 shrink-0' alt={user.name} src={user.photo || END_USER_FALLBACK_IMAGE} fallbackSrc={END_USER_FALLBACK_IMAGE} imgClassName="object-contain" />
|
||||
<div className="flex flex-col">
|
||||
@@ -435,65 +488,221 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign
|
||||
{/* STEP 3: DETAILS */}
|
||||
{step === 'details' && (
|
||||
<Card900>
|
||||
<CardHeader>
|
||||
<CardTitle>{assignmentType === 'IM' ? 'Internal Memo' : 'Project'} Details</CardTitle>
|
||||
<CardDescription>Provide the basic information for this assignment.</CardDescription>
|
||||
<CardHeader className="flex flex-col sm:flex-row sm:items-center justify-between gap-3 pb-4">
|
||||
<div>
|
||||
<CardTitle>{assignmentType === 'IM' ? 'Internal Memo' : 'Project'} Details</CardTitle>
|
||||
<CardDescription>Provide the basic information for this assignment.</CardDescription>
|
||||
</div>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="h-8 gap-1.5 text-xs text-muted-foreground hover:text-destructive hover:border-destructive/30 self-start sm:self-auto"
|
||||
onClick={handleClearProject}
|
||||
>
|
||||
<RotateCcw className="w-3.5 h-3.5" /> Clear Form
|
||||
</Button>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-6">
|
||||
{/* Existing Project Selector / Linked Hero Card */}
|
||||
{assignmentType === 'Project' && (
|
||||
!isExistProject ? (
|
||||
<div className="p-4 rounded-xl border border-border/80 bg-zinc-200/25 dark:bg-zinc-800/90 space-y-3">
|
||||
<div className="space-y-0.5">
|
||||
<div className="flex items-center gap-2">
|
||||
<FolderGit2 className="w-4 h-4 text-primary" />
|
||||
<span className="text-xs font-semibold text-foreground">Import Existing Project</span>
|
||||
<Badge variant="outline" className="text-[10px] font-normal py-0 h-5 border-border/70 text-muted-foreground">
|
||||
Optional
|
||||
</Badge>
|
||||
</div>
|
||||
<p className="text-[11px] text-muted-foreground">
|
||||
Select an existing project for {selectedClient?.name || 'this client'} to autofill information, or create a new one below.
|
||||
</p>
|
||||
</div>
|
||||
<Popover open={openProjectSearch} onOpenChange={setOpenProjectSearch}>
|
||||
<PopoverTrigger asChild>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
className="w-full h-10 px-3 justify-between bg-white dark:bg-zinc-950 border-input text-xs font-normal text-muted-foreground hover:text-foreground hover:bg-muted/40 transition-colors shadow-xs"
|
||||
>
|
||||
<span className="flex items-center gap-2 truncate">
|
||||
<Search className="w-3.5 h-3.5 text-muted-foreground shrink-0" />
|
||||
<span>Select an existing project from client directory...</span>
|
||||
</span>
|
||||
<ChevronDown className="w-4 h-4 opacity-50 shrink-0 ml-2" />
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="p-0 w-(--radix-popover-trigger-width) min-w-[320px] max-w-140" align="start">
|
||||
<Command>
|
||||
<CommandInput placeholder="Search existing projects by name or code..." className="h-9 text-xs" />
|
||||
<CommandList className="max-h-72">
|
||||
<CommandEmpty className="py-6 text-center text-xs text-muted-foreground">No existing projects found.</CommandEmpty>
|
||||
<CommandGroup heading="Existing Projects">
|
||||
{projectList?.map((p: IProject) => (
|
||||
<CommandItem
|
||||
key={p.id}
|
||||
value={`${p.name} ${p.code}`}
|
||||
onSelect={() => {
|
||||
form.setValue('name', p.name!, { shouldValidate: true });
|
||||
form.setValue('code', p.code!, { shouldValidate: true });
|
||||
form.setValue('year', String(p.year), { shouldValidate: true });
|
||||
form.setValue('startDate', new Date(p.start_date!), { shouldValidate: true });
|
||||
form.setValue('endDate', new Date(p.end_date!), { shouldValidate: true });
|
||||
form.setValue('description', p.description || '', { shouldValidate: true });
|
||||
|
||||
setOpenProjectSearch(false);
|
||||
setIsExistProject(true);
|
||||
toast.info(`Autofilled details from ${p.name}`);
|
||||
}}
|
||||
className="cursor-pointer py-2.5 px-3"
|
||||
>
|
||||
<div className="flex items-start gap-2.5 w-full min-w-0">
|
||||
<FolderGit2 className="w-4 h-4 text-primary shrink-0 mt-0.5" />
|
||||
<div className="flex flex-col min-w-0 flex-1">
|
||||
<span className="font-medium text-xs text-foreground leading-snug wrap-break-word">
|
||||
{p.name}
|
||||
</span>
|
||||
<div className="flex items-center gap-2 mt-1 text-[11px] text-muted-foreground font-mono">
|
||||
<span className="px-1.5 py-0.2 rounded bg-muted/60 dark:bg-zinc-800 text-[10px]">
|
||||
# {p.code}
|
||||
</span>
|
||||
<span>•</span>
|
||||
<span>Year: {p.year}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</CommandItem>
|
||||
))}
|
||||
</CommandGroup>
|
||||
</CommandList>
|
||||
</Command>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
</div>
|
||||
) : (
|
||||
<div className="p-4 rounded-xl border border-primary/30 bg-primary/3 dark:bg-primary/6 space-y-3 transition-all">
|
||||
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-3">
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<span className="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-semibold bg-emerald-500/10 text-emerald-600 dark:text-emerald-400 border border-emerald-500/25">
|
||||
<CheckCircle2 className="w-3.5 h-3.5" />
|
||||
Existing Project Linked
|
||||
</span>
|
||||
<span className="text-xs font-mono px-2 py-0.5 rounded bg-muted/60 dark:bg-zinc-800 text-muted-foreground border border-border/60">
|
||||
Code: <strong className="text-foreground"># {formValues.code}</strong>
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2 self-start sm:self-auto">
|
||||
<Popover open={openProjectSearch} onOpenChange={setOpenProjectSearch}>
|
||||
<PopoverTrigger asChild>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="h-7 px-2.5 text-xs bg-background dark:bg-zinc-950 hover:bg-muted/50"
|
||||
>
|
||||
Change Project
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="p-0 w-(--radix-popover-trigger-width) min-w-[320px] max-w-140" align="end">
|
||||
<Command>
|
||||
<CommandInput placeholder="Search existing projects by name or code..." className="h-9 text-xs" />
|
||||
<CommandList className="max-h-72">
|
||||
<CommandEmpty className="py-6 text-center text-xs text-muted-foreground">No existing projects found.</CommandEmpty>
|
||||
<CommandGroup heading="Existing Projects">
|
||||
{projectList?.map((p: IProject) => (
|
||||
<CommandItem
|
||||
key={p.id}
|
||||
value={`${p.name} ${p.code}`}
|
||||
onSelect={() => {
|
||||
form.setValue('name', p.name!, { shouldValidate: true });
|
||||
form.setValue('code', p.code!, { shouldValidate: true });
|
||||
form.setValue('year', String(p.year), { shouldValidate: true });
|
||||
form.setValue('startDate', new Date(p.start_date!), { shouldValidate: true });
|
||||
form.setValue('endDate', new Date(p.end_date!), { shouldValidate: true });
|
||||
form.setValue('description', p.description || '', { shouldValidate: true });
|
||||
|
||||
setOpenProjectSearch(false);
|
||||
setIsExistProject(true);
|
||||
toast.info(`Switched project to ${p.name}`);
|
||||
}}
|
||||
className="cursor-pointer py-2.5 px-3"
|
||||
>
|
||||
<div className="flex items-start gap-2.5 w-full min-w-0">
|
||||
<FolderGit2 className="w-4 h-4 text-primary shrink-0 mt-0.5" />
|
||||
<div className="flex flex-col min-w-0 flex-1">
|
||||
<span className="font-medium text-xs text-foreground leading-snug wrap-break-word">
|
||||
{p.name}
|
||||
</span>
|
||||
<div className="flex items-center gap-2 mt-1 text-[11px] text-muted-foreground font-mono">
|
||||
<span className="px-1.5 py-0.2 rounded bg-muted/60 dark:bg-zinc-800 text-[10px]">
|
||||
{p.code}
|
||||
</span>
|
||||
<span>•</span>
|
||||
<span>Year: {p.year}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</CommandItem>
|
||||
))}
|
||||
</CommandGroup>
|
||||
</CommandList>
|
||||
</Command>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-7 px-2 text-xs text-muted-foreground hover:text-destructive hover:bg-destructive/10"
|
||||
onClick={handleClearProject}
|
||||
>
|
||||
<X className="w-3.5 h-3.5 mr-1" /> Clear / Create New
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="pt-0.5">
|
||||
<h4 className="text-sm font-semibold text-foreground leading-snug wrap-break-word">
|
||||
{formValues.name}
|
||||
</h4>
|
||||
<div className="flex flex-wrap items-center gap-x-3 gap-y-1 mt-1.5 text-xs text-muted-foreground">
|
||||
<span>Year: <strong className="text-foreground font-medium">{formValues.year}</strong></span>
|
||||
{formValues.startDate && (
|
||||
<>
|
||||
<span>•</span>
|
||||
<span>
|
||||
Timeline:{" "}
|
||||
<strong className="text-foreground font-medium">
|
||||
{dayjs(formValues.startDate).format('DD MMM YYYY')} - {formValues.endDate ? dayjs(formValues.endDate).format('DD MMM YYYY') : 'Ongoing'}
|
||||
</strong>
|
||||
</span>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p className="text-[11px] text-muted-foreground border-t border-border/50 pt-2">
|
||||
Fields below are locked and autofilled from this existing project. Click <strong className="text-foreground font-medium">Clear / Create New</strong> to enter new project details manually.
|
||||
</p>
|
||||
</div>
|
||||
)
|
||||
)}
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="name"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{assignmentType === 'Project' ? "Project" : "Internal Memo"} Name</FormLabel>
|
||||
<div className="flex gap-2">
|
||||
<div className="flex-1 relative">
|
||||
<FormControl>
|
||||
<Input placeholder="e.g Software Development Sem 1 PO 4200049718..." {...field} disabled={isExistProject} />
|
||||
</FormControl>
|
||||
{assignmentType === '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-75" align="end">
|
||||
<Command>
|
||||
<CommandInput placeholder="Search existing projects..." />
|
||||
<CommandList>
|
||||
<CommandEmpty>No projects found.</CommandEmpty>
|
||||
<CommandGroup heading="Recent Projects">
|
||||
{projectList.map((p: IProject) => (
|
||||
<CommandItem
|
||||
key={p.id}
|
||||
onSelect={() => {
|
||||
form.setValue('name', p.name!, { shouldValidate: true });
|
||||
form.setValue('code', p.code!, { shouldValidate: true });
|
||||
form.setValue('year', String(p.year), { shouldValidate: true });
|
||||
form.setValue('startDate', new Date(p.start_date!), { shouldValidate: true });
|
||||
form.setValue('endDate', new Date(p.end_date!), { shouldValidate: true });
|
||||
form.setValue('description', p.description!, { shouldValidate: true });
|
||||
|
||||
setOpenProjectSearch(false);
|
||||
setIsExistProject(true)
|
||||
toast.info(`Autofilled details from ${p.name}`);
|
||||
}}
|
||||
>
|
||||
<Check className={cn("mr-2 h-4 w-4", field.value === p.name ? "opacity-100" : "opacity-0")} />
|
||||
{p?.name!}
|
||||
</CommandItem>
|
||||
))}
|
||||
</CommandGroup>
|
||||
</CommandList>
|
||||
</Command>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<FormControl>
|
||||
<Input
|
||||
placeholder={assignmentType === 'Project' ? "e.g Software Development Sem 1 PO 4200049718..." : "e.g Internal Memo Security Audit..."}
|
||||
{...field}
|
||||
disabled={isExistProject}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
@@ -533,70 +742,198 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign
|
||||
)} />
|
||||
</div>
|
||||
|
||||
<FormField control={form.control} name="description" disabled={isExistProject} render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Description</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea placeholder="Describe the scope..." className="min-h-30" {...field} value={field.value || ''} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)} />
|
||||
<FormField control={form.control} name="description" disabled={isExistProject} render={({ field }) => {
|
||||
const charCount = field.value?.length || 0;
|
||||
const maxLen = 500;
|
||||
return (
|
||||
<FormItem>
|
||||
<FormLabel>Description (Optional)</FormLabel>
|
||||
<FormControl>
|
||||
<InputGroup className="flex-col h-auto">
|
||||
<InputGroupTextarea
|
||||
placeholder="Describe the scope..."
|
||||
maxLength={maxLen}
|
||||
className="min-h-24 w-full"
|
||||
disabled={isExistProject}
|
||||
{...field}
|
||||
value={field.value || ''}
|
||||
/>
|
||||
<InputGroupAddon align="block-end" className="justify-start border-none py-1.5 px-3 w-full">
|
||||
<InputGroupText className="text-xs font-mono text-muted-foreground">
|
||||
{maxLen - charCount} characters left
|
||||
</InputGroupText>
|
||||
</InputGroupAddon>
|
||||
</InputGroup>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
);
|
||||
}} />
|
||||
</CardContent>
|
||||
</Card900>
|
||||
)}
|
||||
|
||||
{/* STEP 4: WORK ORDERS */}
|
||||
{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>
|
||||
<Card900>
|
||||
<CardContent className="p-6 space-y-4">
|
||||
<div className="grid gap-4 md:grid-cols-2">
|
||||
<FormField control={form.control} name={"workOrder.name" as any} render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>WO Name</FormLabel>
|
||||
<FormControl><Input placeholder="e.g. Initial Site Survey" {...field} value={field.value || ''} /></FormControl>
|
||||
<Card900>
|
||||
<CardHeader className="flex flex-col sm:flex-row sm:items-center justify-between gap-3 pb-4">
|
||||
<div>
|
||||
<CardTitle>Work Order Details</CardTitle>
|
||||
<CardDescription>Define the initial task for this project.</CardDescription>
|
||||
</div>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="h-8 gap-1.5 text-xs text-muted-foreground hover:text-destructive hover:border-destructive/30 self-start sm:self-auto"
|
||||
onClick={handleClearWorkOrder}
|
||||
>
|
||||
<RotateCcw className="w-3.5 h-3.5" /> Clear Form
|
||||
</Button>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-6">
|
||||
{/* Single column: Work Order Name */}
|
||||
<FormField
|
||||
control={form.control}
|
||||
name={"workOrder.name" as any}
|
||||
render={({ field }) => (
|
||||
<FormItem className="space-y-0.5">
|
||||
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-2">
|
||||
<FormLabel className="text-sm font-semibold text-foreground">
|
||||
WO Name
|
||||
</FormLabel>
|
||||
<label
|
||||
htmlFor="same-as-project"
|
||||
className="inline-flex items-center gap-2 text-xs font-medium cursor-pointer select-none text-muted-foreground hover:text-foreground transition-colors bg-transparent"
|
||||
>
|
||||
<Checkbox
|
||||
id="same-as-project"
|
||||
className="bg-transparent"
|
||||
checked={isSameAsProject}
|
||||
onCheckedChange={(checked) => {
|
||||
const isChecked = !!checked;
|
||||
setIsSameAsProject(isChecked);
|
||||
if (isChecked) {
|
||||
form.setValue('workOrder.name' as any, formValues.name || '', { shouldValidate: true });
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<span>Same as Project Name</span>
|
||||
</label>
|
||||
</div>
|
||||
<FormControl>
|
||||
<Input
|
||||
placeholder="e.g. Initial Site Survey"
|
||||
{...field}
|
||||
value={field.value || ''}
|
||||
disabled={isSameAsProject}
|
||||
/>
|
||||
</FormControl>
|
||||
{isSameAsProject && (
|
||||
<p className="text-[11px] text-emerald-600 dark:text-emerald-400 flex items-center gap-1.5">
|
||||
<Check className="w-3.5 h-3.5" />
|
||||
Synchronized with project name
|
||||
</p>
|
||||
)}
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
{/* Single column: Work Order Code with cohesive InputGroup */}
|
||||
<FormField
|
||||
control={form.control}
|
||||
name={"workOrder.code" as any}
|
||||
render={({ field }) => {
|
||||
const projectCode = formValues.code?.trim() || 'PRJ';
|
||||
return (
|
||||
<FormItem className="space-y-2">
|
||||
<div className="flex items-center justify-between">
|
||||
<FormLabel className="text-sm font-semibold text-foreground">
|
||||
WO Code
|
||||
</FormLabel>
|
||||
<span className="text-[11px] text-muted-foreground font-mono">
|
||||
Prefix: <strong className="text-foreground">{projectCode}-</strong>
|
||||
</span>
|
||||
</div>
|
||||
<FormControl>
|
||||
<InputGroup>
|
||||
<InputGroupAddon align="inline-start">
|
||||
{projectCode}-
|
||||
</InputGroupAddon>
|
||||
<InputGroupInput
|
||||
placeholder="e.g. 01, 65454, or WO-01"
|
||||
{...field}
|
||||
value={field.value || ''}
|
||||
onChange={(e) => {
|
||||
let val = e.target.value;
|
||||
if (val.startsWith(`${projectCode}-`)) {
|
||||
val = val.slice(projectCode.length + 1);
|
||||
}
|
||||
field.onChange(val);
|
||||
}}
|
||||
/>
|
||||
</InputGroup>
|
||||
</FormControl>
|
||||
<p className="text-[11px] text-muted-foreground flex items-center gap-1.5">
|
||||
<span>Full work order code will be submitted as:</span>
|
||||
<span className="font-mono font-semibold text-foreground px-1.5 py-0.5 rounded bg-muted/60 dark:bg-zinc-900 border border-border/60">
|
||||
{projectCode}-{field.value?.trim() || '...'}
|
||||
</span>
|
||||
</p>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)} />
|
||||
<FormField control={form.control} name={"workOrder.code" as any} render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>WO Code</FormLabel>
|
||||
<FormControl><Input placeholder="e.g. WO-24016" {...field} value={field.value || ''} /></FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)} />
|
||||
</div>
|
||||
<div className="grid gap-4 md:grid-cols-2">
|
||||
<FormField control={form.control} name={"workOrder.startDate" as any} render={({ field }) => (
|
||||
<FormItem className="flex flex-col">
|
||||
<FormLabel>WO Start Date</FormLabel>
|
||||
<DatePicker date={field.value as Date | undefined} setDate={field.onChange} />
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)} />
|
||||
<FormField control={form.control} name={"workOrder.endDate" as any} render={({ field }) => (
|
||||
<FormItem className="flex flex-col">
|
||||
<FormLabel>WO End Date</FormLabel>
|
||||
<DatePicker date={field.value as Date | undefined} setDate={field.onChange} />
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)} />
|
||||
</div>
|
||||
<FormField control={form.control} name={"workOrder.description" as any} render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>WO Description</FormLabel>
|
||||
<FormControl><Textarea placeholder="Briefly describe the task.." className="min-h-30" {...field} value={field.value || ''} /></FormControl>
|
||||
);
|
||||
}}
|
||||
/>
|
||||
|
||||
{/* Dates (Start and End) */}
|
||||
<div className="grid gap-4 md:grid-cols-2">
|
||||
<FormField control={form.control} name={"workOrder.startDate" as any} render={({ field }) => (
|
||||
<FormItem className="flex flex-col">
|
||||
<FormLabel>WO Start Date</FormLabel>
|
||||
<DatePicker date={field.value as Date | undefined} setDate={field.onChange} />
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)} />
|
||||
</CardContent>
|
||||
</Card900>
|
||||
</div>
|
||||
<FormField control={form.control} name={"workOrder.endDate" as any} render={({ field }) => (
|
||||
<FormItem className="flex flex-col">
|
||||
<FormLabel>WO End Date</FormLabel>
|
||||
<DatePicker date={field.value as Date | undefined} setDate={field.onChange} />
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)} />
|
||||
</div>
|
||||
|
||||
{/* Description with character counter */}
|
||||
<FormField control={form.control} name={"workOrder.description" as any} render={({ field }) => {
|
||||
const charCount = field.value?.length || 0;
|
||||
const maxLen = 500;
|
||||
return (
|
||||
<FormItem>
|
||||
<FormLabel>WO Description (Optional)</FormLabel>
|
||||
<FormControl>
|
||||
<InputGroup className="flex-col h-auto">
|
||||
<InputGroupTextarea
|
||||
placeholder="Briefly describe the task..."
|
||||
maxLength={maxLen}
|
||||
className="min-h-24 w-full"
|
||||
{...field}
|
||||
value={field.value || ''}
|
||||
/>
|
||||
<InputGroupAddon align="block-end" className="justify-start border-none py-1.5 px-3 w-full">
|
||||
<InputGroupText className="text-xs font-mono text-muted-foreground">
|
||||
{maxLen - charCount} characters left
|
||||
</InputGroupText>
|
||||
</InputGroupAddon>
|
||||
</InputGroup>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
);
|
||||
}} />
|
||||
</CardContent>
|
||||
</Card900>
|
||||
)}
|
||||
|
||||
{/* STEP 5: REVIEW */}
|
||||
@@ -608,7 +945,7 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign
|
||||
</div>
|
||||
|
||||
<div className="grid gap-6">
|
||||
<Card className="overflow-hidden">
|
||||
<Card900 className="overflow-hidden">
|
||||
<div className="bg-primary/5 px-6 py-4 border-b flex justify-between items-center">
|
||||
<CardTitle className="text-lg">Project Assignment Overview</CardTitle>
|
||||
<Badge variant={formValues.type === 'IM' ? 'info' : 'default'} className="px-3">{formValues.type}</Badge>
|
||||
@@ -682,42 +1019,84 @@ export default function ProjectAssignmentForm({ client, clientId, isEdit, assign
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{(formValues.workOrder as any)?.name && (
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center gap-2">
|
||||
<Separator className="flex-1" />
|
||||
<Label className="text-[10px] text-muted-foreground uppercase tracking-widest font-bold px-2">Work Order Detail</Label>
|
||||
<Separator className="flex-1" />
|
||||
</div>
|
||||
<div className="bg-primary/5 rounded-xl border border-primary/10 overflow-hidden">
|
||||
<div className="p-4 flex flex-col md:flex-row md:items-center justify-between gap-4">
|
||||
<div className="flex items-start gap-3">
|
||||
<div className="w-8 h-8 rounded-full bg-primary/20 text-primary flex items-center justify-center shrink-0 mt-1">
|
||||
<Building2 className="w-4 h-4" />
|
||||
{formValues.type === 'Project' && (formValues.workOrder as any)?.name && (() => {
|
||||
const rawWoCode = ((formValues.workOrder as any)?.code || '').trim();
|
||||
const prjCode = formValues.code?.trim() || '';
|
||||
const fullWoCode = rawWoCode
|
||||
? (prjCode && !rawWoCode.startsWith(`${prjCode}-`) ? `${prjCode}-${rawWoCode}` : rawWoCode)
|
||||
: '-';
|
||||
const woStart = (formValues.workOrder as any)?.startDate
|
||||
? dayjs((formValues.workOrder as any).startDate).format('DD MMM YYYY')
|
||||
: null;
|
||||
const woEnd = (formValues.workOrder as any)?.endDate
|
||||
? dayjs((formValues.workOrder as any).endDate).format('DD MMM YYYY')
|
||||
: null;
|
||||
const scheduleStr = woStart && woEnd
|
||||
? `${woStart} - ${woEnd}`
|
||||
: woStart
|
||||
? `From ${woStart}`
|
||||
: 'Not specified';
|
||||
|
||||
return (
|
||||
<div className="space-y-3 pt-2">
|
||||
<div className="flex items-center gap-2">
|
||||
<Briefcase className="w-4 h-4 text-primary" />
|
||||
<span className="text-xs font-semibold uppercase tracking-wider text-muted-foreground">
|
||||
Work Order Detail
|
||||
</span>
|
||||
<Separator className="flex-1" />
|
||||
<Badge variant="outline" className="text-[10px] font-normal py-0 h-5 border-border/70 text-muted-foreground">
|
||||
Initial Assignment
|
||||
</Badge>
|
||||
</div>
|
||||
|
||||
<div className="rounded-xl border border-border/70 bg-card/60 dark:bg-zinc-900/40 overflow-hidden divide-y divide-border/50">
|
||||
{/* Top Section: Badges, Schedule & Title */}
|
||||
<div className="p-4 sm:p-5 space-y-3">
|
||||
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-2.5">
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<span className="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-medium bg-primary/10 text-primary border border-primary/20">
|
||||
<CheckCircle2 className="w-3.5 h-3.5" />
|
||||
Work Order
|
||||
</span>
|
||||
<span className="inline-flex items-center text-xs font-mono font-medium px-2.5 py-0.5 rounded-md bg-muted/60 dark:bg-zinc-800 text-foreground border border-border/60">
|
||||
Code: <strong className="ml-1 text-primary">{fullWoCode}</strong>
|
||||
</span>
|
||||
</div>
|
||||
<div className="inline-flex items-center gap-1.5 text-xs text-muted-foreground bg-muted/40 dark:bg-zinc-800/60 px-2.5 py-1 rounded-md border border-border/50 w-fit">
|
||||
<Clock className="w-3.5 h-3.5 text-muted-foreground shrink-0" />
|
||||
<span>Schedule:</span>
|
||||
<span className="font-medium text-foreground">{scheduleStr}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="space-y-1">
|
||||
<p className="font-bold text-primary">{(formValues.workOrder as any).name}</p>
|
||||
<p className="text-xs text-muted-foreground line-clamp-2">{(formValues.workOrder as any).description}</p>
|
||||
|
||||
<div>
|
||||
<Label className="text-[10px] text-muted-foreground uppercase tracking-wider font-semibold">
|
||||
WO Name
|
||||
</Label>
|
||||
<h4 className="text-base sm:text-lg font-bold text-foreground leading-snug mt-0.5 wrap-break-word">
|
||||
{(formValues.workOrder as any).name}
|
||||
</h4>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex gap-4 text-right">
|
||||
<div className="space-y-1">
|
||||
<Label className="text-[9px] text-muted-foreground uppercase tracking-wider">WO Code</Label>
|
||||
<p className="text-xs font-mono">{(formValues.workOrder as any).code || '-'}</p>
|
||||
</div>
|
||||
<div className="space-y-1">
|
||||
<Label className="text-[9px] text-muted-foreground uppercase tracking-wider">Schedule</Label>
|
||||
<p className="text-[10px] font-medium leading-none">
|
||||
{(formValues.workOrder as any).startDate ? new Date((formValues.workOrder as any).startDate).toLocaleDateString('en-US', { month: 'short', day: 'numeric' }) : '-'} — {(formValues.workOrder as any).endDate ? new Date((formValues.workOrder as any).endDate).toLocaleDateString('en-US', { month: 'short', day: 'numeric' }) : '-'}
|
||||
|
||||
{/* Bottom Section: Description (if available) */}
|
||||
{(formValues.workOrder as any).description && (
|
||||
<div className="p-4 sm:p-5 bg-muted/10 dark:bg-zinc-950/30 space-y-1">
|
||||
<Label className="text-[10px] text-muted-foreground uppercase tracking-wider font-semibold">
|
||||
WO Description
|
||||
</Label>
|
||||
<p className="text-xs sm:text-sm text-muted-foreground leading-relaxed whitespace-pre-wrap wrap-break-word">
|
||||
{(formValues.workOrder as any).description}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
);
|
||||
})()}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</Card900>
|
||||
</div>
|
||||
<div className="flex items-center justify-center gap-2 text-sm text-muted-foreground">
|
||||
<CheckCircle2 className="w-4 h-4 text-green-500" />
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
export default function Loading() {
|
||||
return (
|
||||
<div
|
||||
className="max-w-4xl mx-auto p-6 sm:p-8 pt-6 pb-28 space-y-8"
|
||||
role="status"
|
||||
aria-label="Loading project assignment form"
|
||||
>
|
||||
{/* Header: Back link and Title Skeleton */}
|
||||
<div className="space-y-3">
|
||||
<div className="h-4 w-36 animate-pulse rounded bg-muted/60" />
|
||||
<div className="space-y-1.5">
|
||||
<div className="h-8 w-64 animate-pulse rounded-lg bg-muted" />
|
||||
<div className="h-4 w-96 max-w-full animate-pulse rounded bg-muted/60" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Stepper Progress Skeleton */}
|
||||
<div className="space-y-4 rounded-2xl border border-border/70 bg-card/40 dark:bg-zinc-900/40 p-4 sm:p-6 shadow-2xs">
|
||||
{/* Progress bar line */}
|
||||
<div className="h-1.5 w-full rounded-full bg-muted/50 overflow-hidden">
|
||||
<div className="h-full w-1/5 animate-pulse rounded-full bg-primary/40" />
|
||||
</div>
|
||||
|
||||
{/* Step Circles & Labels */}
|
||||
<div className="flex items-center justify-between gap-2 px-1">
|
||||
{Array.from({ length: 5 }).map((_, i) => (
|
||||
<div key={i} className="flex flex-col items-center gap-2">
|
||||
<div className="h-8 w-8 sm:h-9 sm:w-9 animate-pulse rounded-full bg-muted shrink-0" />
|
||||
<div className="h-3 w-12 sm:w-16 animate-pulse rounded bg-muted/60 hidden sm:block" />
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Main Form Content Skeleton (Card900 style) */}
|
||||
<div className="rounded-2xl border border-border/80 bg-card/60 dark:bg-zinc-900/60 p-6 sm:p-8 space-y-6 shadow-2xs">
|
||||
{/* Card Title & Action button */}
|
||||
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-3 pb-2 border-b border-border/40">
|
||||
<div className="space-y-1.5">
|
||||
<div className="h-6 w-44 animate-pulse rounded-lg bg-muted" />
|
||||
<div className="h-4 w-72 max-w-full animate-pulse rounded bg-muted/60" />
|
||||
</div>
|
||||
<div className="h-8 w-24 animate-pulse rounded-md bg-muted/50 self-start sm:self-auto" />
|
||||
</div>
|
||||
|
||||
{/* 2-Col Selection / Input fields placeholder */}
|
||||
<div className="grid md:grid-cols-2 gap-6 pt-2">
|
||||
<div className="rounded-xl border border-border/70 bg-muted/20 dark:bg-zinc-900/30 p-6 space-y-4">
|
||||
<div className="h-10 w-10 animate-pulse rounded-xl bg-muted" />
|
||||
<div className="space-y-2">
|
||||
<div className="h-5 w-32 animate-pulse rounded bg-muted" />
|
||||
<div className="h-4 w-full animate-pulse rounded bg-muted/60" />
|
||||
<div className="h-4 w-3/4 animate-pulse rounded bg-muted/60" />
|
||||
</div>
|
||||
</div>
|
||||
<div className="rounded-xl border border-border/70 bg-muted/20 dark:bg-zinc-900/30 p-6 space-y-4">
|
||||
<div className="h-10 w-10 animate-pulse rounded-xl bg-muted" />
|
||||
<div className="space-y-2">
|
||||
<div className="h-5 w-32 animate-pulse rounded bg-muted" />
|
||||
<div className="h-4 w-full animate-pulse rounded bg-muted/60" />
|
||||
<div className="h-4 w-3/4 animate-pulse rounded bg-muted/60" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Bottom Navigation Sticky Bar Skeleton */}
|
||||
<div className="fixed bottom-0 left-0 right-0 p-4 sm:p-6 bg-background/80 backdrop-blur-md border-t border-border/60 flex justify-between items-center z-50">
|
||||
<div className="h-9 w-20 animate-pulse rounded-md bg-muted/60" />
|
||||
<div className="h-9 w-24 animate-pulse rounded-md bg-primary/40" />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user