-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Pagination to the Babies and Caregivers Tables (#136)
* feat: pagination component * feat: pagination backend and adding to component, creating pagination component * fix: page size and svg arrow symbols * Move consts to utils to fix build error
- Loading branch information
Showing
22 changed files
with
1,235 additions
and
504 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { PAGINATION_PAGE_SIZE } from "db/consts"; | ||
import React from "react"; | ||
|
||
const pageSize = PAGINATION_PAGE_SIZE; | ||
|
||
const getFirstRecordOnPage = (currPage: number) => | ||
(currPage - 1) * pageSize + 1; | ||
|
||
const getLastRecordOnPage = ( | ||
firstRecordOnPage: number, | ||
totalRecords: number | ||
) => Math.min(firstRecordOnPage + pageSize - 1, totalRecords); | ||
|
||
const isValidPage = (firstRecordOnPage: number, totalRecords: number) => | ||
firstRecordOnPage > 0 && firstRecordOnPage <= totalRecords; | ||
|
||
const LeftArrow = () => ( | ||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"> | ||
<path d="M15.1599 7.41L10.5799 12L15.1599 16.59L13.7499 18L7.74991 12L13.7499 6L15.1599 7.41Z" fill="currentColor"/> | ||
</svg> | ||
); | ||
|
||
const RightArrow = () => ( | ||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"> | ||
<path d="M8.84009 7.41L13.4201 12L8.84009 16.59L10.2501 18L16.2501 12L10.2501 6L8.84009 7.41Z" fill="currentColor"/> | ||
</svg> | ||
); | ||
|
||
const getArrows = ( | ||
firstRecordOnPage: number, | ||
totalRecords: number, | ||
currPage: number, | ||
onNextPage: any, | ||
onPrevPage: any | ||
) => [ | ||
{ symbol: <LeftArrow/>, disabled: firstRecordOnPage <= pageSize, onClick: onPrevPage }, | ||
{ symbol: <RightArrow/>, disabled: totalRecords <= currPage * pageSize, onClick: onNextPage }, | ||
]; | ||
|
||
const ArrowButton = ({ | ||
symbol, | ||
disabled, | ||
onClick | ||
}: { | ||
symbol: JSX.Element; | ||
disabled: boolean; | ||
onClick: () => void; | ||
}) => ( | ||
<div | ||
className={`w-8 h-8 rounded border flex justify-center items-center font-bold text-xl ${ | ||
disabled ? "text-gray-300 cursor-not-allowed" : "text-black cursor-pointer" | ||
}`} | ||
onClick={!disabled ? onClick : undefined} | ||
> | ||
{symbol} | ||
</div> | ||
); | ||
|
||
function Pagination({ | ||
totalRecords, | ||
currPage, | ||
onNextPage, | ||
onPrevPage | ||
}: { | ||
totalRecords: number; | ||
currPage: number; | ||
onNextPage: any; | ||
onPrevPage: any; | ||
}) { | ||
const firstRecordOnPage = getFirstRecordOnPage(currPage); | ||
const lastRecordOnPage = getLastRecordOnPage( | ||
firstRecordOnPage, | ||
totalRecords | ||
); | ||
|
||
if (!isValidPage(firstRecordOnPage, totalRecords)) { | ||
return null; | ||
} | ||
|
||
const arrows = getArrows(firstRecordOnPage, totalRecords, currPage, onNextPage, onPrevPage); | ||
|
||
return ( | ||
<div className="flex items-center gap-3 justify-end"> | ||
<h1 className="text-sm font-bold"> | ||
{firstRecordOnPage} - {lastRecordOnPage} of {totalRecords} | ||
</h1> | ||
<div className="flex items-center gap-2"> | ||
{arrows.map(({ symbol, disabled, onClick }, index) => ( | ||
<ArrowButton key={index} symbol={symbol} disabled={disabled} onClick={onClick} /> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default Pagination; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { CollectionPath } from "@lib/types/db"; | ||
import { getTotalRecords } from "db/firebase/getSize"; | ||
import { useEffect, useState } from "react"; | ||
|
||
export function usePaginatedData<T>(fetchPage: (page: number, paginationReferences: Map<number, any>) => Promise<{ data: T[], paginationInfo: any }>, type: CollectionPath) { | ||
const [paginationReferences, setPaginationReferences] = useState<Map<number, any>>(new Map()); | ||
const [currPage, setCurrPage] = useState(1); | ||
const [data, setData] = useState<T[]>([]); | ||
const [totalRecords, setTotalRecords] = useState<number>(0); | ||
|
||
const fetchTotalRecords = async () => { | ||
try { | ||
const numRecords = await getTotalRecords(type); | ||
if (numRecords) { | ||
setTotalRecords(numRecords); | ||
} | ||
} catch (error) { | ||
console.error('Error fetching records.'); | ||
} | ||
}; | ||
|
||
const updatePaginationReferences = async (paginationInfo: any) => { | ||
setPaginationReferences(prev => { | ||
const newRefs = new Map(prev); | ||
newRefs.set(currPage, paginationInfo); | ||
return newRefs; | ||
}); | ||
}; | ||
|
||
const fetchPageData = async () => { | ||
const newPageData = await fetchPage(currPage, paginationReferences); | ||
setData(newPageData.data); | ||
updatePaginationReferences(newPageData.paginationInfo); | ||
}; | ||
|
||
useEffect(() => { | ||
fetchPageData(); | ||
fetchTotalRecords(); | ||
}, [currPage]); | ||
|
||
return { | ||
data, | ||
totalRecords, | ||
currPage, | ||
setCurrPage, | ||
onNextPage: () => setCurrPage(prev => prev + 1), | ||
onPrevPage: () => setCurrPage(prev => prev - 1) | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from "react"; | ||
import Pagination from "@components/molecules/Pagination/Pagination"; | ||
import CaretakerTable from "@components/tables/CaretakerTable"; | ||
import BabiesTable from "@components/tables/BabiesTable"; | ||
import { BABIES_TAB, CAREGIVERS_TAB } from "@lib/utils/consts"; | ||
|
||
export default function PaginatedTable({type, tableProps, paginatedProps, onNextPage, onPrevPage}: any) { | ||
return ( | ||
<div className="flex flex-col gap-4 mb-3 w-[65vw]"> | ||
<div className="overflow-auto h-[70vh]"> | ||
{(type == BABIES_TAB) ? <BabiesTable props={tableProps} /> : | ||
(type == CAREGIVERS_TAB)? <CaretakerTable props={tableProps}/> : <></>} | ||
</div> | ||
<div> | ||
<Pagination | ||
totalRecords={paginatedProps.totalRecords} | ||
currPage={paginatedProps.pageNumber} | ||
onNextPage={onNextPage} | ||
onPrevPage={onPrevPage} | ||
/> | ||
</div> | ||
</div>) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.