-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: optimize timesheet filtering and view logic (#3520)
- Loading branch information
1 parent
0294f61
commit 7e8e15e
Showing
3 changed files
with
120 additions
and
23 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
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,64 @@ | ||
import { useMemo } from 'react'; | ||
import { FilterStatus } from '@/app/[locale]/timesheet/[memberId]/components/FilterWithStatus'; | ||
import { GroupedTimesheet } from './useTimesheet'; | ||
import { TimesheetLog, TimesheetStatus } from '@/app/interfaces'; | ||
import { useLocalStorageState } from '../useLocalStorageState'; | ||
|
||
export const useTimesheetFilters = (data?: GroupedTimesheet[]) => { | ||
const [activeStatus, setActiveStatus] = useLocalStorageState<FilterStatus>('timesheet-filter-status', 'All Tasks'); | ||
|
||
|
||
const filteredData = useMemo(() => { | ||
if (!data) return []; | ||
|
||
return data.map(group => { | ||
type FilterStatusWithoutAll = Exclude<FilterStatus, 'All Tasks'>; | ||
|
||
const statusMap: Record<FilterStatusWithoutAll, TimesheetStatus> = { | ||
'Pending': 'PENDING', | ||
'Approved': 'APPROVED', | ||
'In review': 'IN REVIEW', | ||
'Draft': 'DRAFT', | ||
'Rejected': 'DENIED' | ||
}; | ||
|
||
const filteredTasks = group.tasks.filter(task => { | ||
if (activeStatus === 'All Tasks') { | ||
return true; | ||
} | ||
return task.timesheet.status === statusMap[activeStatus as FilterStatusWithoutAll]; | ||
}); | ||
|
||
return { | ||
...group, | ||
tasks: filteredTasks | ||
}; | ||
}).filter(group => group.tasks.length > 0); | ||
}, [data, activeStatus]); | ||
|
||
const statusData = useMemo(() => { | ||
const emptyStatusData: Record<TimesheetStatus, TimesheetLog[]> = { | ||
'DRAFT': [], | ||
'PENDING': [], | ||
'IN REVIEW': [], | ||
'DENIED': [], | ||
'APPROVED': [] | ||
}; | ||
|
||
if (!data) return emptyStatusData; | ||
|
||
const allTasks = data.flatMap(group => group.tasks); | ||
return allTasks.reduce((acc, task) => { | ||
const status = task.timesheet.status as TimesheetStatus; | ||
acc[status].push(task); | ||
return acc; | ||
}, { ...emptyStatusData }); | ||
}, [data]); | ||
|
||
return { | ||
activeStatus, | ||
setActiveStatus, | ||
filteredData, | ||
statusData | ||
}; | ||
}; |
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,29 @@ | ||
import { useMemo } from 'react'; | ||
import { GroupedTimesheet } from './useTimesheet'; | ||
|
||
type ViewMode = 'ListView' | 'CalendarView'; | ||
type GroupByDays = 'Daily' | 'Weekly' | 'Monthly'; | ||
|
||
interface TimesheetViewDataProps { | ||
timesheetNavigator: ViewMode; | ||
timesheetGroupByDays: GroupByDays; | ||
paginatedGroups?: GroupedTimesheet[]; | ||
filterDataTimesheet?: GroupedTimesheet[]; | ||
} | ||
|
||
export const useTimesheetViewData = ({ | ||
timesheetNavigator, | ||
timesheetGroupByDays, | ||
paginatedGroups, | ||
filterDataTimesheet | ||
}: TimesheetViewDataProps) => { | ||
const viewData = useMemo(() => { | ||
const shouldUsePaginatedGroups = | ||
timesheetNavigator === 'ListView' || | ||
(timesheetGroupByDays === 'Daily' && timesheetNavigator === 'CalendarView'); | ||
|
||
return shouldUsePaginatedGroups ? paginatedGroups : filterDataTimesheet; | ||
}, [timesheetNavigator, timesheetGroupByDays, paginatedGroups, filterDataTimesheet]); | ||
|
||
return viewData; | ||
}; |