Skip to content

Commit

Permalink
[Feat]: Timesheet-Pagination-Hook-and-Component (#3485)
Browse files Browse the repository at this point in the history
* 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
Innocent-Akim authored Dec 27, 2024
1 parent e6a9b33 commit eacf931
Show file tree
Hide file tree
Showing 6 changed files with 390 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,24 @@ export const PlusIcon = () => <svg width="16" height="16" viewBox="0 0 16 16" fi
<path d="M3.99609 8H11.9961" stroke="#282048" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
<path d="M7.99609 12V4" stroke="#282048" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
</svg>


/**
* ArrowLeftIcon
*
* Renders an arrow pointing left using SVG.
*
* @returns {React.ReactElement} - The rendered arrow left icon component.
*/
export const ArrowLeftIcon = () => <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M6.95345 8.02279L6.95346 8.02278C6.96648 8.00977 6.96648 7.98868 6.95346 7.97566L6.95345 7.97566L2.68679 3.70899L2.68678 3.70898C2.67376 3.69597 2.65267 3.69597 2.63966 3.70898L2.63964 3.709C2.62663 3.72201 2.62663 3.7431 2.63964 3.75611L2.63965 3.75612L6.52919 7.64567L6.88274 7.99922L6.52919 8.35278L2.63968 12.2422C2.63967 12.2423 2.63966 12.2423 2.63965 12.2423M6.95345 8.02279L2.2861 12.6431C2.07781 12.4347 2.07781 12.097 2.2861 11.8887L2.63965 12.2423M6.95345 8.02279L2.68684 12.2895M6.95345 8.02279L2.6396 12.2895M2.63965 12.2423C2.62664 12.2553 2.62663 12.2764 2.6396 12.2895M2.63965 12.2423L2.6396 12.2895M2.6396 12.2895C2.65263 12.3025 2.67381 12.3025 2.68684 12.2895M2.68684 12.2895L2.68678 12.2895L3.04034 12.6431L2.68684 12.2895ZM9.08679 3.70899L9.08678 3.70898C9.07376 3.69597 9.05267 3.69597 9.03966 3.70898L9.03964 3.709C9.02663 3.72201 9.02663 3.7431 9.03964 3.75611L9.03965 3.75612L12.9292 7.64567L13.2828 7.99922L12.9292 8.35278L9.03968 12.2422C9.02662 12.2553 9.02662 12.2765 9.03968 12.2895C9.0527 12.3025 9.07378 12.3025 9.08679 12.2895L9.08679 3.70899ZM9.08679 3.70899L13.3535 7.9756M9.08679 3.70899L13.3535 7.9756M13.3535 7.9756L13.3535 7.97566C13.3665 7.98867 13.3665 8.00977 13.3535 8.02279L13.3535 7.9756ZM13.3535 8.02284L9.08684 12.2895L13.3535 8.02284Z" fill="#09090B" stroke="#18181B" />
</svg>






export const ArrowRightIcon = () => <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M6.95345 8.02279L6.95346 8.02278C6.96648 8.00977 6.96648 7.98868 6.95346 7.97566L6.95345 7.97566L2.68679 3.70899L2.68678 3.70898C2.67376 3.69597 2.65267 3.69597 2.63966 3.70898L2.63964 3.709C2.62663 3.72201 2.62663 3.7431 2.63964 3.75611L2.63965 3.75612L6.52919 7.64567L6.88274 7.99922L6.52919 8.35278L2.63968 12.2422C2.63967 12.2423 2.63966 12.2423 2.63965 12.2423M6.95345 8.02279L2.2861 12.6431C2.07781 12.4347 2.07781 12.097 2.2861 11.8887L2.63965 12.2423M6.95345 8.02279L2.68684 12.2895M6.95345 8.02279L2.6396 12.2895M2.63965 12.2423C2.62664 12.2553 2.62663 12.2764 2.6396 12.2895M2.63965 12.2423L2.6396 12.2895M2.6396 12.2895C2.65263 12.3025 2.67381 12.3025 2.68684 12.2895M2.68684 12.2895L2.68678 12.2895L3.04034 12.6431L2.68684 12.2895ZM9.08679 3.70899L9.08678 3.70898C9.07376 3.69597 9.05267 3.69597 9.03966 3.70898L9.03964 3.709C9.02663 3.72201 9.02663 3.7431 9.03964 3.75611L9.03965 3.75612L12.9292 7.64567L13.2828 7.99922L12.9292 8.35278L9.03968 12.2422C9.02662 12.2553 9.02662 12.2765 9.03968 12.2895C9.0527 12.3025 9.07378 12.3025 9.08679 12.2895L9.08679 3.70899ZM9.08679 3.70899L13.3535 7.9756M9.08679 3.70899L13.3535 7.9756M13.3535 7.9756L13.3535 7.97566C13.3665 7.98867 13.3665 8.00977 13.3535 8.02279L13.3535 7.9756ZM13.3535 8.02284L9.08684 12.2895L13.3535 8.02284Z" fill="#09090B" stroke="#18181B" />
</svg>
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
47 changes: 44 additions & 3 deletions apps/web/app/[locale]/timesheet/[memberId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ import { differenceBetweenHours, getGreeting, secondsToTime } from '@/app/helper
import { useTimesheet } from '@/app/hooks/features/useTimesheet';
import { endOfMonth, startOfMonth } from 'date-fns';
import TimesheetDetailModal from './components/TimesheetDetailModal';
import { useTimesheetPagination } from '@/app/hooks/features/useTimesheetPagination';
import TimesheetPagination from './components/TimesheetPagination';

type TimesheetViewMode = 'ListView' | 'CalendarView';
export type TimesheetDetailMode = 'Pending' | 'MenHours' | 'MemberWork';
const TIMESHEET_PAGE_SIZE = 10;

type ViewToggleButtonProps = {
mode: TimesheetViewMode;
Expand Down Expand Up @@ -52,13 +55,30 @@ const TimeSheet = React.memo(function TimeSheetPage({ params }: { params: { memb
to: endOfMonth(new Date()),
});

const { timesheet: filterDataTimesheet, statusTimesheet, loadingTimesheet, isManage } = useTimesheet({
const { timesheet: filterDataTimesheet, statusTimesheet, loadingTimesheet, isManage, timesheetGroupByDays } = useTimesheet({
startDate: dateRange.from!,
endDate: dateRange.to!,
timesheetViewMode: timesheetNavigator,
inputSearch: search
});

const {
paginatedGroups,
currentPage,
totalPages,
goToPage,
nextPage,
previousPage,
getPageNumbers,
totalGroups,
dates
} = useTimesheetPagination({
data: filterDataTimesheet,
pageSize: TIMESHEET_PAGE_SIZE
});;



React.useEffect(() => {
getOrganizationProjects();
}, [getOrganizationProjects])
Expand Down Expand Up @@ -103,6 +123,10 @@ const TimeSheet = React.memo(function TimeSheetPage({ params }: { params: { memb
],
[activeTeam?.name, currentLocale, t]
);
const shouldRenderPagination =
timesheetNavigator === 'ListView' ||
(timesheetGroupByDays === 'Daily' && timesheetNavigator === 'CalendarView');

return (
<>
{isTimesheetDetailOpen
Expand Down Expand Up @@ -230,16 +254,33 @@ const TimeSheet = React.memo(function TimeSheetPage({ params }: { params: { memb
{timesheetNavigator === 'ListView' ? (
<TimesheetView
user={user}
data={filterDataTimesheet}
data={paginatedGroups}
loading={loadingTimesheet}
/>
) : (
<CalendarView
user={user}
data={filterDataTimesheet}
data={
shouldRenderPagination ?
paginatedGroups :
filterDataTimesheet
}
loading={loadingTimesheet}
/>
)}
{shouldRenderPagination && (
<TimesheetPagination
currentPage={currentPage}
totalPages={totalPages}
onPageChange={goToPage}
getPageNumbers={getPageNumbers}
goToPage={goToPage}
nextPage={nextPage}
previousPage={previousPage}
dates={dates}
totalGroups={totalGroups}
/>
)}
</div>

</Container>
Expand Down
123 changes: 123 additions & 0 deletions apps/web/app/hooks/features/useTimesheetPagination.ts
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,
};
}
Loading

0 comments on commit eacf931

Please sign in to comment.