Skip to content

Commit 3377769

Browse files
committed
fix: sort from server side
1 parent c8503fc commit 3377769

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

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

Lines changed: 12 additions & 2 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'
@@ -27,6 +27,7 @@ import { Project } from '../../models/Project'
2727

2828
import { CopilotRequestModal } from './copilot-request-modal'
2929
import styles from './CopilotRequestsPage.module.scss'
30+
import { Sort } from '~/apps/admin/src/platform/gamification-admin/src/game-lib'
3031

3132
const CopilotTableActions: FC<{request: CopilotRequest}> = props => {
3233
const navigate: NavigateFunction = useNavigate()
@@ -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
@@ -234,6 +239,10 @@ const CopilotRequestsPage: FC = () => {
234239
setSize(size + 1)
235240
}
236241

242+
const onToggleSort = (s: Sort) => {
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: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { xhrGetAsync, xhrPatchAsync, xhrPostAsync } from '~/libs/core'
66
import { buildUrl } from '~/libs/shared/lib/utils/url'
77

88
import { CopilotRequest } from '../models/CopilotRequest'
9+
import { Sort } from '~/apps/admin/src/platform/gamification-admin/src/game-lib'
10+
import { getPaginatedAsync, PaginatedResponse } from '~/libs/core/lib/xhr/xhr-functions/xhr.functions'
911

1012
const baseUrl = `${EnvironmentConfig.API.V5}/projects`
1113
const PAGE_SIZE = 20
@@ -43,32 +45,35 @@ 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 = (url: string): Promise<PaginatedResponse<CopilotRequest[]>> => getPaginatedAsync<CopilotRequest[]>(url)
59+
.then((data: any) => {
60+
return {
61+
...data,
62+
data: data.data.map(copilotRequestFactory),
63+
}
64+
})
5765

5866
const {
5967
isValidating,
6068
data = [],
6169
size,
6270
setSize,
63-
}: SWRInfiniteResponse<CopilotRequest[]> = useSWRInfinite(getKey, fetcher, {
71+
}: SWRInfiniteResponse<PaginatedResponse<CopilotRequest[]>> = useSWRInfinite(getKey, fetcher, {
6472
revalidateOnFocus: false,
6573
})
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
74+
const latestPage = data[data.length - 1] || {}
75+
const copilotRequests = data.flatMap(page => page.data)
76+
const hasMoreCopilotRequests = latestPage.page + 1 < latestPage.totalPages
7277

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

0 commit comments

Comments
 (0)