Skip to content

Commit 750aafe

Browse files
authored
Merge pull request #1198 from topcoder-platform/pm-1650_1
fix(PM-1650): sort from server side
2 parents c8503fc + b8e3be2 commit 750aafe

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

src/apps/copilots/src/pages/copilot-requests/CopilotRequestsPage.module.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
padding: $sp-2 0;
88
}
99

10+
.title {
11+
max-width: 200px;
12+
}
13+
1014
@media (max-width: 767px) {
1115
.title {
1216
min-width: 200px;

src/apps/copilots/src/pages/copilot-requests/index.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FC, useCallback, useContext, useMemo } from 'react'
1+
import { FC, useCallback, useContext, useMemo, useState } from 'react'
22
import { find } from 'lodash'
33
import { NavigateFunction, Params, useNavigate, useParams } from 'react-router-dom'
44
import classNames from 'classnames'
@@ -18,6 +18,7 @@ import {
1818
} from '~/libs/ui'
1919
import { profileContext, ProfileContextData, UserRole } from '~/libs/core'
2020
import { EnvironmentConfig } from '~/config'
21+
import { Sort } from '~/apps/admin/src/platform/gamification-admin/src/game-lib'
2122

2223
import { ProjectTypeLabels } from '../../constants'
2324
import { approveCopilotRequest, CopilotRequestsResponse, useCopilotRequests } from '../../services/copilot-requests'
@@ -136,6 +137,10 @@ const CopilotTableActions: FC<{request: CopilotRequest}> = props => {
136137
const CopilotRequestsPage: FC = () => {
137138
const navigate: NavigateFunction = useNavigate()
138139
const routeParams: Params<string> = useParams()
140+
const [sort, setSort] = useState<Sort>({
141+
direction: 'desc',
142+
fieldName: 'createdAt',
143+
})
139144

140145
const { profile }: ProfileContextData = useContext(profileContext)
141146
const isAdminOrPM: boolean = useMemo(
@@ -148,7 +153,7 @@ const CopilotRequestsPage: FC = () => {
148153
isValidating: requestsLoading,
149154
hasMoreCopilotRequests,
150155
setSize,
151-
size }: CopilotRequestsResponse = useCopilotRequests()
156+
size }: CopilotRequestsResponse = useCopilotRequests(sort)
152157

153158
const viewRequestDetails = useMemo(() => (
154159
routeParams.requestId && find(requests, { id: +routeParams.requestId }) as CopilotRequest
@@ -195,7 +200,7 @@ const CopilotRequestsPage: FC = () => {
195200
},
196201
{
197202
label: 'Type',
198-
propertyName: 'type',
203+
propertyName: 'projectType',
199204
type: 'text',
200205
},
201206
{
@@ -227,13 +232,17 @@ const CopilotRequestsPage: FC = () => {
227232
const tableData = useMemo(() => requests.map(request => ({
228233
...request,
229234
projectName: request.project?.name,
230-
type: ProjectTypeLabels[request.projectType] ?? '',
235+
projectType: ProjectTypeLabels[request.projectType] ?? '',
231236
})), [requests])
232237

233238
function loadMore(): void {
234239
setSize(size + 1)
235240
}
236241

242+
function onToggleSort(s: Sort): void {
243+
setSort(s)
244+
}
245+
237246
// header button config
238247
const addNewRequestButton: ButtonProps = {
239248
label: 'New Copilot Request',
@@ -260,6 +269,7 @@ const CopilotRequestsPage: FC = () => {
260269
data={tableData}
261270
moreToLoad={hasMoreCopilotRequests}
262271
onLoadMoreClick={loadMore}
272+
onToggleSort={onToggleSort}
263273
/>
264274
{requestsLoading && <LoadingCircles /> }
265275
{viewRequestDetails && (

src/apps/copilots/src/services/copilot-requests.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import useSWRInfinite, { SWRInfiniteResponse } from 'swr/infinite'
44
import { EnvironmentConfig } from '~/config'
55
import { xhrGetAsync, xhrPatchAsync, xhrPostAsync } from '~/libs/core'
66
import { buildUrl } from '~/libs/shared/lib/utils/url'
7+
import { Sort } from '~/apps/admin/src/platform/gamification-admin/src/game-lib'
8+
import { getPaginatedAsync, PaginatedResponse } from '~/libs/core/lib/xhr/xhr-functions/xhr.functions'
79

810
import { CopilotRequest } from '../models/CopilotRequest'
911

@@ -43,32 +45,37 @@ export type CopilotRequestsResponse = {
4345
* @param {string} [projectId] - Optional project ID to fetch copilot requests for a specific project.
4446
* @returns {CopilotRequestsResponse} - The response containing copilot requests.
4547
*/
46-
export const useCopilotRequests = (projectId?: string): CopilotRequestsResponse => {
48+
export const useCopilotRequests = (sort: Sort, projectId?: string): CopilotRequestsResponse => {
49+
4750
const getKey = (pageIndex: number, previousPageData: CopilotRequest[]): string | undefined => {
4851
if (previousPageData && previousPageData.length < PAGE_SIZE) return undefined
4952
const url = buildUrl(`${baseUrl}${projectId ? `/${projectId}` : ''}/copilots/requests`)
5053
return `
51-
${url}?page=${pageIndex + 1}&pageSize=${PAGE_SIZE}&sort=createdAt desc
54+
${url}?page=${pageIndex + 1}&pageSize=${PAGE_SIZE}&sort=${sort.fieldName} ${sort.direction}
5255
`
5356
}
5457

55-
const fetcher = (url: string): Promise<CopilotRequest[]> => xhrGetAsync<CopilotRequest[]>(url)
56-
.then((data: any) => data.map(copilotRequestFactory))
58+
const fetcher = (
59+
url: string,
60+
): Promise<PaginatedResponse<CopilotRequest[]>> => getPaginatedAsync<CopilotRequest[]>(url)
61+
.then((data: any) => (
62+
{
63+
...data,
64+
data: data.data.map(copilotRequestFactory),
65+
}
66+
))
5767

5868
const {
5969
isValidating,
6070
data = [],
6171
size,
6272
setSize,
63-
}: SWRInfiniteResponse<CopilotRequest[]> = useSWRInfinite(getKey, fetcher, {
73+
}: SWRInfiniteResponse<PaginatedResponse<CopilotRequest[]>> = useSWRInfinite(getKey, fetcher, {
6474
revalidateOnFocus: false,
6575
})
66-
67-
// Flatten data array
68-
const copilotRequests = data ? data.flat() : []
69-
70-
const lastPage = data[data.length - 1] || []
71-
const hasMoreCopilotRequests = lastPage.length === PAGE_SIZE
76+
const latestPage = data[data.length - 1] || {}
77+
const copilotRequests = data.flatMap(page => page.data)
78+
const hasMoreCopilotRequests = latestPage.page + 1 < latestPage.totalPages
7279

7380
return { data: copilotRequests, hasMoreCopilotRequests, isValidating, setSize: (s: number) => { setSize(s) }, size }
7481
}

0 commit comments

Comments
 (0)