Skip to content

Commit

Permalink
merge to Fix: sort did reset itself after filter update
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnito committed Jun 3, 2020
1 parent c615565 commit b2f10f0
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/components/SortBy/SortBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SortByPopup from './SortByPopup';
const SortBy = props => {
const { sort, showAsPopup, isConflictingFilterActive, intl, ...rest } = props;

const { relevanceKey } = config.custom.sortConfig;
const { relevanceKey, queryParamName } = config.custom.sortConfig;

const options = config.custom.sortConfig.options.map(option => {
const isRelevance = option.key === relevanceKey;
Expand All @@ -22,7 +22,7 @@ const SortBy = props => {
});
const defaultValue = 'createdAt';
const componentProps = {
urlParam: 'sort',
urlParam: queryParamName,
label: intl.formatMessage({ id: 'SortBy.heading' }),
options,
initialValue: isConflictingFilterActive ? relevanceKey : sort || defaultValue,
Expand Down
6 changes: 4 additions & 2 deletions src/containers/SearchPage/MainPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ const cleanSearchFromConflictingParams = (searchParams, sortConfig, filterConfig
searchParams,
filterConfig
);
return sortingFiltersActive ? { ...searchParams, sort: null } : searchParams;
return sortingFiltersActive
? { ...searchParams, [sortConfig.queryParamName]: null }
: searchParams;
};

/**
Expand Down Expand Up @@ -207,7 +209,7 @@ class MainPanel extends Component {
return sortConfig.active ? (
<SortBy
{...mobileClassesMaybe}
sort={urlQueryParams.sort}
sort={urlQueryParams[sortConfig.queryParamName]}
isConflictingFilterActive={!!conflictingFilterActive}
onSelect={this.handleSortBy}
showAsPopup
Expand Down
5 changes: 3 additions & 2 deletions src/containers/SearchPage/SearchPage.helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,12 @@ export const validURLParamsForExtendedData = (params, filters) => {

// extract search parameters, including a custom URL params
// which are validated by mapping the values to marketplace custom config.
export const pickSearchParamsOnly = (params, filters) => {
const { address, origin, bounds, sort, ...rest } = params || {};
export const pickSearchParamsOnly = (params, filters, sortConfig) => {
const { address, origin, bounds, ...rest } = params || {};
const boundsMaybe = bounds ? { bounds } : {};
const originMaybe = config.sortSearchByDistance && origin ? { origin } : {};
const filterParams = validFilterParams(rest, filters);
const sort = rest[sortConfig.queryParamName];
const sortMaybe = sort ? { sort } : {};

return {
Expand Down
7 changes: 5 additions & 2 deletions src/containers/SearchPage/SearchPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export class SearchPageComponent extends Component {
intl,
listings,
filterConfig,
sortConfig,
history,
location,
mapListings,
Expand All @@ -128,11 +129,11 @@ export class SearchPageComponent extends Component {

// urlQueryParams doesn't contain page specific url params
// like mapSearch, page or origin (origin depends on config.sortSearchByDistance)
const urlQueryParams = pickSearchParamsOnly(searchInURL, filterConfig);
const urlQueryParams = pickSearchParamsOnly(searchInURL, filterConfig, sortConfig);

// Page transition might initially use values from previous search
const urlQueryString = stringify(urlQueryParams);
const paramsQueryString = stringify(pickSearchParamsOnly(searchParams, filterConfig));
const paramsQueryString = stringify(pickSearchParamsOnly(searchParams, filterConfig, sortConfig));
const searchParamsAreInSync = urlQueryString === paramsQueryString;

const validQueryParams = validURLParamsForExtendedData(searchInURL, filterConfig);
Expand Down Expand Up @@ -230,6 +231,7 @@ SearchPageComponent.defaultProps = {
searchParams: {},
tab: 'listings',
filterConfig: config.custom.filters,
sortConfig: config.custom.sortConfig,
activeListingId: null,
};

Expand All @@ -246,6 +248,7 @@ SearchPageComponent.propTypes = {
searchParams: object,
tab: oneOf(['filters', 'listings', 'map']).isRequired,
filterConfig: propTypes.filterConfig,
sortConfig: propTypes.sortConfig,

// from withRouter
history: shape({
Expand Down
4 changes: 4 additions & 0 deletions src/marketplace-custom-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ export const sortConfig = {
// Enable/disable the sorting control in the SearchPage
active: true,

// Note: queryParamName 'sort' is fixed,
// you can't change it since Flex API expects it to be named as 'sort'
queryParamName: 'sort',

// Internal key for the relevance option, see notes below.
relevanceKey: 'relevance',

Expand Down
1 change: 1 addition & 0 deletions src/util/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ propTypes.filterConfig = arrayOf(

propTypes.sortConfig = shape({
active: bool,
queryParamName: oneOf(['sort']).isRequired,
relevanceKey: string.isRequired,
conflictingFilters: arrayOf(string),
options: arrayOf(
Expand Down

0 comments on commit b2f10f0

Please sign in to comment.