Skip to content

Commit 41c745b

Browse files
committed
migrating use profile task list calls to use server side pagination
1 parent 66a05e6 commit 41c745b

3 files changed

Lines changed: 102 additions & 49 deletions

File tree

frontend/src/components/areas/public/features/profile/pages/profile-page.tsx

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,106 @@
11
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'
33
import { useParams } from 'react-router-dom'
44

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+
697
return (
798
<UserProfilePublicPage
899
user={user}
9100
tasks={tasks}
10-
listTasks={listTasks}
11-
filterTasks={filterTasks}
12101
searchUser={searchUser}
102+
serverSidePagination={serverSidePagination}
103+
onTabChange={handleTabChange}
13104
/>
14105
)
15106
}

frontend/src/components/design-library/pages/public-pages/user-profile-public-page/user-profile-public-page.tsx

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect } from 'react'
1+
import React from 'react'
22
import { Container } from '@mui/material'
33
import { Grid } from '@mui/material'
44
import ProfileUserHeader from 'design-library/molecules/headers/profile-user-header/profile-user-header'
@@ -10,46 +10,11 @@ import {
1010
customColumnRenderer
1111
} from 'design-library/molecules/tables/issue-table/issue-table'
1212
import { FormattedMessage } from 'react-intl'
13-
import { useParams } from 'react-router-dom'
1413

15-
const UserProfilePublicPage = ({ user, searchUser, tasks, listTasks, filterTasks }) => {
16-
const { userId } = useParams<{ userId: string }>()
14+
const UserProfilePublicPage = ({ user, tasks, searchUser, serverSidePagination, onTabChange }) => {
1715
const { data: profile } = user || {}
1816
const issueMetadata = useIssueMetadata({ includeProject: true })
1917

20-
const listTasksByUserId = async () => {
21-
await listTasks({ userId: userId })
22-
await filterTasks('all')
23-
}
24-
25-
const filterTasksWithOrders = async () => {
26-
await listTasks({})
27-
await filterTasks('supported')
28-
}
29-
30-
const filterTasksByState = async (value) => {
31-
switch (value) {
32-
case 'created':
33-
await listTasksByUserId()
34-
break
35-
case 'supported':
36-
await filterTasksWithOrders()
37-
break
38-
default:
39-
await listTasksByUserId()
40-
break
41-
}
42-
}
43-
44-
const handleTabbedTableChange = (newValue: string) => {
45-
filterTasksByState(newValue)
46-
}
47-
48-
useEffect(() => {
49-
searchUser({ id: userId })
50-
listTasksByUserId()
51-
}, [userId])
52-
5318
return (
5419
<React.Fragment>
5520
<Page>
@@ -60,8 +25,9 @@ const UserProfilePublicPage = ({ user, searchUser, tasks, listTasks, filterTasks
6025
<Root container>
6126
<Grid size={{ xs: 12, md: 12 }}>
6227
<TabbedTable
63-
onChange={handleTabbedTableChange}
28+
onChange={onTabChange}
6429
activeTab={'created'}
30+
serverSidePagination={serverSidePagination}
6531
tabs={[
6632
{
6733
value: 'created',

frontend/src/containers/tasks/task-list-user.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
import { connect } from 'react-redux'
2-
import { listTasks, filterTasks } from '../../actions/taskActions'
2+
import { listTasks } from '../../actions/taskActions'
33
import { searchUser } from '../../actions/userActions'
4-
import { getFilteredTasks } from '../../selectors/tasks'
54
import ProfilePage from '../../components/areas/public/features/profile/pages/profile-page'
65

76
const mapStateToProps = (state: any) => {
87
return {
98
user: state.user,
10-
tasks: getFilteredTasks(state)
9+
tasks: state.tasks
1110
}
1211
}
1312

1413
const mapDispatchToProps = (dispatch: any) => {
1514
return {
1615
searchUser: (params: any) => dispatch(searchUser(params)),
17-
listTasks: ({ organizationId, projectId, userId, status, labelIds, languageIds }: any) =>
18-
dispatch(listTasks({ organizationId, projectId, userId, status, labelIds, languageIds })),
19-
filterTasks: (key: any, value: any, additional: any) =>
20-
dispatch(filterTasks(key, value, additional))
16+
listTasks: (params: any) => dispatch(listTasks(params))
2117
}
2218
}
2319

0 commit comments

Comments
 (0)