From f07990a3fbb2c354fdeaaae93201a0db500c50ca Mon Sep 17 00:00:00 2001 From: Ichsan Kurniawan Date: Fri, 9 Oct 2026 18:37:26 +0700 Subject: [PATCH] Refactor technical status workflow Replace the legacy timeline status modal with a technical-status editor that supports multiple date intervals and descriptive notes. Update task views to use the new Update Technical Status action, normalize status-history parsing for array-based date records, and improve badge formatting for technical stages like SIT, UAT, PTR, Go-Live, and BAST. --- .../(main)/ClientDetailComponent.tsx | 4 +- .../AssignmentDetailComponent.tsx | 17 +- .../[client_id]/[assignment_id]/page.tsx | 2 + .../[project_id]/StatusTimeline.tsx | 38 +- .../[project_id]/WorkOrderCard.tsx | 6 +- .../task/component/UpdateTechnicalStatus.tsx | 571 ++++++++++++++++++ .../task/component/UpdateTimelineStatus.tsx | 199 ------ .../project-assignment/AssignmentGrid.tsx | 2 +- .../project-assignment/AssignmentList.tsx | 2 +- app/(dashboard)/task/type.assignment.ts | 9 +- components/ui/badge-status.tsx | 13 +- components/ui/badge.tsx | 25 +- 12 files changed, 660 insertions(+), 228 deletions(-) create mode 100644 app/(dashboard)/task/component/UpdateTechnicalStatus.tsx delete mode 100644 app/(dashboard)/task/component/UpdateTimelineStatus.tsx diff --git a/app/(dashboard)/task/client-detail/[client_id]/(main)/ClientDetailComponent.tsx b/app/(dashboard)/task/client-detail/[client_id]/(main)/ClientDetailComponent.tsx index 6aca41f..9723fc1 100644 --- a/app/(dashboard)/task/client-detail/[client_id]/(main)/ClientDetailComponent.tsx +++ b/app/(dashboard)/task/client-detail/[client_id]/(main)/ClientDetailComponent.tsx @@ -10,7 +10,7 @@ import { IProjectAssignment } from '../../../type.assignment'; import { useRouter, useSearchParams } from 'next/navigation'; import FilterComponent from '../../../component/FilterComponent'; import { ConfirmDeleteWithInput } from '@/components/modal/ConfirmDeleteWithInput'; -import UpdateTimelineStatus from '../../../component/UpdateTimelineStatus'; +import UpdateTechnicalStatus from '../../../component/UpdateTechnicalStatus'; import { ManageTeam } from '../../../component/ManageTeam'; import AssignmentGrid from '../../../component/project-assignment/AssignmentGrid'; import AssignmentList from '../../../component/project-assignment/AssignmentList'; @@ -113,7 +113,7 @@ export default function ClientDetailComponent({ clientId, assignmentLists }: Pro ) : null} {action === 'UPDATE_STATUS' && selectedData ? ( - + ) : null} {action === 'DELETE' && selectedData ? ( diff --git a/app/(dashboard)/task/client-detail/[client_id]/[assignment_id]/AssignmentDetailComponent.tsx b/app/(dashboard)/task/client-detail/[client_id]/[assignment_id]/AssignmentDetailComponent.tsx index 484b515..7446397 100644 --- a/app/(dashboard)/task/client-detail/[client_id]/[assignment_id]/AssignmentDetailComponent.tsx +++ b/app/(dashboard)/task/client-detail/[client_id]/[assignment_id]/AssignmentDetailComponent.tsx @@ -25,7 +25,7 @@ import { cn } from '@/lib/utils'; import { IClient, CLIENT_FALLBACK_IMAGE, END_USER_FALLBACK_IMAGE } from '../../../types'; import { IProjectAssignment, ProjectAssignmentWorkOrderDetail } from '../../../type.assignment'; import { ManageTeam } from '../../../component/ManageTeam'; -import UpdateTimelineStatus from '../../../component/UpdateTimelineStatus'; +import UpdateTechnicalStatus from '../../../component/UpdateTechnicalStatus'; import Reveal from '@/components/ui/reveal'; interface Props { @@ -80,8 +80,8 @@ export default function AssignmentDetail({ client, initialAssignment: assignment const visibleTeam = showAllTeam ? team : team.slice(0, TEAM_PREVIEW_LIMIT); const hiddenTeamCount = team.length - visibleTeam.length; - const statusHistory = assignment.project_assignment_status_detail?.filter(s => s.start_date) ?? []; - const currentEntry = statusHistory.find(s => !s.end_date) ?? statusHistory[0]; + const statusHistory = assignment.project_assignment_status_detail?.filter(s => Boolean(s.start_date || (Array.isArray(s.date) && s.date.length > 0))) ?? []; + const currentEntry = statusHistory.find(s => !s.end_date && !(Array.isArray(s.date) && s.date[s.date.length - 1]?.end_date)) ?? statusHistory[0]; const duration = formatSpan(assignmentStartDate, assignmentEndDate); const positionCount = new Set(team.map(m => m.user?.position?.name).filter(Boolean)).size; @@ -147,7 +147,7 @@ export default function AssignmentDetail({ client, initialAssignment: assignment className="w-full shrink-0 gap-2 transition-transform active:scale-[0.98] md:w-auto" > - Update status + Update Technical Status @@ -363,7 +363,7 @@ export default function AssignmentDetail({ client, initialAssignment: assignment {statusHistory.length === 0 ? (

- No status changes recorded yet. Use Update status to log the first one. + No status changes recorded yet. Use Update Technical Status to log the first one.

) : (
    @@ -395,6 +395,11 @@ export default function AssignmentDetail({ client, initialAssignment: assignment {span ? (

    {span}

    ) : null} + {entry.description ? ( +

    + {entry.description} +

    + ) : null} ); })} @@ -406,7 +411,7 @@ export default function AssignmentDetail({ client, initialAssignment: assignment {action === 'UPDATE_STATUS' ? ( - + ) : null} {action === 'MANAGE_EMPLOYEE' ? ( diff --git a/app/(dashboard)/task/client-detail/[client_id]/[assignment_id]/page.tsx b/app/(dashboard)/task/client-detail/[client_id]/[assignment_id]/page.tsx index 5310544..0cf6fe3 100644 --- a/app/(dashboard)/task/client-detail/[client_id]/[assignment_id]/page.tsx +++ b/app/(dashboard)/task/client-detail/[client_id]/[assignment_id]/page.tsx @@ -15,6 +15,8 @@ export default async function AssignmentDetailPage({ params }: Props) { const [client, assignment] = await Promise.all([getClientById, getAssignmentById]); + // console.log(assignment) + // 3. Handle 404 Securely on the server if (!assignment) { return ( diff --git a/app/(dashboard)/task/client-detail/[client_id]/project-detail/[project_id]/StatusTimeline.tsx b/app/(dashboard)/task/client-detail/[client_id]/project-detail/[project_id]/StatusTimeline.tsx index 444179e..fb3251e 100644 --- a/app/(dashboard)/task/client-detail/[client_id]/project-detail/[project_id]/StatusTimeline.tsx +++ b/app/(dashboard)/task/client-detail/[client_id]/project-detail/[project_id]/StatusTimeline.tsx @@ -25,13 +25,26 @@ export default function StatusTimeline({ history }: StatusTimelineProps) {

    No status changes recorded

    - Use the Update Status action to record the current stage of this work order. + Use the Update Technical Status action to record the current stage of this work order.

    ); } - const currentEntry = history.find(s => !s.end_date) ?? history[0]; + const getEntryDates = (entry: ProjectAssignmentStatusDetail) => { + if (Array.isArray(entry.date) && entry.date.length > 0) { + const start = entry.date[0]?.start_date ?? entry.start_date; + const end = entry.date[entry.date.length - 1]?.end_date ?? entry.end_date; + return { start, end, allDates: entry.date }; + } + return { + start: entry.start_date, + end: entry.end_date, + allDates: entry.start_date || entry.end_date ? [{ start_date: entry.start_date, end_date: entry.end_date }] : [] + }; + }; + + const currentEntry = history.find(s => !getEntryDates(s).end) ?? history[0]; const visibleHistory = showAll ? history : history.slice(0, TIMELINE_PREVIEW_LIMIT); const hiddenCount = history.length - visibleHistory.length; @@ -40,8 +53,9 @@ export default function StatusTimeline({ history }: StatusTimelineProps) {
      {visibleHistory.map(entry => { const isCurrent = entry.id === currentEntry?.id; - const span = formatSpan(entry.start_date, entry.end_date); - const remarks = (entry as any).status_remarks || (entry as any).remarks; + const { start, end, allDates } = getEntryDates(entry); + const span = formatSpan(start, end); + const remarks = entry.description || (entry as any).status_remarks || (entry as any).remarks; return (
    1. @@ -64,9 +78,19 @@ export default function StatusTimeline({ history }: StatusTimelineProps) { Current ) : null} -

      - {formatDate(entry.start_date)} - {entry.end_date ? formatDate(entry.end_date) : 'Present'} -

      + {allDates.length > 1 ? ( +
      + {allDates.map((d, i) => ( +

      + {formatDate(d.start_date)} - {d.end_date ? formatDate(d.end_date) : 'Present'} +

      + ))} +
      + ) : ( +

      + {formatDate(start)} - {end ? formatDate(end) : 'Present'} +

      + )} {span ? (

      {span}

      ) : null} diff --git a/app/(dashboard)/task/client-detail/[client_id]/project-detail/[project_id]/WorkOrderCard.tsx b/app/(dashboard)/task/client-detail/[client_id]/project-detail/[project_id]/WorkOrderCard.tsx index 4553ca3..37fbdce 100644 --- a/app/(dashboard)/task/client-detail/[client_id]/project-detail/[project_id]/WorkOrderCard.tsx +++ b/app/(dashboard)/task/client-detail/[client_id]/project-detail/[project_id]/WorkOrderCard.tsx @@ -17,7 +17,7 @@ import StatusTimeline from './StatusTimeline'; import AssignedPeople from './AssignedPeople'; import BadgeStatus from '@/components/ui/badge-status'; import { IProjectAssignment } from '@/app/(dashboard)/task/type.assignment'; -import UpdateTimelineStatus from '@/app/(dashboard)/task/component/UpdateTimelineStatus'; +import UpdateTimelineStatus from '@/app/(dashboard)/task/component/UpdateTechnicalStatus'; import { ManageTeam } from '@/app/(dashboard)/task/component/ManageTeam'; import { cn } from '@/lib/utils'; import { COMPLETE_STATUS_LIST, ONPROGRESS_STATUS_LIST } from '@/app/(dashboard)/task/data'; @@ -40,7 +40,7 @@ export default function WorkOrderCard({ workOrder, index, defaultOpen = false }: setAction(undefined); }; - const statusList = workOrder.project_assignment_status_detail?.filter(status => status.start_date) || []; + const statusList = workOrder.project_assignment_status_detail?.filter(status => Boolean(status.start_date || (Array.isArray(status.date) && status.date.length > 0))) || []; const team = workOrder.project_assignment_work_order_detail ?? []; const span = formatSpan(workOrder.wo_start_date, workOrder.wo_end_date); const shownAvatars = team.slice(0, 4); @@ -241,7 +241,7 @@ export default function WorkOrderCard({ workOrder, index, defaultOpen = false }: }} > - Update Status + Update Technical Status } /> diff --git a/app/(dashboard)/task/component/UpdateTechnicalStatus.tsx b/app/(dashboard)/task/component/UpdateTechnicalStatus.tsx new file mode 100644 index 0000000..d6a98d7 --- /dev/null +++ b/app/(dashboard)/task/component/UpdateTechnicalStatus.tsx @@ -0,0 +1,571 @@ +"use client"; + +import { useMemo, useState, type FC } from 'react'; +import { + Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle +} from "@/components/ui/dialog"; +import { Button } from '@/components/ui/button'; +import { DatePicker } from '@/components/ui/datepicker'; +import { Textarea } from '@/components/ui/textarea'; +import { + Table, TableBody, TableCell, TableHead, TableHeader, TableRow +} from "@/components/ui/table"; +import { + AlertCircle, Calendar, Edit2, History, Loader2, Plus, Save, Trash2 +} 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 BadgeStatus from '@/components/ui/badge-status'; +import dayjs from 'dayjs'; + +export interface IDateInterval { + start_date: string; + end_date: string; +} + +export interface IProjectAssignmentTimeline { + id: number; + start_date: string | null; + end_date: string | null; + date?: IDateInterval[] | string | null; + description?: string | null; + updated_at?: string | null; + project_assignment_status: ProjectAssignmentStatus; +} + +export interface ProjectAssignmentStatus { + id: number; + name: string; + description?: string | null; + remark?: any; +} + +export interface StatusItem { + detailId: number; + technicalId: number; + statusName: string; + description: string; + dates: IDateInterval[]; +} + +type Props = { + assignmentId: string | number; + onClose?: VoidFunction; +}; + +interface ModalState { + mode: 'ADD' | 'EDIT'; + technicalId: number; + detailId: number; + statusName: string; + existingDates: IDateInterval[]; + editIndex?: number; + initialStartDate?: Date; + initialEndDate?: Date; + initialDescription: string; +} + +function extractDatesFromRow(row: any): IDateInterval[] { + if (!row) return []; + + let rawDate = row.date; + if (typeof rawDate === 'string') { + try { + rawDate = JSON.parse(rawDate); + } catch { + rawDate = null; + } + } + + if (Array.isArray(rawDate) && rawDate.length > 0) { + return rawDate + .filter(Boolean) + .map((item: any) => ({ + start_date: item?.start_date ? dayjs(item.start_date).format('YYYY-MM-DD') : '', + end_date: item?.end_date ? dayjs(item.end_date).format('YYYY-MM-DD') : '' + })) + .filter((d: IDateInterval) => Boolean(d.start_date || d.end_date)); + } + + if (row.start_date || row.end_date) { + return [{ + start_date: row.start_date ? dayjs(row.start_date).format('YYYY-MM-DD') : '', + end_date: row.end_date ? dayjs(row.end_date).format('YYYY-MM-DD') : '' + }]; + } + + return []; +} + +interface EditModalProps { + assignmentId: string | number; + data: ModalState; + onClose: VoidFunction; + onSuccess: VoidFunction; +} + +function EditTechnicalStatusModal({ assignmentId, data, onClose, onSuccess }: EditModalProps) { + const [startDate, setStartDate] = useState(data.initialStartDate); + const [endDate, setEndDate] = useState(data.initialEndDate); + const [description, setDescription] = useState(data.initialDescription || ''); + const [isSaving, setIsSaving] = useState(false); + + // Validation: end date must not be earlier than start date + const isDateRangeInvalid = useMemo(() => { + if (!startDate || !endDate) return false; + return dayjs(endDate).isBefore(dayjs(startDate), 'day'); + }, [startDate, endDate]); + + const isDateMissing = !startDate || !endDate; + + const handleSave = async () => { + if (isDateMissing) { + toast.error('Date range required', { + description: 'Please select both a start date and an end date.' + }); + return; + } + + if (isDateRangeInvalid) { + toast.error('Invalid date interval', { + description: 'End date cannot be earlier than start date.' + }); + return; + } + + setIsSaving(true); + + let finalDates: IDateInterval[] = [...data.existingDates]; + + if (data.mode === 'ADD') { + const newInterval: IDateInterval = { + start_date: startDate ? dayjs(startDate).format('YYYY-MM-DD') : '', + end_date: endDate ? dayjs(endDate).format('YYYY-MM-DD') : '', + }; + // Retain all existing dates and append the new one + finalDates.push(newInterval); + } else if (data.mode === 'EDIT' && typeof data.editIndex === 'number') { + const updatedInterval: IDateInterval = { + start_date: startDate ? dayjs(startDate).format('YYYY-MM-DD') : '', + end_date: endDate ? dayjs(endDate).format('YYYY-MM-DD') : '', + }; + // Update only the targeted date, preserve all other existing dates + finalDates = finalDates.map((item, idx) => + idx === data.editIndex ? updatedInterval : item + ); + } + + const submitPayload = { + project_assignment_technical_status_id: Number(data.technicalId), + project_assignment_status_detail_id: Number(data.detailId), + date: finalDates, + description: description.trim() + }; + + const res = await serverApiCall('PUT', `/project-assignment/technical-status/${assignmentId}`, submitPayload); + + setIsSaving(false); + + if (res.success) { + toast.success(data.mode === 'ADD' ? 'Schedule interval added successfully' : 'Schedule interval updated successfully'); + onSuccess(); + } else { + toast.error('Failed to save status', { + description: `(${res.code || '-'}) ${res.message}` + }); + } + }; + + const modalTitle = data.mode === 'ADD' ? 'Add Schedule' : 'Edit Schedule'; + + const modalDescription = data.mode === 'ADD' + ? `Add a new schedule interval for ${data.statusName}. Previously recorded dates will be retained.` + : `Update the selected schedule interval and deliverable notes.`; + + return ( + { if (!open && !isSaving) onClose(); }}> + + +
      + +
      + {modalTitle} + {modalDescription} +
      + +
      + {/* Show existing dates overview when adding */} + {data.existingDates.length > 0 && data.mode === 'ADD' && ( +
      +
      + Existing Schedules ({data.existingDates.length}) + +1 new interval will be added +
      +
      + {data.existingDates.map((item, idx) => ( + + + {dayjs(item.start_date).format('DD MMM YYYY')} – {dayjs(item.end_date).format('DD MMM YYYY')} + + ))} +
      +
      + )} + +
      +
      + + +
      +
      + + +
      +
      + + {isDateRangeInvalid && ( +
      + + End date cannot be earlier than start date. +
      + )} + +
      +
      + + {description.length}/500 +
      +