Files
hats-app/app/performance/[id]/components/EmployeeAlpha.tsx
T
Ichsan Kurniawan 7d18e9f38f Add CreateAttendance and update UI/hooks
Add a new CreateAttendance component to allow admins to create manual attendance (Alpha) with confirmation dialog and server API call. Lazy-load the CreateAttendance component in EmployeeAlpha and wrap usages with Suspense plus a loading fallback to reduce initial bundle size. Update Performance page to use a centered Suspense fallback. Improve ImportExcel UX by showing a full-loading state, tweaking button/icon styles, and fixing modal sizing/scroll behavior. Enhance useFetchWithAxios to accept nullable URLs and SWR options, adjust the fetcher signature, and return the nested data payload. Add a "use server" directive and imports in lib/server-api, and apply small type/formatting tweaks (table size cast and minor UI/text refinements).
2026-02-22 23:33:15 +07:00

135 lines
7.2 KiB
TypeScript

"use client"
import { lazy, Suspense, useState, type FC } from 'react';
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogDescription,
} from '@/components/ui/dialog';
import { CalendarX2, Calendar, Eye } from 'lucide-react';
import { Badge } from '@/components/ui/badge';
import { format, getMonth, getYear, parseISO } from 'date-fns';
import { Button } from '@/components/ui/button';
import LazyLoading from '@/components/loader/LazyLoading';
const CreateAttendance = lazy(() => import('./CreateAttendance'));
type Props = {
user: any
alphaData: any[]
};
const monthColors: Record<number, { bg: string; text: string; border: string; dot: string }> = {
0: { bg: 'bg-primary/10', text: 'text-primary', border: 'border-primary/20', dot: 'bg-primary' },
1: { bg: 'bg-destructive/10', text: 'text-destructive', border: 'border-destructive/20', dot: 'bg-destructive' },
};
const EmployeeAlpha: FC<Props> = ({ alphaData, user }) => {
const [open, setOpen] = useState(false);
// Group by month
const grouped = alphaData.reduce<Record<string, typeof alphaData>>((acc, item) => {
const d = parseISO(item.date);
const key = `${getYear(d)}-${String(getMonth(d)).padStart(2, '0')}`;
if (!acc[key]) acc[key] = [];
acc[key].push(item);
return acc;
}, {});
const sortedMonths = Object.keys(grouped).sort();
return (
<>
<Eye size={20} onClick={() => setOpen(true)} className='text-emerald-600 hover:text-his duration-200 cursor-pointer' />
{/* Modal */}
<Dialog open={open} onOpenChange={setOpen}>
<DialogContent className="sm:max-w-lg p-0 gap-0 overflow-hidden border-border/50 bg-card">
{/* Modal Header */}
<div className="relative bg-destructive/5 border-b border-destructive/10 px-6 pt-6 pb-5">
<div className="absolute -top-8 -right-8 w-24 h-24 rounded-full bg-destructive/10 blur-xl" />
<DialogHeader className="relative">
<div className="flex items-center gap-3 mb-2">
<div className="flex h-10 w-10 items-center justify-center rounded-lg bg-destructive/10 border border-destructive/20">
<CalendarX2 className="h-5 w-5 text-destructive" />
</div>
<div>
<DialogTitle className="text-lg font-semibold text-foreground">
Absence Records
</DialogTitle>
<DialogDescription className="text-sm text-muted-foreground mt-0.5">
{alphaData.length} days without check-in
</DialogDescription>
</div>
</div>
</DialogHeader>
</div>
{/* Modal Body */}
<div className="px-6 py-5 max-h-[60vh] overflow-y-auto space-y-5">
{sortedMonths.map((monthKey, mIdx) => {
const dates = grouped[monthKey];
const sampleDate = parseISO(dates[0].date);
const monthLabel = format(sampleDate, 'MMMM yyyy');
const colorIdx = mIdx % 2;
const colors = monthColors[colorIdx];
return (
<div key={monthKey} className="animate-fade-in" style={{ animationDelay: `${mIdx * 80}ms` }}>
{/* Month Header */}
<div className="flex items-center justify-between mb-3">
<div className="flex items-center gap-2">
<div className={`h-2 w-2 rounded-full ${colors.dot}`} />
<span className="text-sm font-semibold text-foreground">{monthLabel}</span>
</div>
<Badge variant="secondary" className="text-xs font-mono">
{dates.length} day{dates.length > 1 ? 's' : ''}
</Badge>
</div>
{/* Date Items */}
<div className="space-y-2">
{dates.map((item, idx) => {
const d = parseISO(item.date);
return (
<div key={item.date} className={`flex items-center gap-3 rounded-xl border ${colors.border} ${colors.bg} px-4 py-3 transition-all duration-200 hover:scale-[1.01]`} style={{ animationDelay: `${(mIdx * 4 + idx) * 50}ms` }}>
<div className={`flex h-9 w-9 items-center justify-center rounded-lg bg-card border ${colors.border}`}>
<Calendar className={`h-4 w-4 ${colors.text}`} />
</div>
<div className="flex-1 min-w-0">
<p className="text-sm font-medium text-foreground">{format(d, 'EEEE')}</p>
<p className="text-xs text-muted-foreground font-mono">{format(d, 'dd MMMM yyyy')}</p>
</div>
<Badge variant="outline" className={`${colors.text} ${colors.border} ${colors.bg} text-xs`}>Alpha</Badge>
<Suspense fallback={<LazyLoading />}>
<CreateAttendance user={user} date={format(d, 'yyyy-MM-dd')} />
</Suspense>
</div>
);
})}
</div>
</div>
);
})}
</div>
{/* Modal Footer */}
<div className="border-t border-border px-6 py-4 bg-muted/30">
<div className="flex items-center justify-between">
<p className="text-xs text-muted-foreground">
Total: <span className="font-semibold font-mono text-foreground">{alphaData.length}</span> absence records
</p>
<Button variant="outline" size="sm" onClick={() => setOpen(false)}>
Close
</Button>
</div>
</div>
</DialogContent>
</Dialog>
</>
);
}
export default EmployeeAlpha;