mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 11:06:29 +00:00
126 lines
5.6 KiB
TypeScript
126 lines
5.6 KiB
TypeScript
import { FC } from 'react';
|
|
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
|
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '../ui/select';
|
|
import { ChevronLeftIcon, ChevronRightIcon, DoubleArrowLeftIcon, DoubleArrowRightIcon } from '@radix-ui/react-icons';
|
|
import { Button } from '../ui/button';
|
|
|
|
interface Props {
|
|
totalRowinTable?: number
|
|
totalRows: number
|
|
className?: string
|
|
}
|
|
|
|
const Pagination: FC<Props> = ({ totalRows, totalRowinTable, className }) => {
|
|
const router = useRouter()
|
|
const searchParams = useSearchParams();
|
|
const path = usePathname()
|
|
|
|
const savedTab = searchParams!.get('tab') as string || ''
|
|
const startDate = searchParams!.get('start_date') as string || ''
|
|
const endDate = searchParams!.get('end_date') as string || ''
|
|
const savedSort = searchParams!.get('sort') as string || ''
|
|
const savedPage = Number(searchParams!.get('page') as string) || 1;
|
|
const savedLimit = Number(searchParams!.get('limit') as string) || 10;
|
|
const savedSearch = searchParams!.get('search') as string || ''
|
|
|
|
const totalPage = Math.ceil(totalRows / savedLimit);
|
|
const lastPage = totalPage === savedPage
|
|
|
|
const handleGetQuery = () => {
|
|
const query = new URLSearchParams();
|
|
if (savedTab) query.set('tab', savedTab.toString());
|
|
if (savedSort) query.set('sort', savedSort.toString());
|
|
if (savedSearch) query.set('search', savedSearch.toString());
|
|
if (startDate) query.set('start_date', startDate.toString());
|
|
if (endDate) query.set('end_date', endDate.toString());
|
|
|
|
return query
|
|
}
|
|
|
|
const handlePageChange = (page: number) => {
|
|
const existQuery = handleGetQuery()
|
|
|
|
const url = `${path}?page=${page}&limit=${savedLimit}&${existQuery}`
|
|
router.replace(url)
|
|
}
|
|
|
|
const handleLimitChange = (val: string) => {
|
|
const existQuery = handleGetQuery()
|
|
|
|
const url = `${path}?page=${1}&limit=${val}&${existQuery}`
|
|
router.replace(url)
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<div className="flex items-center justify-between px-2">
|
|
<div className="flex-1 text-sm text-muted-foreground">
|
|
Total {totalRowinTable || totalRows} row(s) data.
|
|
</div>
|
|
<div className="flex items-center space-x-6 lg:space-x-8">
|
|
<div className="hidden md:flex items-center space-x-2">
|
|
<p className="text-sm font-medium">Rows per page</p>
|
|
<Select
|
|
value={savedLimit.toString()}
|
|
onValueChange={(value) => handleLimitChange(value)}
|
|
>
|
|
<SelectTrigger className="h-8 w-[70px]">
|
|
<SelectValue placeholder='10' />
|
|
</SelectTrigger>
|
|
<SelectContent side="top">
|
|
{[10, 20, 30, 40, 50].map((pageSize) => (
|
|
<SelectItem key={pageSize} value={`${pageSize}`}>
|
|
{pageSize}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
<div className="hidden md:flex w-[100px] items-center justify-center text-sm font-medium">
|
|
Page {savedPage} of {totalPage}
|
|
</div>
|
|
<div className="flex items-center space-x-2">
|
|
<Button
|
|
variant="outline"
|
|
className="hidden h-8 w-8 p-0 lg:flex"
|
|
onClick={() => handlePageChange(1)}
|
|
disabled={savedPage === 1 || totalPage == 0}
|
|
>
|
|
<span className="sr-only">Go to first page</span>
|
|
<DoubleArrowLeftIcon className="h-4 w-4" />
|
|
</Button>
|
|
<Button
|
|
variant="outline"
|
|
className="h-8 w-8 p-0"
|
|
onClick={() => handlePageChange(savedPage - 1)}
|
|
disabled={savedPage === 1 || totalPage == 0}
|
|
>
|
|
<span className="sr-only">Go to previous page</span>
|
|
<ChevronLeftIcon className="h-4 w-4" />
|
|
</Button>
|
|
<Button
|
|
variant="outline"
|
|
className="h-8 w-8 p-0"
|
|
onClick={() => handlePageChange(savedPage + 1)}
|
|
disabled={lastPage || totalPage == 0}
|
|
>
|
|
<span className="sr-only">Go to next page</span>
|
|
<ChevronRightIcon className="h-4 w-4" />
|
|
</Button>
|
|
<Button
|
|
variant="outline"
|
|
className="hidden h-8 w-8 p-0 lg:flex"
|
|
onClick={() => handlePageChange(totalPage)}
|
|
disabled={lastPage || totalPage == 0}
|
|
>
|
|
<span className="sr-only">Go to last page</span>
|
|
<DoubleArrowRightIcon className="h-4 w-4" />
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default Pagination; |