mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 11:06:29 +00:00
d4168b45f2
Gate assignment and team actions by permissions, limit client and end-user images to 2 MB, and make the technical status end date optional. Also update status remark handling and rename the Task navigation label.
267 lines
12 KiB
TypeScript
267 lines
12 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useEffect, useMemo, useTransition } from 'react';
|
|
import { useRouter } from 'next/navigation';
|
|
import { IProjectAssignment } from '../type.assignment';
|
|
import { useFetchWithAxios } from '@/lib/axios/useFetch';
|
|
import { serverApiCall } from '@/lib/server-api';
|
|
import { toast } from 'sonner';
|
|
import {
|
|
Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle,
|
|
} from '@/components/ui/dialog';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Label } from '@/components/ui/label';
|
|
import { Textarea } from '@/components/ui/textarea';
|
|
import {
|
|
Select, SelectContent, SelectItem, SelectTrigger, SelectValue,
|
|
} from '@/components/ui/select';
|
|
import BadgeStatus from '@/components/ui/badge-status';
|
|
import { ClipboardCheck, Loader2 } from 'lucide-react';
|
|
|
|
export interface IMasterStatus {
|
|
id: number;
|
|
name: string;
|
|
description?: string | null;
|
|
remark?: string | null;
|
|
type?: string;
|
|
}
|
|
|
|
|
|
interface UpdateAdministrationStatusProps {
|
|
assignmentId?: number;
|
|
assignment?: IProjectAssignment;
|
|
onClose: () => void;
|
|
onSuccess?: () => void;
|
|
}
|
|
|
|
export default function UpdateAdministrationStatus({
|
|
assignmentId: propAssignmentId,
|
|
assignment,
|
|
onClose,
|
|
onSuccess,
|
|
}: UpdateAdministrationStatusProps) {
|
|
const router = useRouter();
|
|
const [isPending, startTransition] = useTransition();
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
const effectiveId = propAssignmentId ?? assignment?.id;
|
|
const assignmentName = assignment?.wo_name || assignment?.im_name || assignment?.project?.name || (effectiveId ? `Assignment #${effectiveId}` : 'Project Assignment');
|
|
const assignmentCode = assignment?.wo_code || assignment?.im_code || assignment?.project?.code;
|
|
|
|
const { data: fetchedStatuses, isLoading: isLoadingStatuses } = useFetchWithAxios('/project-assignment-status?type=master');
|
|
|
|
const statusList: IMasterStatus[] = Array.isArray(fetchedStatuses) && fetchedStatuses.length > 0 ? fetchedStatuses : [];
|
|
|
|
const initialStatusId = useMemo(() => {
|
|
if (assignment?.project_assignment_status?.id) {
|
|
return String(assignment.project_assignment_status.id);
|
|
}
|
|
|
|
if ((assignment as any)?.project_assignment_status_id) {
|
|
return String((assignment as any).project_assignment_status_id);
|
|
}
|
|
|
|
const currentName = assignment?.project_assignment_status?.name || (assignment as any)?.status?.name || (assignment as any)?.status;
|
|
|
|
if (currentName && statusList.length > 0) {
|
|
const findStatus = statusList.find((s) => s.name?.toLowerCase().trim() === String(currentName).toLowerCase().trim());
|
|
if (findStatus) return String(findStatus.id);
|
|
}
|
|
|
|
if (statusList.length > 0 && statusList[0]?.id) {
|
|
return String(statusList[0].id);
|
|
}
|
|
|
|
return '';
|
|
}, [assignment, statusList]);
|
|
|
|
const [selectedStatusId, setSelectedStatusId] = useState<string>(initialStatusId);
|
|
const [description, setDescription] = useState<string>(assignment?.project_assignment_status?.remark || assignment?.note || '');
|
|
|
|
useEffect(() => {
|
|
if (initialStatusId) {
|
|
setSelectedStatusId(initialStatusId);
|
|
}
|
|
}, [initialStatusId]);
|
|
|
|
useEffect(() => {
|
|
const initialRemark = assignment?.project_assignment_status_notes || assignment?.project_assignment_status?.remark || assignment?.note || '';
|
|
setDescription(initialRemark);
|
|
}, [assignment]);
|
|
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
if (!effectiveId) {
|
|
toast.error('Invalid assignment identifier');
|
|
return;
|
|
}
|
|
|
|
if (!selectedStatusId) {
|
|
toast.error('Please select an administrative status');
|
|
return;
|
|
}
|
|
|
|
setIsSubmitting(true);
|
|
try {
|
|
const payload = {
|
|
project_assignment_status_id: Number(selectedStatusId),
|
|
notes: description.trim(),
|
|
};
|
|
|
|
const res = await serverApiCall('PUT', `/project-assignment/status/${effectiveId}`, payload);
|
|
|
|
if (res.success) {
|
|
toast.success('Administration status updated successfully');
|
|
startTransition(() => {
|
|
router.refresh();
|
|
});
|
|
onSuccess?.();
|
|
onClose();
|
|
} else {
|
|
toast.error('Failed to update administration status', {
|
|
description: res.message || 'An unexpected error occurred.',
|
|
});
|
|
}
|
|
} catch (err: any) {
|
|
toast.error('An error occurred while saving status', {
|
|
description: err?.message || 'Please try again later.',
|
|
});
|
|
} finally {
|
|
setIsSubmitting(false);
|
|
}
|
|
};
|
|
|
|
const isBusy = isSubmitting || isPending;
|
|
|
|
return (
|
|
<Dialog open onOpenChange={(open) => !open && !isBusy && onClose()}>
|
|
<DialogContent className="sm:max-w-120 p-0 overflow-hidden">
|
|
{/* Modal Header */}
|
|
<div className="border-b border-border/70 bg-muted/40 px-6 py-5">
|
|
<DialogHeader className="space-y-1.5 text-left">
|
|
<DialogTitle className="flex items-center gap-2">
|
|
<ClipboardCheck className="h-5 w-5 text-his" />
|
|
Update Administration Status
|
|
</DialogTitle>
|
|
<DialogDescription className="text-muted-foreground">
|
|
Update the overarching lifecycle stage and audit notes for{' '}
|
|
<span className="font-semibold text-foreground">
|
|
{assignmentName}
|
|
</span>
|
|
{assignmentCode &&
|
|
<span className="ml-1 font-mono text-muted-foreground">
|
|
(# {assignmentCode})
|
|
</span>
|
|
}
|
|
.
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
</div>
|
|
|
|
{/* Form Body */}
|
|
<form onSubmit={handleSubmit} className="space-y-5 px-6 pb-5 pt-2">
|
|
{/* Current Status Reference Card */}
|
|
{assignment?.project_assignment_status?.name && (
|
|
<div className="flex items-center justify-between rounded-lg border border-border/60 bg-muted/30 px-3.5 py-2.5 text-xs">
|
|
<span className="text-muted-foreground font-medium">Current Status</span>
|
|
<BadgeStatus status={assignment.project_assignment_status.name} />
|
|
</div>
|
|
)}
|
|
|
|
{/* Status Select Field */}
|
|
<div className="space-y-2">
|
|
<div className="flex items-center justify-between">
|
|
<Label htmlFor="admin-status-select" className="text-xs font-semibold">
|
|
New Administration Status <span className="text-destructive">*</span>
|
|
</Label>
|
|
{isLoadingStatuses && (
|
|
<span className="text-[11px] text-muted-foreground flex items-center gap-1">
|
|
<Loader2 className="size-3 animate-spin" /> Loading options...
|
|
</span>
|
|
)}
|
|
</div>
|
|
|
|
<Select
|
|
value={selectedStatusId}
|
|
onValueChange={setSelectedStatusId}
|
|
disabled={isBusy}
|
|
>
|
|
<SelectTrigger
|
|
id="admin-status-select"
|
|
className="w-full h-10 text-sm"
|
|
>
|
|
<SelectValue placeholder="Select administrative status" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{statusList.map((status) => (
|
|
<SelectItem
|
|
key={status.id}
|
|
value={String(status.id)}
|
|
className="py-2 cursor-pointer"
|
|
>
|
|
<div className="flex items-center justify-between gap-4 w-full">
|
|
<span className="font-medium text-foreground">{status.name}</span>
|
|
<BadgeStatus status={status.name} />
|
|
</div>
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
|
|
{/* Description / Notes Field */}
|
|
<div className="space-y-2">
|
|
<div className="flex items-center justify-between">
|
|
<Label htmlFor="admin-status-description" className="text-xs font-semibold">
|
|
Notes / Description
|
|
</Label>
|
|
<span className="text-[11px] text-muted-foreground">
|
|
{description.length} / 500
|
|
</span>
|
|
</div>
|
|
<Textarea
|
|
id="admin-status-description"
|
|
value={description}
|
|
onChange={(e) => setDescription(e.target.value.slice(0, 500))}
|
|
placeholder="Add reason for status change, audit remarks, or handover notes..."
|
|
className="min-h-26.25 text-sm leading-relaxed"
|
|
disabled={isBusy}
|
|
/>
|
|
<p className="text-[11px] text-muted-foreground leading-normal">
|
|
This description will be recorded as audit trail notes for this assignment.
|
|
</p>
|
|
</div>
|
|
|
|
{/* Modal Footer */}
|
|
<DialogFooter className="gap-2 sm:gap-2 pt-2 border-t border-border/60">
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={onClose}
|
|
disabled={isBusy}
|
|
className="w-full sm:w-auto"
|
|
>
|
|
Cancel
|
|
</Button>
|
|
<Button
|
|
type="submit"
|
|
disabled={isBusy || !selectedStatusId}
|
|
className="w-full sm:w-auto bg-his text-white hover:bg-his/90 gap-1.5 shadow-sm font-medium"
|
|
>
|
|
{isBusy ? (
|
|
<>
|
|
<Loader2 className="size-3.5 animate-spin" />
|
|
Updating...
|
|
</>
|
|
) : (
|
|
'Save Status'
|
|
)}
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|