Files
hats-app/app/(dashboard)/task/client-detail/[client_id]/[assignment_id]/AddActivityLogModal.tsx
T
Ichsan Kurniawan ce6750ff02 Add timestamps to activity logs
Require a date and time when creating activity logs, allow minute-level selection, and improve date-picker scrolling within dialogs.
2026-10-20 15:41:55 +07:00

264 lines
12 KiB
TypeScript

"use client";
import { useState, useMemo } from 'react';
import { useRouter } from 'next/navigation';
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 { FilePlus2, Loader2, Save } from 'lucide-react';
import { DateTimePicker } from '@/components/ui/date-time-picker';
import dayjs from 'dayjs';
export interface IActivityLogType {
id: number;
name: string;
description?: string | null;
remark?: string | null;
type?: string;
}
const FALLBACK_LOG_TYPES: IActivityLogType[] = [
{ id: 13, name: 'update', description: 'General update on assignment progress', type: 'log' },
{ id: 14, name: 'action', description: 'Action item completed or undertaken', type: 'log' },
{ id: 15, name: 'issue', description: 'Blocker, bug, or risk encountered', type: 'log' },
{ id: 16, name: 'system', description: 'System or administrative event', type: 'log' },
];
interface AddActivityLogModalProps {
assignmentId: number;
assignmentName?: string;
assignmentCode?: string;
open: boolean;
onClose: () => void;
onSuccess?: () => void;
}
export default function AddActivityLogModal({
assignmentId,
assignmentName = 'Project Assignment',
assignmentCode,
open,
onClose,
onSuccess,
}: AddActivityLogModalProps) {
const router = useRouter();
const [selectedTypeId, setSelectedTypeId] = useState<string>('13');
const [description, setDescription] = useState<string>('');
const [date, setDate] = useState<Date | undefined>();
const [isSubmitting, setIsSubmitting] = useState(false);
const { data: fetchedData, isLoading: isLoadingTypes } = useFetchWithAxios(open ? '/project-assignment-status?type=log' : null);
const logTypes: IActivityLogType[] = useMemo(() => {
if (Array.isArray(fetchedData) && fetchedData.length > 0) {
return fetchedData;
}
if (fetchedData && Array.isArray((fetchedData as any)?.data) && (fetchedData as any).data.length > 0) {
return (fetchedData as any).data;
}
return FALLBACK_LOG_TYPES;
}, [fetchedData]);
const activeType = useMemo(() => {
return logTypes.find((t) => String(t.id) === selectedTypeId) || logTypes[0];
}, [logTypes, selectedTypeId]);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!assignmentId) {
toast.error('Invalid assignment ID');
return;
}
if (!description.trim()) {
toast.error('Description is required', {
description: 'Please describe the activity, update, or action taken.',
});
return;
}
setIsSubmitting(true);
const typeName = activeType?.name || 'update';
const payload = {
date_at: dayjs(date).format('YYYY-MM-DD HH:mm:ss'),
project_assignment_id: Number(assignmentId),
type: typeName,
description: description.trim(),
};
let res = await serverApiCall('POST', '/project-assignment-history', payload);
setIsSubmitting(false);
if (res.success) {
toast.success('Activity log added successfully', {
description: `Recorded "${typeName}" log for this assignment.`,
});
setDescription('');
onClose();
if (onSuccess) {
onSuccess();
} else {
router.refresh();
}
} else {
toast.error('Failed to add activity log', {
description: res.message || 'An unexpected error occurred while saving the log.',
});
}
};
return (
<Dialog open={open} onOpenChange={(isOpen) => !isOpen && !isSubmitting && onClose()}>
<DialogContent className="sm:max-w-lg p-0 overflow-hidden border border-border/80 bg-card text-foreground rounded-2xl shadow-2xl">
{/* 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.5 font-bold text-foreground">
<span className="flex h-8 w-8 items-center justify-center rounded-xl bg-his/10 text-his">
<FilePlus2 className="h-4 w-4" />
</span>
Add Activity Log
</DialogTitle>
<DialogDescription className="text-muted-foreground leading-relaxed">
Record a manual development update, action, or note 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-0">
{/* Description Field */}
<div className="space-y-2">
<div className="flex items-center justify-between">
<Label htmlFor="log-description" className="text-xs font-semibold text-foreground">
Datetime <span className="text-destructive">*</span>
</Label>
</div>
<DateTimePicker
placeholder="Pilih tanggal selesai"
date={date}
setDate={setDate}
/>
</div>
{/* Activity Type Field with Badge */}
<div className="space-y-2">
<div className="flex items-center justify-between">
<Label htmlFor="log-type-select" className="text-xs font-semibold text-foreground">
Activity Type <span className="text-destructive">*</span>
</Label>
{isLoadingTypes && (
<span className="text-[11px] text-muted-foreground flex items-center gap-1 font-mono">
<Loader2 className="h-3 w-3 animate-spin" /> Loading types...
</span>
)}
</div>
<Select value={selectedTypeId} onValueChange={setSelectedTypeId} disabled={isSubmitting}>
<SelectTrigger id="log-type-select" className="w-full h-10 text-sm">
<SelectValue placeholder="Select activity type">
{activeType && (
<div className="flex items-center justify-between gap-3 w-full pr-2">
<span className="capitalize font-medium">{activeType.name}</span>
<BadgeStatus status={activeType.name} />
</div>
)}
</SelectValue>
</SelectTrigger>
<SelectContent>
{logTypes.map((type) => (
<SelectItem key={type.id} value={String(type.id)} className="py-2.5 cursor-pointer">
<div className="flex items-center justify-between gap-4 w-full">
<div className="flex flex-col text-left">
<span className="font-medium text-foreground capitalize text-xs">{type.name}</span>
{type.description && type.description !== type.name && (
<span className="text-[11px] text-muted-foreground line-clamp-1">{type.description}</span>
)}
</div>
<BadgeStatus status={type.name} />
</div>
</SelectItem>
))}
</SelectContent>
</Select>
</div>
{/* Description Field */}
<div className="space-y-2">
<div className="flex items-center justify-between">
<Label htmlFor="log-description" className="text-xs font-semibold text-foreground">
Description <span className="text-destructive">*</span>
</Label>
<span className="text-[11px] text-muted-foreground font-mono">
{description.length} / 500
</span>
</div>
<Textarea
id="log-description"
value={description}
onChange={(e) => setDescription(e.target.value.slice(0, 500))}
placeholder="Detail the activity, bug fix, deployment action, or task completed..."
rows={4}
required
disabled={isSubmitting}
className="text-xs leading-relaxed resize-none"
/>
<p className="text-[11px] text-muted-foreground leading-normal">
This activity entry will appear in the Development Activity Log history timeline.
</p>
</div>
{/* Footer Actions */}
<DialogFooter className="gap-2 pt-2 border-t border-border/70">
<Button
type="button"
variant="outline"
onClick={onClose}
disabled={isSubmitting}
className="text-xs"
>
Cancel
</Button>
<Button
type="submit"
disabled={isSubmitting || !description.trim() || !date}
className="text-xs gap-1.5 bg-his hover:bg-his/90 text-white font-medium shadow-md shadow-his/20"
>
{isSubmitting ? (
<>
<Loader2 className="h-3.5 w-3.5 animate-spin" />
Saving...
</>
) : (
<>
<Save className="h-3.5 w-3.5" />
Save Activity
</>
)}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
);
}