|
1 | 1 | import UserProfilePublicPage from 'design-library/pages/public-pages/user-profile-public-page/user-profile-public-page' |
2 | | -import React from 'react' |
| 2 | +import React, { useEffect, useCallback } from 'react' |
3 | 3 | import { useParams } from 'react-router-dom' |
4 | 4 |
|
5 | | -const ProfilePage = ({ user, searchUser, tasks, listTasks, filterTasks }) => { |
| 5 | +const TAB_TO_PARAM: Record<string, string> = { |
| 6 | + created: 'userId', |
| 7 | + supported: 'supportedByUserId' |
| 8 | +} |
| 9 | + |
| 10 | +const DEFAULT_ROWS_PER_PAGE = 10 |
| 11 | + |
| 12 | +const ProfilePage = ({ user, searchUser, tasks, listTasks }) => { |
| 13 | + const { userId } = useParams<{ userId: string }>() |
| 14 | + const [page, setPage] = React.useState(0) |
| 15 | + const [rowsPerPage, setRowsPerPage] = React.useState(DEFAULT_ROWS_PER_PAGE) |
| 16 | + const [currentSort, setCurrentSort] = React.useState<{ sortBy?: string; sortDirection?: string }>( |
| 17 | + {} |
| 18 | + ) |
| 19 | + const [currentTab, setCurrentTab] = React.useState('created') |
| 20 | + |
| 21 | + const fetchIssues = useCallback( |
| 22 | + ( |
| 23 | + tabOverride?: string, |
| 24 | + pageOverride?: number, |
| 25 | + rowsOverride?: number, |
| 26 | + sortOverride?: typeof currentSort |
| 27 | + ) => { |
| 28 | + const activeTab = tabOverride ?? currentTab |
| 29 | + const activePage = pageOverride ?? page |
| 30 | + const activeRows = rowsOverride ?? rowsPerPage |
| 31 | + const activeSort = sortOverride ?? currentSort |
| 32 | + const paramKey = TAB_TO_PARAM[activeTab] ?? 'userId' |
| 33 | + listTasks({ |
| 34 | + [paramKey]: userId, |
| 35 | + page: activePage, |
| 36 | + limit: activeRows, |
| 37 | + ...(activeSort.sortBy ? activeSort : {}) |
| 38 | + }) |
| 39 | + }, |
| 40 | + [userId, currentTab, page, rowsPerPage, currentSort, listTasks] |
| 41 | + ) |
| 42 | + |
| 43 | + useEffect(() => { |
| 44 | + if (userId) { |
| 45 | + searchUser({ id: userId }) |
| 46 | + fetchIssues() |
| 47 | + } |
| 48 | + }, [userId]) |
| 49 | + |
| 50 | + const handleTabChange = useCallback( |
| 51 | + (newTab: string) => { |
| 52 | + setCurrentTab(newTab) |
| 53 | + setPage(0) |
| 54 | + setCurrentSort({}) |
| 55 | + fetchIssues(newTab, 0, rowsPerPage, {}) |
| 56 | + }, |
| 57 | + [fetchIssues, rowsPerPage] |
| 58 | + ) |
| 59 | + |
| 60 | + const handlePageChange = useCallback( |
| 61 | + (newPage: number) => { |
| 62 | + setPage(newPage) |
| 63 | + fetchIssues(currentTab, newPage, rowsPerPage, currentSort) |
| 64 | + }, |
| 65 | + [fetchIssues, currentTab, rowsPerPage, currentSort] |
| 66 | + ) |
| 67 | + |
| 68 | + const handleRowsPerPageChange = useCallback( |
| 69 | + (newRowsPerPage: number) => { |
| 70 | + setRowsPerPage(newRowsPerPage) |
| 71 | + setPage(0) |
| 72 | + fetchIssues(currentTab, 0, newRowsPerPage, currentSort) |
| 73 | + }, |
| 74 | + [fetchIssues, currentTab, currentSort] |
| 75 | + ) |
| 76 | + |
| 77 | + const handleSortChange = useCallback( |
| 78 | + (sortBy: string, sortDirection: 'asc' | 'desc' | 'none') => { |
| 79 | + const newSort = sortDirection === 'none' ? {} : { sortBy, sortDirection } |
| 80 | + setCurrentSort(newSort) |
| 81 | + setPage(0) |
| 82 | + fetchIssues(currentTab, 0, rowsPerPage, newSort) |
| 83 | + }, |
| 84 | + [fetchIssues, currentTab, rowsPerPage] |
| 85 | + ) |
| 86 | + |
| 87 | + const serverSidePagination = { |
| 88 | + enabled: true, |
| 89 | + totalCount: tasks.totalCount ?? 0, |
| 90 | + page, |
| 91 | + rowsPerPage, |
| 92 | + onPageChange: handlePageChange, |
| 93 | + onRowsPerPageChange: handleRowsPerPageChange, |
| 94 | + onSortChange: handleSortChange |
| 95 | + } |
| 96 | + |
6 | 97 | return ( |
7 | 98 | <UserProfilePublicPage |
8 | 99 | user={user} |
9 | 100 | tasks={tasks} |
10 | | - listTasks={listTasks} |
11 | | - filterTasks={filterTasks} |
12 | 101 | searchUser={searchUser} |
| 102 | + serverSidePagination={serverSidePagination} |
| 103 | + onTabChange={handleTabChange} |
13 | 104 | /> |
14 | 105 | ) |
15 | 106 | } |
|
0 commit comments