Files
hats-app/app/(dashboard)/task/component/UpdateTimelineStatus.tsx
T
Ichsan Kurniawan 200ef0571c Fix fallback images and timeline dates
Updated project assignment forms to use shared client/end-user fallback images and improve avatar presentation in selection lists and detail panels. Also normalized timeline and team date payloads to send YYYY-MM-DD values instead of ISO timestamps, and default missing project years to the current year.
2026-10-07 12:16:52 +07:00

199 lines
10 KiB
TypeScript

import { useState, type FC } from 'react';
import {
AlertDialog, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle
} from "@/components/ui/alert-dialog"
import { Button } from '@/components/ui/button';
import { DatePicker } from '@/components/ui/datepicker';
import { Edit2, Loader2, Save, X } from 'lucide-react';
import { useFetchWithAxios } from '@/lib/axios/useFetch';
import { Skeleton } from '@/components/ui/skeleton';
import { serverApiCall } from '@/lib/server-api';
import { toast } from 'sonner';
import { useRouter } from 'next/navigation';
import {
Table, TableBody, TableCell, TableHead, TableHeader, TableRow
} from "@/components/ui/table";
import BadgeStatus from '@/components/ui/badge-status';
import dayjs from 'dayjs';
export interface IProjectAssignmentTimeline {
id: number
end_date: any
start_date: any
project_assignment_status: ProjectAssignmentStatus
}
export interface ProjectAssignmentStatus {
id: number
name: string
remark: any
}
type Props = {
assignmentId: string | number
onClose?: VoidFunction
};
const UpdateTimelineStatus: FC<Props> = ({ assignmentId, onClose }) => {
const [editingId, setEditingId] = useState<number | null>(null);
const [draftData, setDraftData] = useState<any>({});
const [isSaving, setIsSaving] = useState(false);
const { data: existTimeline, isLoading: isLoadingTimeline, mutate } = useFetchWithAxios(`project-assignment/status-detail/${assignmentId}`)
const router = useRouter()
const handleEdit = (row: IProjectAssignmentTimeline) => {
setEditingId(row.id);
setDraftData({
detailId: Number(row.id),
masterId: Number(row.project_assignment_status.id),
start_date_assign: row.start_date,
end_date_assign: row.end_date,
});
};
const handleSave = async () => {
setIsSaving(true);
const submitPayload = {
"project_assignment_status_detail_id": Number(draftData.detailId),
"project_assignment_status_id": Number(draftData.masterId),
"start_date": draftData.start_date_assign ? dayjs(draftData.start_date_assign).format("YYYY-MM-DD") : '',
"end_date": draftData.end_date_assign ? dayjs(draftData.end_date_assign).format("YYYY-MM-DD") : '',
"note": draftData.note,
"remarks": draftData.note
}
console.log(submitPayload)
const res = await serverApiCall('PUT', `/project-assignment/status/${assignmentId}`, submitPayload)
setIsSaving(false);
if (res.success) {
toast.success('Status updated successfully')
handleSuccess!()
} else {
toast.error(`Fail to save status`, { description: `(${res.code || '-'}) ${res.message}` })
}
};
const handleDraftChange = (field: any, value: string) => {
setDraftData((prev: any) => ({ ...prev, [field]: value }));
};
const handleCancel = () => {
setEditingId(null);
setDraftData({});
};
const handleSuccess = () => {
handleCancel()
mutate()
router.refresh()
}
return (
<>
<AlertDialog open={true} >
<AlertDialogContent className="min-w-full md:min-w-max max-h-[90vh] overflow-y-auto">
<AlertDialogHeader>
<AlertDialogTitle>Update Status</AlertDialogTitle>
<AlertDialogDescription className='w-full sm:max-w-140'>
Update the timeline status for this assignment. You can also add a note or remark regarding this status change. This will help to keep track of the progress.
</AlertDialogDescription>
</AlertDialogHeader>
<div className="rounded-md border bg-card overflow-x-auto my-3">
<Table>
<TableHeader>
<TableRow>
<TableHead>Status</TableHead>
<TableHead>Start Date</TableHead>
<TableHead>End Date</TableHead>
<TableHead className="text-right">Action</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{isLoadingTimeline ?
<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>
:
existTimeline.length === 0 ?
<TableRow>
<TableCell colSpan={5} className="w-full h-32">
<div className="text-center text-muted-foreground flex flex-col items-center justify-center gap-2">
No team members have been added yet.<br />Create a team to get started with project assignments
</div>
</TableCell>
</TableRow>
:
existTimeline.sort((a: any, b: any) => a.id - b.id).map((row: IProjectAssignmentTimeline) => {
const isEditing = editingId === row.id;
const isOtherRowEditing = editingId !== null && editingId !== row.id;
return (
<TableRow key={row.id}>
<TableCell>
<BadgeStatus status={row.project_assignment_status.name} />
</TableCell>
<TableCell>
{isEditing ?
<DatePicker date={draftData.start_date_assign as Date | undefined} setDate={(date) => handleDraftChange("start_date_assign", date as any)} />
:
<span className="text-sm">{row.start_date ? dayjs(row.start_date).format('MMM DD, YYYY') : null}</span>
}
</TableCell>
<TableCell>
{isEditing ?
<DatePicker date={draftData.end_date_assign as Date | undefined} setDate={(date) => handleDraftChange("end_date_assign", date as any)} />
:
<span className="text-sm">{row.end_date ? dayjs(row.end_date).format('MMM DD, YYYY') : null}</span>
}
</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>
}
</div>
</TableCell>
</TableRow>
);
})
}
</TableBody>
</Table>
</div>
<AlertDialogFooter>
<AlertDialogCancel onClick={onClose} disabled={isSaving}>Close</AlertDialogCancel>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</>
);
}
export default UpdateTimelineStatus;