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.
308 lines
20 KiB
TypeScript
308 lines
20 KiB
TypeScript
import { Suspense, lazy } from "react"
|
|
import Link from "next/link"
|
|
import { getServerSession } from "next-auth"
|
|
import dayjs from "dayjs"
|
|
|
|
import { generateHourMinutes, titleCase } from "@/lib/helper"
|
|
import { fetchDataWithPost } from "@/lib/fetchData"
|
|
|
|
// import { CalendarDateRangePicker } from "./components/DaterangePicker"
|
|
import { Button } from "@/components/ui/button"
|
|
import { Card900, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
|
|
|
|
import LazyLoading from "@/components/loader/LazyLoading"
|
|
import { authOptions } from "@/lib/auth"
|
|
|
|
// const LastPresence = lazy(() => import("./components/LastPresence"))
|
|
const WorkingHours = lazy(() => import("../components/WorkingHours"))
|
|
const PresenceHistory = lazy(() => import("../components/PresenceHistory"))
|
|
const PieChart = lazy(() => import("../components/PieChart"))
|
|
const AreaChart = lazy(() => import("../components/AreaChart"))
|
|
const PresenceDetail = lazy(() => import("../components/PresenceDetail"))
|
|
const NeedApproval = lazy(() => import("../components/NeedApproval"))
|
|
const AdjustmentAutoCheckout = lazy(() => import("../components/AdjustmentAutoCheckout"))
|
|
const TableEmployee = lazy(() => import("../components/TableEmployee"))
|
|
const EmployeeBirthday = lazy(() => import("../components/EmployeeBirthday"))
|
|
const EmployeeContractExp = lazy(() => import("../components/EmployeeContractExp"))
|
|
const ApprovalLeaveSick = lazy(() => import("../components/ApprovalLeaveSick"))
|
|
|
|
export const dynamic = 'force-dynamic'
|
|
|
|
export default async function DashboardPage() {
|
|
const session = await getServerSession(authOptions)
|
|
|
|
const today = fetchDataWithPost('/presence/history/today', { nik: session?.user.detail.nik })
|
|
const all = fetchDataWithPost('/presence/history', { nik: session?.user.detail.nik, start_date: dayjs().subtract(1, 'week'), end_date: dayjs().format('YYYY-MM-DD') })
|
|
const dashboard = fetchDataWithPost('/dashboard', {})
|
|
const last = fetchDataWithPost('/presence/last-status/today', { nik: session?.user.detail.nik })
|
|
|
|
// Wait for the promises to resolve
|
|
const [todayPresence, allPresence, dataDashboard, lastPresence] = await Promise.all([today, all, dashboard, last])
|
|
|
|
const alreadyCheckin = todayPresence.history.length > 0
|
|
const alreadyCheckout = (alreadyCheckin && todayPresence.history[0].presence_checkout) || todayPresence.history.length === 0
|
|
|
|
// console.log(dataDashboard)
|
|
return (
|
|
<>
|
|
<div className="flex-1 space-y-8 p-6 sm:p-8 pt-3 sm:pt-7 pb-8">
|
|
<div className="flex items-start justify-between space-y-2">
|
|
<div className="hidden sm:block">
|
|
<h2 className="text-lg sm:text-3xl font-medium tracking-tight capitalize">Welcome Back, <span className="font-bold">{titleCase(session?.user.detail.nickname!)}</span></h2>
|
|
<p className="text-xs md:text-base text-muted-foreground mt-1">This is your dashboard of attendance, working hours and task report</p>
|
|
</div>
|
|
<div className="flex sm:hidden justify-between">
|
|
<div>
|
|
<h2 className="text-lg sm:text-3xl font-medium tracking-tight capitalize">Welcome Back, <span className="font-bold">{titleCase(session?.user.detail.nickname!)}</span></h2>
|
|
<p className="text-xs md:text-base text-muted-foreground xl:w-2/3 mt-2">This is your dashboard of attendance, working hours and task report</p>
|
|
</div>
|
|
{lastPresence.is_checkin && lastPresence.device_code !== '11001' &&
|
|
<Link href='/iam-here'>
|
|
<Button variant='ghost' title="I'am Here">
|
|
<span className='animated-tada tada text-2xl'>👋🏻</span>
|
|
</Button>
|
|
</Link>
|
|
}
|
|
</div>
|
|
<div className="flex items-center space-x-2">
|
|
{/* <CalendarDateRangePicker /> */}
|
|
{alreadyCheckout ?
|
|
<Link href='/presence-v2' className="hidden sm:block">
|
|
<Button className="min-w-[104px]" >Check-in <i className="ti ti-login text-lg"></i></Button>
|
|
</Link>
|
|
:
|
|
<Link href={lastPresence.is_checkin && lastPresence.device_code === '11001' ? '' : '/presence-v2?checkout=true'} className="hidden sm:block">
|
|
<Button className="min-w-[104px]" disabled={lastPresence.is_checkin && lastPresence.device_code === '11001'}>Check-out <i className="ti ti-logout text-lg"></i></Button>
|
|
</Link>
|
|
}
|
|
</div>
|
|
</div>
|
|
|
|
<div className="space-y-5">
|
|
<div className="grid gap-5 sm:grid-cols-2 lg:grid-cols-4">
|
|
<Card900>
|
|
<CardHeader className="flex flex-row items-center justify-between space-y-0">
|
|
<CardTitle className="text-sm font-medium">
|
|
{todayPresence.history.length === 0 || lastPresence.is_checkin ? "Today's Check-in" : "Today's Check-out"}
|
|
</CardTitle>
|
|
<i className="ti ti-brand-days-counter icon h-4 w-4 text-his text-xl"></i>
|
|
</CardHeader>
|
|
<CardContent>
|
|
{todayPresence.history.length === 0 || lastPresence.is_checkin ?
|
|
<div className="text-2xl font-bold">{todayPresence.history[0]?.working_hours_start ? dayjs(dataDashboard.presence?.todays_attendance_hours.check_in).format('HH:mm A') : '--:-- --'}</div>
|
|
:
|
|
<div className="text-2xl font-bold">{dataDashboard.presence?.todays_attendance_hours.check_out ? dayjs(dataDashboard.presence?.todays_attendance_hours.check_out).format('HH:mm A') : '--:-- --'}</div>
|
|
}
|
|
<p className="text-xs text-muted-foreground">
|
|
{todayPresence.history.length === 0 || lastPresence.is_checkin ? "your today's check-in time" : "your today's check-out time"}
|
|
</p>
|
|
</CardContent>
|
|
</Card900>
|
|
<Card900>
|
|
<CardHeader className="flex flex-row items-center justify-between space-y-0">
|
|
<CardTitle className="text-sm font-medium">
|
|
Today's Working Hours
|
|
</CardTitle>
|
|
<i className="ti ti-clock-play icon h-4 w-4 text-his text-xl"></i>
|
|
</CardHeader>
|
|
<Suspense fallback={<LazyLoading />}>
|
|
<WorkingHours
|
|
currentTotalTime={dataDashboard.presence?.todays_working_hours_in_second.in_second}
|
|
isRunning={dataDashboard.presence?.todays_working_hours_in_second.running_count}
|
|
/>
|
|
</Suspense>
|
|
</Card900>
|
|
<Card900>
|
|
<CardHeader className="flex flex-row items-center justify-between space-y-0">
|
|
<CardTitle className="text-sm font-medium">
|
|
Last Presence
|
|
</CardTitle>
|
|
<i className="ti ti-history icon h-4 w-4 text-his text-xl"></i>
|
|
</CardHeader>
|
|
<Suspense fallback={<LazyLoading />}>
|
|
{/* <LastPresence history={allPresence.history} /> */}
|
|
<CardContent>
|
|
<div className="text-2xl font-bold">
|
|
{dataDashboard.presence.last_presence.check_in ?
|
|
dayjs(dataDashboard.presence.last_presence.check_in).format('HH:mm a') + ' - ' + (dataDashboard.presence.last_presence.check_out ?
|
|
dayjs(dataDashboard.presence.last_presence.check_out).format('HH:mm a')
|
|
:
|
|
'--:--'
|
|
)
|
|
:
|
|
'--:-- - --:--'
|
|
}
|
|
</div>
|
|
<p className="text-xs text-muted-foreground">
|
|
your last presence {dataDashboard.presence.last_presence.check_in ? 'at ' + dayjs(dataDashboard.presence.last_presence.check_in).format('MMM DD, YYYY') : ''}
|
|
</p>
|
|
</CardContent>
|
|
</Suspense>
|
|
</Card900>
|
|
<Card900>
|
|
<CardHeader className="flex flex-row items-center justify-between space-y-0">
|
|
<CardTitle className="text-sm font-medium">Working Hour in this Month</CardTitle>
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className="icon text-his h-5 w-5">
|
|
<path stroke="none" d="M0 0h24v24H0z" fill="none" />
|
|
<path d="M11.795 21h-6.795a2 2 0 0 1 -2 -2v-12a2 2 0 0 1 2 -2h12a2 2 0 0 1 2 2v4" />
|
|
<path d="M18 18m-4 0a4 4 0 1 0 8 0a4 4 0 1 0 -8 0" />
|
|
<path d="M15 3v4" />
|
|
<path d="M7 3v4" />
|
|
<path d="M3 11h16" />
|
|
<path d="M18 16.496v1.504l1 1" />
|
|
</svg>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<div className="text-2xl font-bold truncate">{dataDashboard?.presence?.working_hours_in_this_month ? generateHourMinutes(dataDashboard?.presence?.working_hours_in_this_month) : '0 Hour'}</div>
|
|
<p className="text-xs text-muted-foreground">
|
|
number of working hours in this month
|
|
</p>
|
|
</CardContent>
|
|
</Card900>
|
|
</div>
|
|
<div className="grid gap-5 grid-cols-1 md:grid-cols-2">
|
|
<Card900>
|
|
<CardHeader>
|
|
<CardTitle>Overview</CardTitle>
|
|
<CardDescription>Statistics of today's attendance summary.</CardDescription>
|
|
</CardHeader>
|
|
<CardContent className="sm:pl-0">
|
|
<Suspense fallback={<LazyLoading />}>
|
|
<PieChart data={dataDashboard.presence} />
|
|
</Suspense>
|
|
</CardContent>
|
|
</Card900>
|
|
<Card900>
|
|
<CardHeader>
|
|
<CardTitle>Your Statistics</CardTitle>
|
|
<CardDescription>Statistics of your attendance in last 14 days.</CardDescription>
|
|
</CardHeader>
|
|
<CardContent className="px-0">
|
|
<Suspense fallback={<LazyLoading />}>
|
|
<AreaChart data={dataDashboard.presence.statistics} />
|
|
</Suspense>
|
|
</CardContent>
|
|
</Card900>
|
|
</div>
|
|
<div className="grid gap-5 grid-cols-1 md:grid-cols-2">
|
|
<Card900>
|
|
<CardHeader>
|
|
<CardTitle>Attendance's List</CardTitle>
|
|
<CardDescription>List of today's attendance in your department.</CardDescription>
|
|
</CardHeader>
|
|
<CardContent className="px-0">
|
|
<Suspense fallback={<LazyLoading />}>
|
|
<PresenceDetail data={dataDashboard.presence} />
|
|
</Suspense>
|
|
</CardContent>
|
|
</Card900>
|
|
<Card900>
|
|
<CardHeader>
|
|
<CardTitle>Your History</CardTitle>
|
|
<CardDescription>List of your last 4 attendance history in this week.</CardDescription>
|
|
</CardHeader>
|
|
<CardContent className="px-0">
|
|
<Suspense fallback={<LazyLoading />}>
|
|
<PresenceHistory data={allPresence.history} />
|
|
</Suspense>
|
|
</CardContent>
|
|
</Card900>
|
|
{(session?.user.detail.role.code === 'MNG' || session?.user.detail.role.code === 'TL') &&
|
|
<Card900>
|
|
<CardHeader>
|
|
<CardTitle>Need to be Approve</CardTitle>
|
|
<CardDescription>List of today's attendance that require approval.</CardDescription>
|
|
</CardHeader>
|
|
<CardContent className="px-0">
|
|
<Suspense fallback={<LazyLoading />}>
|
|
<NeedApproval data={dataDashboard.presence?.need_to_be_approved} />
|
|
</Suspense>
|
|
</CardContent>
|
|
</Card900>
|
|
}
|
|
{(session?.user.detail.role.code === 'MNG' || session?.user.detail.role.code === 'TL') && dataDashboard.presence?.need_to_be_approved_auto_checkout?.total > 0 &&
|
|
<Card900>
|
|
<CardHeader>
|
|
<CardTitle>Adjustment Autocheckout</CardTitle>
|
|
<CardDescription>List of attendance that is not checked out.</CardDescription>
|
|
</CardHeader>
|
|
<CardContent className="px-0">
|
|
<Suspense fallback={<LazyLoading />}>
|
|
<AdjustmentAutoCheckout data={dataDashboard.presence?.need_to_be_approved_auto_checkout} />
|
|
</Suspense>
|
|
</CardContent>
|
|
</Card900>
|
|
}
|
|
{(session?.user.detail.role.code === 'MNG' || session?.user.detail.role.code === 'TL') && dataDashboard.presence?.need_to_be_approved_application_form?.total > 0 &&
|
|
<Card900>
|
|
<CardHeader>
|
|
<CardTitle>Work Leave and Sick Approval</CardTitle>
|
|
<CardDescription>List of work leave and sick application form.</CardDescription>
|
|
</CardHeader>
|
|
<CardContent className="px-0">
|
|
<Suspense fallback={<LazyLoading />}>
|
|
<ApprovalLeaveSick data={dataDashboard.presence?.need_to_be_approved_application_form} />
|
|
</Suspense>
|
|
</CardContent>
|
|
</Card900>
|
|
}
|
|
<Card900>
|
|
<CardHeader>
|
|
<CardTitle>Employees</CardTitle>
|
|
<CardDescription>List of employees in your department.</CardDescription>
|
|
</CardHeader>
|
|
<CardContent className="px-0">
|
|
<Suspense fallback={<LazyLoading />}>
|
|
<TableEmployee data={dataDashboard.employee} />
|
|
</Suspense>
|
|
</CardContent>
|
|
</Card900>
|
|
<Card900>
|
|
<CardHeader>
|
|
<CardTitle>Employee Birthday</CardTitle>
|
|
<CardDescription>List of employees birthday.</CardDescription>
|
|
</CardHeader>
|
|
<CardContent className="px-0">
|
|
<Suspense fallback={<LazyLoading />}>
|
|
<EmployeeBirthday data={dataDashboard.employee_birthday} />
|
|
</Suspense>
|
|
</CardContent>
|
|
</Card900>
|
|
{session?.user.detail.role.code === 'MNG' &&
|
|
<Card900>
|
|
<CardHeader>
|
|
<CardTitle>Employee Contract Expiration</CardTitle>
|
|
<CardDescription>List of employee contract expiration.</CardDescription>
|
|
</CardHeader>
|
|
<CardContent className="px-0">
|
|
<Suspense fallback={<LazyLoading />}>
|
|
<EmployeeContractExp data={dataDashboard.employee_contract_expiration} />
|
|
</Suspense>
|
|
</CardContent>
|
|
</Card900>
|
|
}
|
|
</div>
|
|
{/* <Card className="pb-5">
|
|
<CardHeader>
|
|
<CardTitle>Employees</CardTitle>
|
|
<CardDescription>List of employees in your department.</CardDescription>
|
|
</CardHeader>
|
|
<CardContent className="max-h-[400px] overflow-auto scrollbar-thin dark:scrollbar-track-zinc-white dark:scrollbar-track-zinc-950 scrollbar-thumb-zinc-400 dark:scrollbar-thumb-zinc-500">
|
|
<Suspense fallback={<LazyLoading />}>
|
|
<TableEmployee data={dataDashboard.employee} />
|
|
</Suspense>
|
|
</CardContent>
|
|
</Card> */}
|
|
</div>
|
|
</div>
|
|
|
|
{lastPresence.is_checkin && lastPresence.device_code !== '11001' &&
|
|
<Link href='/iam-here' className='hidden sm:block fixed bottom-8 right-10'>
|
|
<button className='bg-his rounded-full h-12 w-12 flex justify-center items-center text-2xl hover:bg-opacity-80 duration-300' title="I'am Here">
|
|
<span className='animatecss animatecss-tada animatecss-infinite'>👋🏻</span>
|
|
</button>
|
|
</Link>
|
|
}
|
|
</>
|
|
)
|
|
} |