mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 16:46:29 +00:00
fa973a8722
Refactor status update UI: remove the old FormUpdateStatus component and introduce UpdateTimelineStatus (inline timeline editor with row-level editing, date pickers, and notes). Update imports/usages across client, assignment, project and work-order components to use the new component and adjust props (assignmentId/onClose). Also rename AddTeam usage to ManageTeam in the assignment detail. Add client description support: FormClient now includes a description field and textarea, and IClient type gains an optional description. Minor cleanup: remove stray comment in HomeTask/page.
208 lines
11 KiB
TypeScript
208 lines
11 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 { Textarea } from '@/components/ui/textarea';
|
|
import * as Yup from 'yup'
|
|
import { yupResolver } from '@hookform/resolvers/yup';
|
|
import { useForm } from 'react-hook-form';
|
|
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';
|
|
|
|
const validationSchema = Yup.object().shape({
|
|
project_status_id: Yup.string().required(),
|
|
date: Yup.date().required('Date is required'),
|
|
note: Yup.string().optional().default(''),
|
|
});
|
|
|
|
type FormDataType = Yup.InferType<typeof validationSchema>;
|
|
|
|
type Props = {
|
|
assignmentId: string | number
|
|
onClose?: VoidFunction
|
|
};
|
|
|
|
const UpdateTimelineStatus: FC<Props> = ({ assignmentId, onClose }) => {
|
|
const [editingId, setEditingId] = useState<string | 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 form = useForm<FormDataType>({
|
|
resolver: yupResolver(validationSchema),
|
|
});
|
|
|
|
const router = useRouter()
|
|
|
|
const handleEdit = (row: any) => {
|
|
setEditingId(row.id);
|
|
|
|
setDraftData({
|
|
start_date_assign: row.start_date_assign,
|
|
end_date_assign: row.end_date_assign,
|
|
"note": draftData.note,
|
|
"remarks": draftData.note
|
|
});
|
|
};
|
|
|
|
const handleSave = async () => {
|
|
setIsSaving(true);
|
|
|
|
const submitPayload = {
|
|
"project_status_id": Number(draftData.project_status_id),
|
|
"start_date": draftData.start_date_assign ? new Date(draftData.start_date_assign).toISOString() : '',
|
|
"end_date": draftData.end_date_assign ? new Date(draftData.end_date_assign).toISOString() : '',
|
|
"note": draftData.note,
|
|
"remarks": draftData.note
|
|
}
|
|
|
|
console.log(submitPayload)
|
|
const res = await serverApiCall('PUT', `/project-assignment/status/${draftData.id}`, submitPayload)
|
|
|
|
setIsSaving(false);
|
|
if (res.success) {
|
|
toast.success('Team updated successfully')
|
|
handleSuccess!()
|
|
} else {
|
|
toast.error(`Fail to save team`, { 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">
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>Update Status</AlertDialogTitle>
|
|
<AlertDialogDescription>
|
|
Update the timeline status for this assignment. You can also add a note or remark regarding this status change. This will help you keep track of the progress and any important information related to this assignment.
|
|
</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>Note/Remarks</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. Create a team to get started with project assignments
|
|
</div>
|
|
</TableCell>
|
|
</TableRow>
|
|
:
|
|
existTimeline.map((row: any) => {
|
|
const isEditing = editingId === row.id;
|
|
const isOtherRowEditing = editingId !== null && editingId !== row.id;
|
|
|
|
return (
|
|
<TableRow key={row.id}>
|
|
<TableCell>
|
|
<BadgeStatus status={row.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_assign}</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_assign}</span>
|
|
}
|
|
</TableCell>
|
|
|
|
<TableCell>
|
|
{isEditing ?
|
|
<Textarea placeholder="Add a note about this status change..." className="min-h-10" value={draftData.note} onChange={(e) => handleDraftChange("note", e.target.value)} />
|
|
:
|
|
<span className="text-sm">{row.note}</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-blue-600 hover:text-blue-700 hover:bg-blue-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; |