mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:46:29 +00:00
7f8e6637e4
Extract NextAuth configuration into a shared library, load admin sidebar items from role permissions, and update related RBAC types and UI.
214 lines
15 KiB
TypeScript
214 lines
15 KiB
TypeScript
import { FC, Suspense, lazy } from 'react';
|
|
import { redirect } from 'next/navigation';
|
|
import { getServerSession } from 'next-auth';
|
|
import dayjs from 'dayjs';
|
|
import { Card, Card900, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
|
import LazyLoading from '@/components/loader/LazyLoading';
|
|
import fetchData, { fetchDataWithPost } from '@/lib/fetchData';
|
|
import { generateHourMinutes } from '@/lib/helper';
|
|
import ViewAvatar from '@/components/ViewAvatar';
|
|
import { authOptions } from '@/lib/auth';
|
|
|
|
const DonutChart = lazy(() => import('./DonutChart'));
|
|
const AreaChart = lazy(() => import('./AreaChart'))
|
|
const TableAttendanceHistory = lazy(() => import('./Table'))
|
|
const ExportComponent = lazy(() => import('./Export'))
|
|
const EmployeeAlpha = lazy(() => import('./EmployeeAlpha'));
|
|
|
|
type Props = {
|
|
id: string | undefined
|
|
start_date?: string
|
|
end_date?: string
|
|
};
|
|
|
|
const PerformanceComponent: FC<Props> = async ({ id, start_date, end_date }) => {
|
|
const session = await getServerSession(authOptions)
|
|
if (id === 'undefined') {
|
|
return redirect(`/auth?&error=Restart your session to continue !`)
|
|
}
|
|
|
|
const ALLOWED_ROLES = ['SYSADMIN', 'ADMIN', 'MNG', 'TL']
|
|
if (!ALLOWED_ROLES.includes(session?.user.detail.role.code!)) {
|
|
if (session?.user.detail.id !== Number(id)) {
|
|
return redirect('/not-found')
|
|
}
|
|
}
|
|
|
|
const fetchUser = fetchData(`/user/${id}`)
|
|
const fetchPerformace = fetchDataWithPost('/dashboard/user/' + id, { start_date: start_date || dayjs().subtract(1, 'day').subtract(1, 'month'), end_date: end_date || dayjs().subtract(1, 'day').format('YYYY-MM-DD') })
|
|
const fetchAlpha = fetchDataWithPost(`/presence/report/absent`, { user_id: Number(id), start_date: start_date || dayjs().subtract(1, 'day').subtract(1, 'month'), end_date: end_date || dayjs().subtract(1, 'day').format('YYYY-MM-DD') })
|
|
|
|
const [user, performance, alpha] = await Promise.all([fetchUser, fetchPerformace, fetchAlpha])
|
|
|
|
return (
|
|
<>
|
|
<div className='gap-4 space-y-7 w-full mx-auto mt-6'>
|
|
<Card900>
|
|
<CardContent className='flex flex-col sm:flex-row items-center gap-6 sm:gap-0'>
|
|
<ViewAvatar avatar={user.avatar} className='w-40 h-40 sm:min-w-44 sm:w-44! sm:h-44! duration-300' />
|
|
<div className='sm:ml-6 2xl:ml-10 w-full'>
|
|
<span className='text-lg font-semibold leading-none tracking-tight sm:text-2xl'>{user.fullname}</span>
|
|
<div className='grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-4 gap-2 mt-3 w-full'>
|
|
<div className='flex flex-col'>
|
|
<span className='text-muted-foreground text-sm'>Position</span>
|
|
<span>{user.position?.name}</span>
|
|
</div>
|
|
<div className='flex flex-col'>
|
|
<span className='text-muted-foreground text-sm'>NIKTG</span>
|
|
<span>{user.niktg || '-'}</span>
|
|
</div>
|
|
<div className='flex flex-col'>
|
|
<span className='text-muted-foreground text-sm'>NIK</span>
|
|
<span>{user.nik || '-'}</span>
|
|
</div>
|
|
<div className='flex flex-col'>
|
|
<span className='text-muted-foreground text-sm'>Employee Type</span>
|
|
<span>{user.employee_type?.name || '-'}</span>
|
|
</div>
|
|
<div className='flex flex-col'>
|
|
<span className='text-muted-foreground text-sm'>Office Email</span>
|
|
<a title={user.email_office || ''} href={`mailto:${user.email_office}`} referrerPolicy="no-referrer" className='truncate'>
|
|
{user.email_office || '-'}
|
|
</a>
|
|
</div>
|
|
<div className='flex flex-col'>
|
|
<span className='text-muted-foreground text-sm'>Phone</span>
|
|
<span>{user.phone_number}</span>
|
|
</div>
|
|
<div className='flex flex-col'>
|
|
<span className='text-muted-foreground text-sm'>Hire Date</span>
|
|
<span>{user.hire_date ? dayjs(user.hire_date).format('MMM DD, YYYY') : '-'}</span>
|
|
</div>
|
|
<div className='flex flex-col'>
|
|
<span className='text-muted-foreground text-sm'>Contract Expiration Date</span>
|
|
<span>{user.contract_expiration_date ? dayjs(user.contract_expiration_date).format('MMM DD, YYYY') : '-'}</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</CardContent>
|
|
</Card900>
|
|
<div className='grid gap-3 grid-cols-1 sm:grid-cols-2 xl:grid-cols-4'>
|
|
<div className='space-y-3'>
|
|
<Card900>
|
|
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
|
|
<CardTitle className="text-sm font-medium">Working Hours</CardTitle>
|
|
<i className="ti ti-report icon h-4 w-4 text-his text-xl"></i>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<div className="text-2xl font-bold truncate">{generateHourMinutes(performance.presence?.working_hours)}</div>
|
|
<p className="text-xs text-muted-foreground">
|
|
number of working hours in {start_date ? `${dayjs(start_date).format('MMM DD')} - ${dayjs(end_date).format('MMM DD')}` : 'the Past 30 Days'}
|
|
</p>
|
|
</CardContent>
|
|
</Card900>
|
|
<Card900>
|
|
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
|
|
<CardTitle className="text-sm font-medium">Average <span className='text-green-500 font-semibold'>Check IN</span> | <span className='text-rose-500 font-semibold'>Check OUT</span></CardTitle>
|
|
<i className="ti ti-clock-24 icon h-4 w-4 text-his text-xl"></i>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<div className="text-2xl font-bold"><span className='text-green-500'>{performance.presence.check_in_mod}:00</span> | <span className='text-rose-500'>{performance.presence.check_out_mod}:00</span></div>
|
|
<p className="text-xs text-muted-foreground">
|
|
in {start_date ? `${dayjs(start_date).format('MMM DD')} - ${dayjs(end_date).format('MMM DD')}` : 'the Past 30 Days'}
|
|
</p>
|
|
</CardContent>
|
|
</Card900>
|
|
</div>
|
|
<div className='space-y-3'>
|
|
<Card900>
|
|
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
|
|
<div className='flex gap-2'>
|
|
<CardTitle className="text-sm font-medium">Total Alpha</CardTitle>
|
|
<Suspense fallback={<LazyLoading />}>
|
|
<EmployeeAlpha user={user} alphaData={alpha} />
|
|
</Suspense>
|
|
</div>
|
|
<i className="ti ti-clock-play icon h-4 w-4 text-his text-xl"></i>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<div className="text-2xl font-bold">{alpha && alpha.length ? alpha.length : 0}</div>
|
|
<p className="text-xs text-muted-foreground">
|
|
in {start_date ? `${dayjs(start_date).format('MMM DD')} - ${dayjs(end_date).format('MMM DD')}` : 'the Past 30 Days'}
|
|
</p>
|
|
</CardContent>
|
|
</Card900>
|
|
<Card900 className='relative'>
|
|
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
|
|
<CardTitle className="text-sm font-medium">Total <span className='text-cyan-500 font-semibold'>Task</span> | <span className='text-amber-500 font-semibold'>On Progress</span> | <span className='text-lime-500 font-semibold'>Done</span></CardTitle>
|
|
<i className="ti ti-checklist icon h-4 w-4 text-his text-xl"></i>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<div className="text-2xl font-bold"><span className='text-cyan-500'>-</span> | <span className='text-amber-500'>-</span> | <span className='text-lime-500'>-</span></div>
|
|
<p className="text-xs text-muted-foreground">
|
|
in {start_date ? `${dayjs(start_date).format('MMM DD')} - ${dayjs(end_date).format('MMM DD')}` : 'the Past 30 Days'}
|
|
</p>
|
|
</CardContent>
|
|
<div className='absolute inset-0 w-full h-full bg-white/60 dark:bg-black/60 rounded-2xl flex justify-center items-center font-semibold text-xl'>
|
|
<p className='animate-pulse'>Coming soon</p>
|
|
</div>
|
|
</Card900>
|
|
</div>
|
|
<Card900>
|
|
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-0">
|
|
<CardTitle className="text-sm font-medium">Total <span className='text-violet-500 font-semibold'>Leave</span> | <span className='text-yellow-500 font-semibold'>Sick</span> | <span className='text-cyan-500 font-semibold'>Permit</span></CardTitle>
|
|
<i className="ti ti-pills icon h-4 w-4 text-his text-xl"></i>
|
|
</CardHeader>
|
|
<CardContent className='pb-0'>
|
|
<p className="text-xs text-muted-foreground">in {start_date ? `${dayjs(start_date).format('MMM DD')} - ${dayjs(end_date).format('MMM DD')}` : 'the Past 30 Days'}</p>
|
|
{performance.presence?.total_ct01 || performance.presence?.total_skt || performance.presence?.total_iz01 ?
|
|
<Suspense fallback={<LazyLoading />}>
|
|
<DonutChart data={[performance.presence?.total_ct01, performance.presence?.total_skt, performance.presence?.total_iz01]} labels={['Leave', 'Sick', 'Permit']} colors={['#8b5cf6', '#eab308', '#06b6d4']} />
|
|
</Suspense>
|
|
:
|
|
<div className='flex items-center justify-center h-40 text-sm font-medium'>
|
|
No Data.
|
|
</div>
|
|
}
|
|
</CardContent>
|
|
</Card900>
|
|
<Card900>
|
|
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-0">
|
|
<CardTitle className="text-sm font-medium">Total <span className='text-sky-500 font-semibold'>WFO</span> | <span className='text-emerald-500 font-semibold'>WFS</span> | <span className='text-pink-600 font-semibold'>WFH</span></CardTitle>
|
|
<i className="ti ti-map-pins icon h-4 w-4 text-his text-xl"></i>
|
|
</CardHeader>
|
|
<CardContent className='pb-0'>
|
|
<p className="text-xs text-muted-foreground">in {start_date ? `${dayjs(start_date).format('MMM DD')} - ${dayjs(end_date).format('MMM DD')}` : 'the Past 30 Days'}</p>
|
|
<Suspense fallback={<LazyLoading />}>
|
|
<DonutChart data={[performance.presence?.total_wfo, performance.presence?.total_wfs, performance.presence?.total_wfh]} labels={['WFO', 'WFS', 'WFH']} colors={['#3b82f6', '#10b981', '#db2777']} />
|
|
</Suspense>
|
|
</CardContent>
|
|
</Card900>
|
|
</div>
|
|
<Card900>
|
|
<CardHeader>
|
|
<CardTitle>Attendance Statistics</CardTitle>
|
|
<CardDescription>Statistics of your attendance in {start_date ? `${dayjs(start_date).format('MMM DD')} - ${dayjs(end_date).format('MMM DD')}` : 'last 30 days'}.</CardDescription>
|
|
</CardHeader>
|
|
<CardContent className="px-0">
|
|
<Suspense fallback={<LazyLoading />}>
|
|
<AreaChart data={performance.presence?.statistics} />
|
|
</Suspense>
|
|
</CardContent>
|
|
</Card900>
|
|
<Card>
|
|
<CardHeader className='flex flex-col md:flex-row items-center justify-between space-y-5 sm:space-y-0'>
|
|
<div>
|
|
<CardTitle>Attendance History</CardTitle>
|
|
<CardDescription className='mt-1 sm:block hidden'>Here's a list of your attendance history in {start_date ? `${dayjs(start_date).format('MMM DD')} - ${dayjs(end_date).format('MMM DD')}` : 'the Past 30 Days'}!</CardDescription>
|
|
</div>
|
|
<Suspense fallback={<LazyLoading />}>
|
|
<ExportComponent fullname={user.fullname} data={performance.presence?.history} />
|
|
</Suspense>
|
|
</CardHeader>
|
|
<CardContent className='flex justify-center items-center'>
|
|
<Suspense fallback={<LazyLoading />}>
|
|
<TableAttendanceHistory data={performance.presence?.history} />
|
|
</Suspense>
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default PerformanceComponent; |