Skip to content

Commit

Permalink
global: update query state from response
Browse files Browse the repository at this point in the history
  • Loading branch information
zzacharo committed Oct 15, 2020
1 parent 2b774db commit 284c707
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 60 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"expect": "^26.5.3",
"json": "^10.0.0",
"lodash": "^4.17.20",
"node-sass": "^4.12.0",
"node-sass": "^4.14.0",
"qs": "^6.8.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
Expand Down
34 changes: 17 additions & 17 deletions src/lib/api/UrlHandlerApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import _isNaN from 'lodash/isNaN';
import _isNil from 'lodash/isNil';
import _cloneDeep from 'lodash/cloneDeep';

const pushHistory = query => {
const pushHistory = (query) => {
if (window.history.pushState) {
window.history.pushState({ path: query }, '', query);
}
};

const replaceHistory = query => {
const replaceHistory = (query) => {
if (window.history.replaceState) {
window.history.replaceState({ path: query }, '', query);
}
Expand All @@ -32,7 +32,7 @@ class UrlParser {
this.parse = this.parse.bind(this);
}

_sanitizeParamValue = value => {
_sanitizeParamValue = (value) => {
let parsedValue = parseInt(value);
if (_isNaN(parsedValue)) {
try {
Expand All @@ -58,7 +58,7 @@ class UrlParser {
parse(queryString = '') {
const parsedParams = Qs.parse(queryString, { ignoreQueryPrefix: true });
const params = {};
Object.entries(parsedParams).forEach(entry => {
Object.entries(parsedParams).forEach((entry) => {
const key = entry[0];
const value = entry[1];
params[key] = this._sanitizeParamValue(value);
Expand Down Expand Up @@ -115,7 +115,7 @@ export class UrlHandlerApi {

// build the serializer from URL params to Query state by flipping the urlParamsMapping
this.fromUrlParamsMapping = {};
Object.keys(this.urlParamsMapping).forEach(stateKey => {
Object.keys(this.urlParamsMapping).forEach((stateKey) => {
this.fromUrlParamsMapping[this.urlParamsMapping[stateKey]] = stateKey;
});

Expand All @@ -128,7 +128,7 @@ export class UrlHandlerApi {
* Map filters from list to string that is human readable
* [ 'type', 'photo', [ 'subtype', 'png' ]] => type:photo+subtype:png
*/
_filterListToString = filter => {
_filterListToString = (filter) => {
const childFilter =
filter.length === 3
? this.urlFilterSeparator.concat(this._filterListToString(filter[2]))
Expand All @@ -139,27 +139,27 @@ export class UrlHandlerApi {
/**
* Map each query state field to an URL param
*/
_mapQueryStateToUrlParams = queryState => {
_mapQueryStateToUrlParams = (queryState) => {
const params = {};
Object.keys(queryState)
.filter(stateKey => stateKey in this.urlParamsMapping)
.filter(stateKey => {
.filter((stateKey) => stateKey in this.urlParamsMapping)
.filter((stateKey) => {
// filter out negative or null values
if (
(stateKey === 'page' || stateKey === 'size') &&
queryState[stateKey] <= 0
) {
return false;
}
if (stateKey === 'hiddenParams'){
if (stateKey === 'hiddenParams') {
return false;
}
return queryState[stateKey] !== null;
})
.forEach(stateKey => {
.forEach((stateKey) => {
const paramKey = this.urlParamsMapping[stateKey];
if (stateKey === 'filters') {
params[paramKey] = queryState[stateKey].map(filter =>
params[paramKey] = queryState[stateKey].map((filter) =>
this._filterListToString(filter)
);
} else {
Expand All @@ -179,7 +179,7 @@ export class UrlHandlerApi {
* Map filters from string to list
* type:photo+subtype:png => [ 'type', 'photo', [ 'subtype', 'png' ]]
*/
_filterStringToList = filterStr => {
_filterStringToList = (filterStr) => {
const childSepPos = filterStr.indexOf(this.urlFilterSeparator);
const hasChild = childSepPos > -1;

Expand All @@ -203,9 +203,9 @@ export class UrlHandlerApi {
/**
* Map each URL param to a query state field
*/
_mapUrlParamsToQueryState = urlParamsObj => {
_mapUrlParamsToQueryState = (urlParamsObj) => {
const result = {};
Object.keys(urlParamsObj).forEach(paramKey => {
Object.keys(urlParamsObj).forEach((paramKey) => {
if (this.urlParamValidator.isValid(paramKey, urlParamsObj[paramKey])) {
const queryStateKey = this.fromUrlParamsMapping[paramKey];
result[queryStateKey] = urlParamsObj[paramKey];
Expand All @@ -215,7 +215,7 @@ export class UrlHandlerApi {
// if only 1 filter, create an array with one element
urlParamsObj[paramKey] = [urlParamsObj[paramKey]];
}
result[queryStateKey] = urlParamsObj[paramKey].map(filter =>
result[queryStateKey] = urlParamsObj[paramKey].map((filter) =>
this._filterStringToList(filter)
);
}
Expand All @@ -226,7 +226,7 @@ export class UrlHandlerApi {

_mergeParamsIntoState = (urlStateObj, queryState) => {
const _queryState = _cloneDeep(queryState);
Object.keys(urlStateObj).forEach(stateKey => {
Object.keys(urlStateObj).forEach((stateKey) => {
if (stateKey in _queryState) {
_queryState[stateKey] = urlStateObj[stateKey];
}
Expand Down
7 changes: 4 additions & 3 deletions src/lib/api/contrib/elasticsearch/ESResponseSerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ export class ESResponseSerializer {
* @param {object} payload the backend response payload
*/
serialize(payload) {
const { aggregations, hits } = payload;
return {
aggregations: payload.aggregations || {},
hits: payload.hits.hits.map(hit => hit._source),
total: payload.hits.total.value,
aggregations: aggregations || {},
hits: hits.hits.map((hit) => hit._source),
total: hits.total.value,
};
}
}
8 changes: 5 additions & 3 deletions src/lib/api/contrib/invenio/InvenioResponseSerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ export class InvenioResponseSerializer {
* @param {object} payload the backend response payload
*/
serialize(payload) {
const { aggregations, hits, ...extras } = payload;
return {
aggregations: payload.aggregations || {},
hits: payload.hits.hits,
total: payload.hits.total,
aggregations: aggregations || {},
hits: hits.hits,
total: hits.total,
extras: extras,
};
}
}
17 changes: 15 additions & 2 deletions src/lib/api/contrib/invenio/InvenioSearchApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

import _get from 'lodash/get';
import _hasIn from 'lodash/hasIn';
import _isEmpty from 'lodash/isEmpty';
import axios from 'axios';
import { InvenioRequestSerializer } from './InvenioRequestSerializer';
import { InvenioResponseSerializer } from './InvenioResponseSerializer';
import { updateQueryState } from '../../../state/selectors';
import { STORE_KEYS } from '../../../storeConfig';

export class InvenioSearchApi {
constructor(config) {
Expand Down Expand Up @@ -78,9 +81,19 @@ export class InvenioSearchApi {
* @param {string} stateQuery the `query` state with the user input
*/
async search(stateQuery) {
const response = await this.http.request({
let response = await this.http.request({
params: stateQuery,
});
return this.responseSerializer.serialize(response.data);
response = this.responseSerializer.serialize(response.data);
const newQueryState = updateQueryState(
stateQuery,
response.extras,
STORE_KEYS
);
if (!_isEmpty(newQueryState)) {
response.newQueryState = newQueryState;
}
delete response.extras;
return response;
}
}
7 changes: 5 additions & 2 deletions src/lib/components/Sort/Sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Sort extends Component {
}

_computeValue = (sortBy, sortOrder) => {
return `${sortBy}-${sortOrder}`;
return sortOrder ? `${sortBy}-${sortOrder}` : sortBy;
};

onChange = (value) => {
Expand All @@ -47,12 +47,13 @@ class Sort extends Component {
totalResults,
label,
overridableId,
sortOrderDisabled,
} = this.props;
return (
<ShouldRender
condition={
currentSortBy !== null &&
currentSortOrder !== null &&
(sortOrderDisabled || currentSortBy !== null) &&
!loading &&
totalResults > 0
}
Expand Down Expand Up @@ -81,13 +82,15 @@ Sort.propTypes = {
updateQuerySorting: PropTypes.func.isRequired,
label: PropTypes.func,
overridableId: PropTypes.string,
sortOrderDisabled: PropTypes.bool,
};

Sort.defaultProps = {
currentSortBy: null,
currentSortOrder: null,
label: (cmp) => cmp,
overridableId: '',
sortOrderDisabled: false,
};

const Element = ({ overridableId, ...props }) => {
Expand Down

0 comments on commit 284c707

Please sign in to comment.