-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feat]: Timesheet-Pagination-Hook-and-Component (#3485)
* feat:Add custom hook for paginating grouped timesheet data * feat:Added TimesheetPagination component for managing pagination of timesheet data. * feat:Added TimesheetPagination component for managing pagination of timesheet data.
- Loading branch information
1 parent
e6a9b33
commit eacf931
Showing
6 changed files
with
390 additions
and
5 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
85 changes: 85 additions & 0 deletions
85
apps/web/app/[locale]/timesheet/[memberId]/components/TimesheetPagination.tsx
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,85 @@ | ||
import { Pagination, PaginationContent, PaginationEllipsis, PaginationItem, PaginationLink } from '@components/ui/pagination' | ||
import React from 'react' | ||
import { MdKeyboardDoubleArrowLeft, MdKeyboardDoubleArrowRight } from 'react-icons/md'; | ||
interface TimesheetPaginationProps { | ||
totalPages?: number; | ||
onPageChange?: (page: number) => void; | ||
nextPage?: () => void; | ||
previousPage?: () => void; | ||
goToPage: (page: number) => void; | ||
currentPage?: number; | ||
getPageNumbers: () => (number | string)[]; | ||
dates?: string[]; | ||
totalGroups?: number | ||
|
||
} | ||
|
||
/** | ||
* A component for paginating timesheet data. | ||
* | ||
* @param {TimesheetPaginationProps} props - The props for the component. | ||
* @param {number} [props.totalPages] - The total number of pages. | ||
* @param {(page: number) => void} [props.onPageChange] - A function to call when the page is changed. | ||
* @param {(page: number) => void} props.goToPage - A function to call when the user navigates to a specific page. | ||
* @param {() => void} props.nextPage - A function to call when the user navigates to the next page. | ||
* @param {() => void} props.previousPage - A function to call when the user navigates to the previous page. | ||
* @param {number} [props.currentPage] - The current page number. | ||
* @param {() => (number | string)[]} props.getPageNumbers - A function to get an array of page numbers. | ||
* | ||
* @returns {React.ReactElement} - The component element. | ||
*/ | ||
function TimesheetPagination({ totalPages, onPageChange, goToPage, nextPage, previousPage, currentPage, getPageNumbers, dates, totalGroups }: TimesheetPaginationProps) { | ||
return ( | ||
// totalPages > 1 | ||
<> | ||
{totalPages && totalPages > 1 && ( | ||
|
||
<Pagination className='flex flex-row justify-between items-center gap-[327px] w-full h-[64px] rounded-b-[6px] p-1'> | ||
<div className='text-[#7E7991] font-medium w-full'> | ||
Page {currentPage} of {totalPages} ({dates?.length} items of {totalGroups}) | ||
</div> | ||
<PaginationContent className='flex w-full justify-end'> | ||
|
||
<PaginationItem> | ||
<button | ||
className='box-border flex flex-row justify-center items-center p-2 gap-2 w-8 h-8 bg-white dark:bg-dark--theme-light border border-gray-200 dark:border-gray-800 shadow-sm rounded-[6px]' | ||
onClick={previousPage} | ||
disabled={currentPage === 1} > | ||
<MdKeyboardDoubleArrowLeft /> | ||
|
||
</button> | ||
</PaginationItem> | ||
{getPageNumbers().map((pageNumber, index) => ( | ||
<PaginationItem key={index}> | ||
{pageNumber === '...' ? ( | ||
<PaginationEllipsis | ||
className='box-border cursor-pointer flex flex-row justify-center items-center p-2 gap-2 w-8 h-8 dark:bg-dark--theme-light border border-gray-200 dark:border-gray-800 shadow-sm rounded-[6px]' | ||
/> | ||
) : ( | ||
<PaginationLink | ||
className='box-border cursor-pointer flex flex-row justify-center items-center p-2 gap-2 w-8 h-8 dark:bg-dark--theme-light border border-gray-200 dark:border-gray-800 shadow-sm rounded-[6px]' | ||
isActive={currentPage === pageNumber} | ||
onClick={() => goToPage(pageNumber as number)}> | ||
{pageNumber} | ||
</PaginationLink> | ||
)} | ||
</PaginationItem> | ||
))} | ||
<PaginationItem> | ||
<button | ||
disabled={currentPage === totalPages} | ||
className='box-border flex flex-row justify-center items-center p-2 gap-2 w-8 h-8 dark:bg-dark--theme-light border border-gray-200 dark:border-gray-800 shadow-sm rounded-[6px]' | ||
onClick={nextPage}> | ||
<MdKeyboardDoubleArrowRight /> | ||
</button> | ||
</PaginationItem> | ||
</PaginationContent> | ||
</Pagination> | ||
) | ||
} | ||
</> | ||
) | ||
|
||
} | ||
|
||
export default TimesheetPagination |
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,123 @@ | ||
import { useState, useMemo } from 'react'; | ||
import { TimesheetLog } from '@/app/interfaces'; | ||
|
||
export interface GroupedTimesheet { | ||
date: string; | ||
tasks: TimesheetLog[]; | ||
} | ||
|
||
interface PaginationState { | ||
currentPage: number; | ||
totalPages: number; | ||
totalGroups: number; | ||
totalTasks: number; | ||
dates: string[]; | ||
} | ||
|
||
interface UseTimesheetPaginationProps { | ||
data: GroupedTimesheet[]; | ||
pageSize?: number; | ||
} | ||
|
||
/** | ||
* Custom hook for paginating grouped timesheet data. | ||
* | ||
* @param {GroupedTimesheet[]} data - An array of grouped timesheet data, each group containing a date and associated tasks. | ||
* @param {number} [pageSize=10] - The number of groups to show per page. | ||
* | ||
* @returns {Object} - An object containing pagination details and functions. | ||
* @property {GroupedTimesheet[]} paginatedGroups - The currently visible groups based on pagination. | ||
* @property {number} currentPage - The current page number. | ||
* @property {number} totalPages - The total number of pages available. | ||
* @property {number} totalGroups - The total number of groups in the data. | ||
* @property {number} totalTasks - The total number of tasks across all groups. | ||
* @property {string[]} dates - The dates of the currently visible groups. | ||
* @property {function} goToPage - A function to navigate to a specific page. | ||
* @property {function} nextPage - A function to navigate to the next page. | ||
* @property {function} previousPage - A function to navigate to the previous page. | ||
* @property {function} getPageNumbers - A function to get an array of page numbers for pagination controls. | ||
*/ | ||
|
||
export function useTimesheetPagination({ | ||
data, | ||
pageSize = 10, | ||
}: UseTimesheetPaginationProps) { | ||
const [currentPage, setCurrentPage] = useState(1); | ||
|
||
const paginationState = useMemo<PaginationState>(() => { | ||
const totalGroups = data.length; | ||
const totalPages = Math.max(1, Math.ceil(totalGroups / pageSize)); | ||
const validCurrentPage = Math.min(currentPage, totalPages); | ||
|
||
const startIndex = (validCurrentPage - 1) * pageSize; | ||
const endIndex = Math.min(startIndex + pageSize, totalGroups); | ||
const paginatedDates = data | ||
.slice(startIndex, endIndex) | ||
.map(group => group.date); | ||
|
||
const totalTasks = data.reduce((sum, group) => sum + group.tasks.length, 0); | ||
|
||
return { | ||
currentPage: validCurrentPage, | ||
totalPages, | ||
totalGroups, | ||
totalTasks, | ||
dates: paginatedDates, | ||
}; | ||
}, [data, pageSize, currentPage]); | ||
|
||
const paginatedGroups = useMemo(() => { | ||
const startIndex = (paginationState.currentPage - 1) * pageSize; | ||
const endIndex = Math.min(startIndex + pageSize, data.length); | ||
return data.slice(startIndex, endIndex); | ||
}, [data, pageSize, paginationState.currentPage]); | ||
|
||
const goToPage = (page: number) => { | ||
setCurrentPage(Math.max(1, Math.min(page, paginationState.totalPages))); | ||
}; | ||
|
||
const nextPage = () => { | ||
if (currentPage < paginationState.totalPages) { | ||
goToPage(currentPage + 1); | ||
} | ||
}; | ||
|
||
const previousPage = () => { | ||
if (currentPage > 1) { | ||
goToPage(currentPage - 1); | ||
} | ||
}; | ||
|
||
const getPageNumbers = (): (number | string)[] => { | ||
const { currentPage, totalPages } = paginationState; | ||
const delta = 2; | ||
const range: (number | string)[] = []; | ||
|
||
for (let i = 1; i <= totalPages; i++) { | ||
if ( | ||
i === 1 || | ||
i === totalPages || | ||
(i >= currentPage - delta && i <= currentPage + delta) | ||
) { | ||
range.push(i); | ||
} else if (range[range.length - 1] !== '...') { | ||
range.push('...'); | ||
} | ||
} | ||
|
||
return range; | ||
}; | ||
|
||
return { | ||
paginatedGroups, | ||
currentPage: paginationState.currentPage, | ||
totalPages: paginationState.totalPages, | ||
totalGroups: paginationState.totalGroups, | ||
totalTasks: paginationState.totalTasks, | ||
dates: paginationState.dates, | ||
goToPage, | ||
nextPage, | ||
previousPage, | ||
getPageNumbers, | ||
}; | ||
} |
Oops, something went wrong.