Skip to content

Commit

Permalink
fix: add sort by date on recently viewed v1
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiago Neves authored and talbertosilva committed Jan 4, 2024
1 parent 7a59906 commit 0bc8aa2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ export const expectedLocalPayload = [
},
{
productId: 33333333,
lastVisitDate: '2020-02-03T11:08:50.010Z',
lastVisitDate: '2020-02-02T11:08:50.010Z',
},
];
19 changes: 18 additions & 1 deletion packages/core/src/recentlyViewed/redux/__tests__/reducer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
expectedRemotePayload,
} from '../__mocks__/getRecentlyViewed';
import reducer, { actionTypes } from '..';
import sortBy from 'lodash/sortBy';

let initialState;

Expand Down Expand Up @@ -137,6 +138,20 @@ describe('Recently Viewed reducer', () => {
expect(state.result).toEqual(initialState.result);
});

it('should return the products ordered by date', () => {
const state = reducer(undefined, {
type: actionTypes.GET_RECENTLY_VIEWED_PRODUCTS_SUCCESS,
payload: expectedRemotePayload,
});

expect(state.result?.computed).toEqual(
sortBy(
expectedRemotePayload.entries,
entry => new Date(entry.lastVisitDate),
),
);
});

it(`should handle ${actionTypes.GET_RECENTLY_VIEWED_PRODUCTS_SUCCESS} action type`, () => {
const state = reducer(undefined, {
type: actionTypes.GET_RECENTLY_VIEWED_PRODUCTS_SUCCESS,
Expand All @@ -152,7 +167,9 @@ describe('Recently Viewed reducer', () => {
payload: expectedLocalPayload,
});

expect(state.result.computed).toEqual(expectedLocalPayload);
expect(state.result.computed).toEqual(
sortBy(expectedLocalPayload, entry => new Date(entry.lastVisitDate)),
);
});

it(`should handle ${actionTypes.DELETE_RECENTLY_VIEWED_PRODUCT_SUCCESS} action type`, () => {
Expand Down
17 changes: 15 additions & 2 deletions packages/core/src/recentlyViewed/redux/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import * as actionTypes from './actionTypes';
import { combineReducers } from 'redux';
import omit from 'lodash/omit';
import sortBy from 'lodash/sortBy';
import uniqBy from 'lodash/uniqBy';

export const INITIAL_STATE = {
Expand Down Expand Up @@ -61,7 +62,13 @@ const result = (
return {
remote: action.payload,
pagination: omit(action.payload, 'entries'),
computed: uniqBy([...action.payload.entries, ...computed], 'productId'),
computed: uniqBy(
sortBy(
[...action.payload.entries, ...computed],
entry => new Date(entry.lastVisitDate),
),
'productId',
),
};
}
case actionTypes.SAVE_RECENTLY_VIEWED_PRODUCT: {
Expand All @@ -70,7 +77,13 @@ const result = (
// Merge the new payload before the previously computed entries and filter the repeated ones
return {
...state,
computed: uniqBy([...action.payload, ...computed], 'productId'),
computed: uniqBy(
sortBy(
[...action.payload, ...computed],
entry => new Date(entry.lastVisitDate),
),
'productId',
),
};
}
case actionTypes.DELETE_RECENTLY_VIEWED_PRODUCT_SUCCESS: {
Expand Down

0 comments on commit 0bc8aa2

Please sign in to comment.