Files
hats-app/app/(dashboard)/task/client-detail/[client_id]/[assignment_id]/UpdateRealProgressModal.tsx
T
Ichsan Kurniawan 3fbecab072 Add assignment progress and activity logging
Add modals for updating real progress and recording activity logs, and enhance the milestone timeline with date-based positioning and activity details. Update dashboard metrics to show active internal memos.
2026-10-16 15:37:20 +07:00

211 lines
9.0 KiB
TypeScript

"use client";
import { useState } from 'react';
import { useRouter } from 'next/navigation';
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 { Gauge, Loader2, Save } from 'lucide-react';
interface UpdateRealProgressModalProps {
assignmentId: number;
assignmentName: string;
assignmentCode?: string;
currentProgress?: number | null;
open: boolean;
onClose: () => void;
onSuccess?: () => void;
}
export default function UpdateRealProgressModal({
assignmentId,
assignmentName,
assignmentCode,
currentProgress = 0,
open,
onClose,
onSuccess,
}: UpdateRealProgressModalProps) {
const router = useRouter();
const initialVal = typeof currentProgress === 'number' && !isNaN(currentProgress)
? Math.min(100, Math.max(0, currentProgress))
: 0;
const [progress, setProgress] = useState<number>(initialVal);
const [description, setDescription] = useState<string>('');
const [isSaving, setIsSaving] = useState(false);
const presetValues = [0, 25, 50, 75, 100];
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!assignmentId) {
toast.error('Invalid assignment ID');
return;
}
setIsSaving(true);
const payload = {
project_assignment_id: Number(assignmentId),
progress: Number(progress),
description: description.trim(),
};
const res = await serverApiCall('PUT', `/project-assignment/progress/${assignmentId}`, payload);
setIsSaving(false);
if (res.success) {
toast.success('Real progress updated successfully', {
description: `Progress updated to ${progress}%`,
});
onClose();
if (onSuccess) {
onSuccess();
} else {
router.refresh();
}
} else {
toast.error('Failed to update progress', {
description: res.message || 'An unexpected error occurred',
});
}
};
return (
<Dialog open={open} onOpenChange={(isOpen) => !isOpen && !isSaving && onClose()}>
<DialogContent className="sm:max-w-lg p-0 overflow-hidden border border-border/80 bg-card 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">
<Gauge className="h-4 w-4" />
</span>
Update Real Progress
</DialogTitle>
<DialogDescription className="text-muted-foreground leading-relaxed">
Set the current completion percentage and 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 py-5">
{/* Draggable Progress Control */}
<div className="space-y-3 rounded-xl border border-border/70 bg-muted/20 p-4">
<div className="flex items-center justify-between">
<Label htmlFor="progress-slider" className="text-xs font-semibold text-foreground">
Completion Percentage
</Label>
<div className="flex items-center gap-1.5 font-mono">
<span className="text-2xl font-extrabold text-primary tracking-tight">
{progress}%
</span>
</div>
</div>
{/* Interactive Range Slider */}
<div className="relative py-2">
<input
id="progress-slider"
type="range"
min={0}
max={100}
step={1}
value={progress}
onChange={(e) => setProgress(Number(e.target.value))}
disabled={isSaving}
className="w-full h-2.5 bg-zinc-200 dark:bg-zinc-700 rounded-lg appearance-none cursor-pointer accent-his focus:outline-none focus:ring-2 focus:ring-his/30 transition-all"
/>
</div>
{/* Quick Presets */}
<div className="flex items-center justify-between gap-1 pt-1">
{presetValues.map((val) => (
<button
key={val}
type="button"
onClick={() => setProgress(val)}
disabled={isSaving}
className={`px-2.5 py-1 text-[11px] font-mono rounded-md border transition-all ${progress === val
? 'bg-his text-white border-his font-bold shadow-xs'
: 'bg-background hover:bg-muted text-muted-foreground border-border/70 hover:text-foreground'
}`}
>
{val}%
</button>
))}
</div>
</div>
{/* Description Field */}
<div className="space-y-2">
<div className="flex items-center justify-between">
<Label htmlFor="progress-description" className="text-xs font-semibold text-foreground">
Progress Notes / Description
</Label>
<span className="text-[11px] text-muted-foreground font-mono">
{description.length} / 500
</span>
</div>
<Textarea
id="progress-description"
value={description}
onChange={(e) => setDescription(e.target.value)}
placeholder="Describe current achievements, modules completed, or status notes..."
rows={3}
maxLength={500}
disabled={isSaving}
className="text-xs resize-none"
/>
</div>
{/* Footer Actions */}
<DialogFooter className="gap-2 pt-2 border-t border-border/70">
<Button
type="button"
variant="outline"
onClick={onClose}
disabled={isSaving}
className="text-xs"
>
Cancel
</Button>
<Button
type="submit"
disabled={isSaving}
className="text-xs gap-1.5 bg-his hover:bg-his/90 text-white font-medium shadow-md shadow-his/20"
>
{isSaving ? (
<>
<Loader2 className="h-3.5 w-3.5 animate-spin" />
Saving...
</>
) : (
<>
<Save className="h-3.5 w-3.5" />
Save Progress
</>
)}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
);
}