Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(store): django pagination and query selectors
Signed-off-by: Boaz Shuster <boaz.shuster.github@gmail.com>
- Loading branch information
Showing
5 changed files
with
96 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { RootState } from 'store/rootReducer'; | ||
import { PageResults } from './types'; | ||
|
||
export const selectPagination = <T extends PageResults>( | ||
stateProjector: (state: RootState) => T | ||
) => { | ||
return (state: RootState) => { | ||
return getPagination(stateProjector(state)); | ||
}; | ||
}; | ||
|
||
export const getPagination = <T extends PageResults>(payload: T) => { | ||
if (payload === null) { | ||
return { | ||
page: 1, | ||
perPage: 1, | ||
count: 0, | ||
}; | ||
} | ||
|
||
let urlParams = null; | ||
if (payload.links.next !== null) { | ||
urlParams = new URLSearchParams(payload.links.next.split('?')[1]); | ||
const limit = Number(urlParams.get('limit')); | ||
const offset = Number(urlParams.get('offset')) - limit; | ||
return { | ||
page: offset / limit + 1, | ||
perPage: limit, | ||
count: payload.meta.count, | ||
}; | ||
} | ||
|
||
if (payload.links.previous !== null) { | ||
urlParams = new URLSearchParams(payload.links.previous.split('?')[1]); | ||
const limit = Number(urlParams.get('limit')); | ||
const offset = Number(urlParams.get('offset')) + limit; | ||
return { | ||
page: offset / limit + 1, | ||
perPage: limit, | ||
count: payload.meta.count, | ||
}; | ||
} | ||
|
||
urlParams = new URLSearchParams(payload.links.first.split('?')[1]); | ||
return { | ||
page: 1, | ||
perPage: Number(urlParams.get('limit')), | ||
count: payload.meta.count, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { RootState } from 'store/rootReducer'; | ||
import { PageResults } from './types'; | ||
|
||
export const selectQuery = <T extends PageResults>( | ||
stateProjector: (state: RootState) => T, | ||
keys: string[] | ||
) => { | ||
return (state: RootState) => { | ||
const params = getQuery(stateProjector(state)); | ||
return keys.reduce((acc, curr) => { | ||
return { ...acc, [curr]: params.get(curr) }; | ||
}, {}); | ||
}; | ||
}; | ||
|
||
const getQuery = <T extends PageResults>(payload: T) => { | ||
if (payload === null) { | ||
return new URLSearchParams(); | ||
} | ||
const [, search] = payload.links.first.split('?'); | ||
return new URLSearchParams(search); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { PagedLinks, PagedMetaData } from 'api/api'; | ||
|
||
export interface PageResults { | ||
meta: PagedMetaData; | ||
links?: PagedLinks; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters