cache claim search results by query
This commit is contained in:
parent
49e57e8ba0
commit
b87e2f92f3
14 changed files with 189 additions and 107 deletions
src/util
71
src/util/query-params.js
Normal file
71
src/util/query-params.js
Normal file
|
@ -0,0 +1,71 @@
|
|||
// @flow
|
||||
import { SEARCH_OPTIONS } from 'constants/search';
|
||||
|
||||
const DEFAULT_SEARCH_RESULT_FROM = 0;
|
||||
const DEFAULT_SEARCH_SIZE = 20;
|
||||
|
||||
export function parseQueryParams(queryString: string) {
|
||||
if (queryString === '') return {};
|
||||
const parts = queryString
|
||||
.split('?')
|
||||
.pop()
|
||||
.split('&')
|
||||
.map(p => p.split('='));
|
||||
|
||||
const params = {};
|
||||
parts.forEach(array => {
|
||||
const [first, second] = array;
|
||||
params[first] = second;
|
||||
});
|
||||
return params;
|
||||
}
|
||||
|
||||
export function toQueryString(params: { [string]: string | number }) {
|
||||
if (!params) return '';
|
||||
|
||||
const parts = [];
|
||||
Object.keys(params).forEach(key => {
|
||||
if (Object.prototype.hasOwnProperty.call(params, key) && params[key]) {
|
||||
parts.push(`${key}=${params[key]}`);
|
||||
}
|
||||
});
|
||||
|
||||
return parts.join('&');
|
||||
}
|
||||
|
||||
export const getSearchQueryString = (
|
||||
query: string,
|
||||
options: any = {},
|
||||
includeUserOptions: boolean = false
|
||||
) => {
|
||||
const encodedQuery = encodeURIComponent(query);
|
||||
const queryParams = [
|
||||
`s=${encodedQuery}`,
|
||||
`size=${options.size || DEFAULT_SEARCH_SIZE}`,
|
||||
`from=${options.from || DEFAULT_SEARCH_RESULT_FROM}`,
|
||||
];
|
||||
|
||||
if (includeUserOptions) {
|
||||
const claimType = options[SEARCH_OPTIONS.CLAIM_TYPE];
|
||||
queryParams.push(`claimType=${claimType}`);
|
||||
|
||||
// If they are only searching for channels, strip out the media info
|
||||
if (!claimType.includes(SEARCH_OPTIONS.INCLUDE_CHANNELS)) {
|
||||
queryParams.push(
|
||||
`mediaType=${[
|
||||
SEARCH_OPTIONS.MEDIA_FILE,
|
||||
SEARCH_OPTIONS.MEDIA_AUDIO,
|
||||
SEARCH_OPTIONS.MEDIA_VIDEO,
|
||||
SEARCH_OPTIONS.MEDIA_TEXT,
|
||||
SEARCH_OPTIONS.MEDIA_IMAGE,
|
||||
SEARCH_OPTIONS.MEDIA_APPLICATION,
|
||||
].reduce(
|
||||
(acc, currentOption) => (options[currentOption] ? `${acc}${currentOption},` : acc),
|
||||
''
|
||||
)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return queryParams.join('&');
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue