Files
hats-app/app/(dashboard)/task/client-detail/[client_id]/[assignment_id]/UpdateRealProgressModal.tsx
T
Ichsan Kurniawan 390d03204a Add activity log timeline
Show assignment history on a date-based milestone timeline and refine milestone date positioning. Update activity log submission and description limits, refresh progress editing controls, and adjust the system log badge color.
2026-10-16 19:26:25 +07:00

264 lines
12 KiB
TypeScript

"use client";
import { useState, useEffect } 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);
useEffect(() => {
if (open) {
const val = typeof currentProgress === 'number' && !isNaN(currentProgress)
? Math.min(100, Math.max(0, currentProgress))
: 0;
setProgress(val);
setDescription('');
}
}, [open, currentProgress]);
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">
{/* Modern Filled Progress Slider Control */}
<div className="space-y-4 rounded-xl border border-border/80 bg-muted/20 p-4.5">
<div className="flex items-center justify-between">
<div>
<Label htmlFor="progress-slider" className="text-xs font-semibold text-foreground">
Completion Percentage
</Label>
<p className="text-[11px] text-muted-foreground mt-0.5">
Drag slider or enter percentage directly
</p>
</div>
<div className="flex items-center gap-1 bg-background border border-border/80 rounded-lg px-2.5 py-1 shadow-2xs focus-within:ring-2 focus-within:ring-his/30 focus-within:border-his transition-all">
<input
type="number"
min={0}
max={100}
value={progress}
onChange={(e) => {
const v = parseInt(e.target.value, 10);
if (isNaN(v)) {
setProgress(0);
} else {
setProgress(Math.min(100, Math.max(0, v)));
}
}}
disabled={isSaving}
className="w-11 text-right font-mono font-bold text-base bg-transparent text-foreground focus:outline-none"
/>
<span className="font-mono font-bold text-sm text-his select-none">%</span>
</div>
</div>
{/* Custom Filled Slider Component */}
<div className="relative py-2.5 select-none">
{/* Track container */}
<div className="relative h-3 w-full rounded-full bg-zinc-200/90 dark:bg-zinc-800 border border-zinc-300/70 dark:border-zinc-700/60 overflow-hidden">
{/* Active filled track (0% to progress%) */}
<div
className="h-full bg-his transition-[width] duration-75 rounded-full"
style={{ width: `${progress}%` }}
/>
</div>
{/* Draggable thumb positioned precisely along the track */}
<div
className="absolute top-1/2 -translate-y-1/2 -translate-x-1/2 pointer-events-none transition-all duration-75 z-10"
style={{ left: `${progress}%` }}
>
<div className="h-5 w-5 rounded-full bg-white dark:bg-zinc-900 border-2 border-his shadow-md flex items-center justify-center">
<div className="h-1.5 w-1.5 rounded-full bg-his" />
</div>
</div>
{/* Transparent native range input overlay for seamless drag, click, and keyboard accessibility */}
<input
id="progress-slider"
type="range"
min={0}
max={100}
step={1}
value={progress}
onChange={(e) => setProgress(Number(e.target.value))}
disabled={isSaving}
className="absolute inset-0 w-full h-full opacity-0 cursor-pointer z-20 focus:outline-none"
aria-label="Completion percentage slider"
/>
</div>
{/* Quick Presets */}
<div className="grid grid-cols-5 gap-1.5 pt-0.5">
{presetValues.map((val) => {
const isSelected = progress === val;
return (
<button
key={val}
type="button"
onClick={() => setProgress(val)}
disabled={isSaving}
className={`py-1.5 text-xs font-mono rounded-lg border transition-all ${isSelected
? 'bg-his text-white border-his font-bold shadow-xs'
: 'bg-background hover:bg-muted text-muted-foreground border-border/70 hover:text-foreground font-medium'
}`}
>
{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>
);
}