Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
feat(viewpatients): enables to navigation to previous page
Browse files Browse the repository at this point in the history
Add a field in PageRequest to store refrence that will enable getting previous page.

feat #1969
  • Loading branch information
akshay-ap committed May 3, 2020
1 parent 220ad9c commit 52a59d3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
8 changes: 7 additions & 1 deletion src/__tests__/patients/list/ViewPatients.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,13 @@ describe('Patients', () => {
{
sorts: [{ field: 'index', direction: 'asc' }],
},
{ number: 1, size: 1, nextPageInfo: { index: null }, direction: 'next' },
{
number: 1,
size: 1,
nextPageInfo: { index: null },
direction: 'next',
previousPageInfo: { index: null },
},
)
})
})
Expand Down
3 changes: 3 additions & 0 deletions src/clients/db/PageRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ export default interface PageRequest {
number: number | undefined
size: number | undefined
nextPageInfo: { [key: string]: string | null } | undefined
previousPageInfo: { [key: string]: string | null } | undefined

direction: 'previous' | 'next' | null
}
export const UnpagedRequest: PageRequest = {
number: undefined,
size: undefined,
nextPageInfo: undefined,
direction: null,
previousPageInfo: undefined,
}
17 changes: 10 additions & 7 deletions src/clients/db/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export default class Repository<T extends AbstractDBModel> {
s.direction = s.direction === 'asc' ? 'desc' : 'asc'
selector[s.field] = {
$lte:
pageRequest.nextPageInfo && pageRequest.nextPageInfo[s.field]
? pageRequest.nextPageInfo[s.field]
pageRequest.previousPageInfo && pageRequest.previousPageInfo[s.field]
? pageRequest.previousPageInfo[s.field]
: null,
}
})
Expand All @@ -72,33 +72,36 @@ export default class Repository<T extends AbstractDBModel> {
selector,
sort: sort.sorts.length > 0 ? sort.sorts.map((s) => ({ [s.field]: s.direction })) : undefined,
limit: pageRequest.size ? pageRequest.size + 1 : undefined,
skip: pageRequest.direction === 'previous' ? pageRequest.size : undefined,
})
const mappedResult = result.docs.map(mapDocument)

if (pageRequest.direction === 'previous') {
mappedResult.reverse()
}
const nextPageInfo: { [key: string]: string } = {}

const nextPageInfo: { [key: string]: string } = {}
if (mappedResult.length > 0) {
sort.sorts.forEach((s) => {
nextPageInfo[s.field] = mappedResult[mappedResult.length - 1][s.field]
})
}

const previousPageInfo: { [key: string]: string } = {}
sort.sorts.forEach((s) => {
previousPageInfo[s.field] = mappedResult[0][s.field]
})

const pagedResult: Page<T> = {
content:
pageRequest.size !== undefined && mappedResult.length === pageRequest.size + 1
? mappedResult.slice(0, mappedResult.length - 1)
: mappedResult,
hasNext: pageRequest.size !== undefined && mappedResult.length === pageRequest.size + 1,
// hasPrevious: pageRequest.number !== undefined && pageRequest.number > 1,
hasPrevious: false,
hasPrevious: pageRequest.number !== undefined && pageRequest.number > 1,
pageRequest: {
size: pageRequest.size,
number: pageRequest.number,
nextPageInfo,
previousPageInfo,
direction: null,
},
}
Expand Down
5 changes: 4 additions & 1 deletion src/patients/list/ViewPatients.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const ViewPatients = () => {
size: 1,
number: 1,
nextPageInfo: { index: null },
previousPageInfo: { index: null },
direction: 'next',
})

Expand All @@ -39,6 +40,7 @@ const ViewPatients = () => {
number: p.number + 1,
size: p.size,
nextPageInfo: patients.pageRequest?.nextPageInfo,
previousPageInfo: undefined,
direction: 'next',
}
return newPageRequest
Expand All @@ -53,7 +55,8 @@ const ViewPatients = () => {
return {
number: p.number - 1,
size: p.size,
nextPageInfo: patients.pageRequest?.nextPageInfo,
nextPageInfo: undefined,
previousPageInfo: patients.pageRequest?.previousPageInfo,
direction: 'previous',
}
}
Expand Down

0 comments on commit 52a59d3

Please sign in to comment.