mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:46:29 +00:00
269 lines
15 KiB
TypeScript
269 lines
15 KiB
TypeScript
'use client'
|
|
|
|
import { FC, lazy, Suspense, useEffect, useState } from 'react';
|
|
import { Badge } from '@/components/ui/badge';
|
|
import { ScrollArea } from '@/components/ui/scroll-area';
|
|
import dayjs from 'dayjs';
|
|
import FilterDataDashboard from './FilterDataDashboard';
|
|
import { cn } from '@/lib/utils';
|
|
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from '@/components/ui/alert-dialog';
|
|
import { Icons } from '@/components/Icons';
|
|
import { Button } from '@/components/ui/button';
|
|
import useAxiosAuth from '@/lib/axios/useAxiosAuth';
|
|
import { getVariantWorkingStatus, toastError } from '@/lib/helper';
|
|
import PageLoading from '@/components/loader/PageLoading';
|
|
import { useRouter } from 'next/navigation';
|
|
import { toast } from 'sonner';
|
|
import FallbackAvatar from '@/components/loader/FallbackAvatar';
|
|
|
|
const ViewAvatar = lazy(() => import('@/components/ViewAvatar'));
|
|
const Avatar = lazy(() => import('@/components/ViewAvatar').then(mod => ({ default: mod.Avatar })));
|
|
|
|
type Props = {
|
|
data: any
|
|
className?: string
|
|
};
|
|
|
|
const sortBy = 'start_date'
|
|
const filterBy = ['fullname', 'position_name', 'category']
|
|
|
|
const ApprovalLeaveSick: FC<Props> = ({ data, className }) => {
|
|
const [isLoading, setIsLoading] = useState(false)
|
|
const [isFullLoading, setIsFullLoading] = useState(false)
|
|
const [showDialog, setShowDialog] = useState(false)
|
|
const [showConfirm, setShowConfirm] = useState(false)
|
|
const [isApproval, setIsApproval] = useState(false)
|
|
const [dataPresence, setDataPresence] = useState<any>(null)
|
|
const [presenceId, setPresenceId] = useState(0)
|
|
const [dataFilter, setDataFilter] = useState<any[]>([])
|
|
const [search, setSearch] = useState('')
|
|
const [sort, setSort] = useState('desc')
|
|
|
|
const axios = useAxiosAuth()
|
|
const router = useRouter()
|
|
|
|
useEffect(() => {
|
|
let newData = [...data.list]
|
|
if (!search) {
|
|
newData = [...data.list]
|
|
} else {
|
|
newData = [...data.list].filter(item => (
|
|
filterBy.some(key => {
|
|
const value = item[key]?.toLowerCase();
|
|
return value && value.includes(search.toLowerCase());
|
|
})
|
|
))
|
|
}
|
|
|
|
if (sort === 'asc') {
|
|
newData.sort((a, b) => (dayjs(a[sortBy]).isAfter(dayjs(b[sortBy])) ? 1 : -1))
|
|
} else {
|
|
newData.sort((b, a) => (dayjs(a[sortBy]).isAfter(dayjs(b[sortBy])) ? 1 : -1))
|
|
}
|
|
setDataFilter(newData)
|
|
}, [data.list, search, sort])
|
|
|
|
|
|
const handleFetchPresenceByID = async (presenceId: number) => {
|
|
try {
|
|
setPresenceId(presenceId)
|
|
setIsFullLoading(true)
|
|
const { data: res } = await axios.get('/application-form/approval/info/' + presenceId)
|
|
// console.log(res.data)
|
|
setDataPresence(res.data)
|
|
setShowDialog(true)
|
|
} catch (error: any) {
|
|
toast.error(`Fail to fetch presence`, {
|
|
description: `(${error.response.data.code || error.response.status || '-'}) ${error.response.data.message || error.response.statusText || error.message}`
|
|
})
|
|
} finally {
|
|
setIsFullLoading(false)
|
|
}
|
|
}
|
|
|
|
const handleApproval = async () => {
|
|
try {
|
|
setIsLoading(true)
|
|
const { data: res } = await axios.put('/application-form/approval/' + presenceId, {
|
|
"approve_status": isApproval
|
|
})
|
|
|
|
toast.success(isApproval ? 'Success Approval' : 'Success Decline', {
|
|
description: res.message,
|
|
action: <Button variant='outline' onClick={() => router.push('/admin/report/leave-permit-sick')}>See approval</Button>,
|
|
})
|
|
setShowDialog(false)
|
|
setShowConfirm(false)
|
|
router.refresh()
|
|
} catch (error: any) {
|
|
toastError(toast, `Failed approval, (${error.response.data.code || error.response.status || '-'}) ${error.response.data.message || error.response.statusText || error.message}`)
|
|
} finally {
|
|
setIsLoading(false)
|
|
}
|
|
}
|
|
|
|
console.log(dataPresence)
|
|
// console.log(dataFilter)
|
|
return (
|
|
<>
|
|
<FilterDataDashboard search={search} onSearch={e => setSearch(e.target.value)} sort={sort} onSortChange={val => setSort(val)} />
|
|
<ScrollArea className={cn("hidden sm:block px-6", className)} height='max-h-[325px]'>
|
|
<div className="space-y-8">
|
|
{data && data.list?.length > 0 ?
|
|
dataFilter.map((item: any, key: number) =>
|
|
<div className="grid grid-cols-8" key={key}>
|
|
<div className="flex items-center col-span-4">
|
|
<Suspense fallback={<FallbackAvatar />}>
|
|
<ViewAvatar avatar={item.avatar} />
|
|
</Suspense>
|
|
<div className="ml-4 space-y-0.5 max-w-[70%] 2xl:max-w-[80%]">
|
|
<p className="text-xs sm:text-sm font-medium leading-none">{item.fullname}</p>
|
|
<p className="text-xs md:text-sm text-muted-foreground">
|
|
{item.position_name || '--'}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<div className='col-span-2 flex items-center text-2xl'>
|
|
<i className="ti ti-window-maximize text-his cursor-pointer hover:opacity-70 duration-300" onClick={() => handleFetchPresenceByID(item.application_form_id)} title='Approval Leave and Sick'></i>
|
|
</div>
|
|
<div className="col-span-2 text-xs text-right space-y-1">
|
|
<Badge variant={getVariantWorkingStatus(item.category?.toUpperCase())}>{item.category}</Badge>
|
|
<p className="font-semibold">{dayjs(item.start_date).format('MMM DD, YYYY')}</p>
|
|
</div>
|
|
</div>
|
|
)
|
|
:
|
|
<div className="h-32 w-full flex justify-center items-center" >
|
|
No results.
|
|
</div>
|
|
}
|
|
</div>
|
|
</ScrollArea>
|
|
{/* MOBILE VIEW */}
|
|
<div className={cn("overflow-y-auto block sm:hidden max-h-[325px] px-6", className)}>
|
|
<div className="w-full -mt-2">
|
|
{data && data.list?.length > 0 ?
|
|
dataFilter.map((item: any, key: number) =>
|
|
<div className="flex justify-between border-b border-zinc-300 dark:border-zinc-800 pt-3 pb-3.5" key={key} onClick={() => handleFetchPresenceByID(item.application_form_id)}>
|
|
<div className="flex">
|
|
<Suspense fallback={<FallbackAvatar />}>
|
|
<Avatar avatar={item.avatar} className='w-10 h-10' sizes='40px' />
|
|
</Suspense>
|
|
<div className="ml-3.5 space-y-0.5">
|
|
<p className="text-sm font-medium leading-none">{item.fullname}</p>
|
|
<p className="text-xs text-muted-foreground pb-2">
|
|
{item.position_name || '--'}
|
|
</p>
|
|
<p className="text-xs font-semibold text-muted-foreground border-t border-zinc-300 dark:border-zinc-800 pt-1 w-max">{dayjs(item.start_date).format('MMM DD, YYYY')}</p>
|
|
</div>
|
|
</div>
|
|
<div className="text-xs w-max">
|
|
<Badge variant={getVariantWorkingStatus(item.category?.toUpperCase())}>{item.category}</Badge>
|
|
</div>
|
|
</div>
|
|
)
|
|
:
|
|
<div className="h-32 w-full flex justify-center items-center" >
|
|
No results.
|
|
</div>
|
|
}
|
|
</div>
|
|
</div>
|
|
|
|
{showDialog &&
|
|
<AlertDialog open={true}>
|
|
<AlertDialogContent className='max-w-full md:max-w-[760px] py-6 md:px-10 overflow-y-auto max-h-[95vh]'>
|
|
<AlertDialogHeader className='flex flex-row justify-between'>
|
|
<div>
|
|
<AlertDialogTitle>Detail Application</AlertDialogTitle>
|
|
<AlertDialogDescription>
|
|
This action cannot be undone. This will permanently approve or decline this application form.
|
|
</AlertDialogDescription>
|
|
</div>
|
|
<Button variant='outline' size='icon' disabled={isLoading} onClick={() => setShowDialog(false)}><i className="ti ti-x text-xl"></i ></Button>
|
|
</AlertDialogHeader>
|
|
<div className='grid grid-cols-1 md:grid-cols-5 text-center md:text-left text-sm md:text-base md:my-3'>
|
|
<div className='col-span-2 flex flex-col items-center md:items-start'>
|
|
<Suspense fallback={<FallbackAvatar className='w-52 h-52 shadow' />}>
|
|
<ViewAvatar avatar={dataPresence?.avatar} className='w-52 h-52 sm:w-52 sm:h-52 shadow' sizes='208px' />
|
|
</Suspense>
|
|
<span className='text-base font-semibold mt-2'>{dataPresence.user.fullname}</span>
|
|
<span className='text-muted-foreground'>{dataPresence?.position_name || dataPresence?.user.nickname}</span>
|
|
</div>
|
|
<div className='col-span-3 space-y-4 mt-5 md:mt-0'>
|
|
<div>
|
|
<p className='text-muted-foreground text-xs md:text-sm font-medium'>Category</p>
|
|
<Badge variant={getVariantWorkingStatus(dataPresence.category.name)}>{dataPresence.category.name}</Badge>
|
|
</div>
|
|
<div>
|
|
<p className='text-muted-foreground text-xs md:text-sm font-medium'>Address</p>
|
|
<p>{dataPresence.contact_street}, {dataPresence.contact_city}</p>
|
|
</div>
|
|
<div className='flex flex-col'>
|
|
<p className='text-muted-foreground text-xs md:text-sm font-medium'>Period</p>
|
|
<p>{dayjs(dataPresence.start_date).format('dddd, DD MMM YYYY')} <span className='text-muted-foreground'>to</span> {dayjs(dataPresence.end_date).format('dddd, DD MMM YYYY')}</p>
|
|
</div>
|
|
<div>
|
|
<p className='text-muted-foreground text-xs md:text-sm font-medium'>Return to work</p>
|
|
<p>{dayjs(dataPresence.return_to_work_date).format('dddd, DD MMMM YYYY')}</p>
|
|
</div>
|
|
<div>
|
|
<p className='text-muted-foreground text-xs md:text-sm font-medium'>Notes</p>
|
|
<p className='wrap-break-word'>{dataPresence.note || '-'}</p>
|
|
{dataPresence.tag.length > 0 && dataPresence.tag.map((item: string) =>
|
|
<Badge variant='secondary' className='mr-1' key={item}>{item}</Badge>
|
|
)}
|
|
</div>
|
|
<div>
|
|
<p className='text-muted-foreground text-xs md:text-sm font-medium'>Send to</p>
|
|
<p>{dataPresence.superior}</p>
|
|
</div>
|
|
<div>
|
|
<p className='text-muted-foreground text-xs md:text-sm font-medium'>Mentions to</p>
|
|
<p>{dataPresence.cc.length > 0 ? dataPresence.cc.join(', ') : '-'}</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel disabled={isLoading} onClick={() => { setIsApproval(false); setShowConfirm(true) }} className='text-red-500 font-semibold'>
|
|
Decline
|
|
</AlertDialogCancel>
|
|
<AlertDialogAction disabled={isLoading} onClick={() => { setIsApproval(true); setShowConfirm(true) }}>
|
|
{isLoading && (
|
|
<Icons.spinner className="mr-1 h-4 w-4 animate-spin" />
|
|
)}
|
|
Approve
|
|
</AlertDialogAction>
|
|
</AlertDialogFooter>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
}
|
|
{showConfirm &&
|
|
<AlertDialog open={true}>
|
|
<AlertDialogContent>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>Approval Confirmation</AlertDialogTitle>
|
|
<AlertDialogDescription>
|
|
This action cannot be undone. This will permanently <span className='font-bold'>{isApproval ? 'Approve' : 'Decline'}</span> this application form.
|
|
</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel disabled={isLoading} onClick={() => setShowConfirm(false)} className='text-red-500 font-semibold'>
|
|
Cancel
|
|
</AlertDialogCancel>
|
|
<AlertDialogAction disabled={isLoading} onClick={handleApproval}>
|
|
{isLoading && (
|
|
<Icons.spinner className="mr-1 h-4 w-4 animate-spin" />
|
|
)}
|
|
Continue
|
|
</AlertDialogAction>
|
|
</AlertDialogFooter>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
}
|
|
{isFullLoading && <PageLoading className='bg-opacity-40 dark:bg-opacity-20 z-999' />}
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default ApprovalLeaveSick; |