Skip to content

Commit

Permalink
PI-10295 ENGAGE: Product Slider Widget sort order doesn't work
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbridge committed Mar 23, 2021
1 parent ed5ba0c commit 3d8eac1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
15 changes: 14 additions & 1 deletion themes/theme-gmd/widgets/selectors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createSelector } from 'reselect';
import { SORT_PRICE_ASC, SORT_PRICE_DESC } from '@shopgate/engage/core';
import { generateResultHash } from '@shopgate/pwa-common/helpers/redux';
import { transformDisplayOptions } from '@shopgate/pwa-common/helpers/data';
import { getSortOrder } from '@shopgate/pwa-common/selectors/history';
Expand Down Expand Up @@ -122,8 +123,20 @@ export const getProductsResultWithHash = createSelector(
export const getProductsResultsWithIds = (state, type, params, id) => {
const currentSort = getSortOrder(state);
const { value: productIds = [], sort = currentSort } = params || {};
const products = productIds.map(productId => getProduct(state, { productId })).filter(Boolean);
let products = productIds.map(productId => getProduct(state, { productId })).filter(Boolean);
const hash = getResultHash(state, type, params, id);

const transformedSort = transformDisplayOptions(sort);

// Sort by price since fetch by id does not support sorting
if (transformedSort === SORT_PRICE_ASC) {
products = products.sort((p1, p2) => p1.price.unitPrice - p2.price.unitPrice);
}

if (transformedSort === SORT_PRICE_DESC) {
products = products.sort((p1, p2) => p2.price.unitPrice - p1.price.unitPrice);
}

return {
products,
totalProductCount: products.length,
Expand Down
14 changes: 13 additions & 1 deletion themes/theme-ios11/widgets/selectors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createSelector } from 'reselect';
import { SORT_PRICE_ASC, SORT_PRICE_DESC } from '@shopgate/engage/core';
import { generateResultHash } from '@shopgate/pwa-common/helpers/redux';
import { transformDisplayOptions } from '@shopgate/pwa-common/helpers/data';
import { getSortOrder } from '@shopgate/pwa-common/selectors/history';
Expand Down Expand Up @@ -122,8 +123,19 @@ export const getProductsResultWithHash = createSelector(
export const getProductsResultsWithIds = (state, type, params, id) => {
const currentSort = getSortOrder(state);
const { value: productIds = [], sort = currentSort } = params || {};
const products = productIds.map(productId => getProduct(state, { productId })).filter(Boolean);
let products = productIds.map(productId => getProduct(state, { productId })).filter(Boolean);
const hash = getResultHash(state, type, params, id);

const transformedSort = transformDisplayOptions(sort);

// Sort by price since fetch by id does not support sorting
if (transformedSort === SORT_PRICE_ASC) {
products = products.sort((p1, p2) => p1.price.unitPrice - p2.price.unitPrice);
}

if (transformedSort === SORT_PRICE_DESC) {
products = products.sort((p1, p2) => p2.price.unitPrice - p1.price.unitPrice);
}
return {
products,
totalProductCount: products.length,
Expand Down

0 comments on commit 3d8eac1

Please sign in to comment.