2019-06-06 21:17:33 +00:00
|
|
|
import { createSelector } from 'reselect';
|
|
|
|
import differenceInMilliseconds from 'date-fns/difference_in_milliseconds';
|
|
|
|
import orderBy from 'lodash/orderBy';
|
|
|
|
import { DIRECTION } from '../actions/sorting';
|
|
|
|
|
|
|
|
const getSketches = state => state.sketches;
|
|
|
|
const getField = state => state.sorting.field;
|
|
|
|
const getDirection = state => state.sorting.direction;
|
2019-11-10 20:39:22 +00:00
|
|
|
const getSearchTerm = state => state.search.sketchSearchTerm;
|
2019-06-06 21:17:33 +00:00
|
|
|
|
2019-08-21 18:08:08 +00:00
|
|
|
const getFilteredSketches = createSelector(
|
2019-06-06 21:17:33 +00:00
|
|
|
getSketches,
|
2019-08-21 18:08:08 +00:00
|
|
|
getSearchTerm,
|
|
|
|
(sketches, search) => {
|
|
|
|
if (search) {
|
|
|
|
const searchStrings = sketches.map((sketch) => {
|
|
|
|
const smallSketch = {
|
|
|
|
name: sketch.name
|
|
|
|
};
|
|
|
|
return { ...sketch, searchString: Object.values(smallSketch).join(' ').toLowerCase() };
|
|
|
|
});
|
|
|
|
return searchStrings.filter(sketch => sketch.searchString.includes(search.toLowerCase()));
|
|
|
|
}
|
|
|
|
return sketches;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const getSortedSketches = createSelector(
|
|
|
|
getFilteredSketches,
|
2019-06-06 21:17:33 +00:00
|
|
|
getField,
|
|
|
|
getDirection,
|
|
|
|
(sketches, field, direction) => {
|
|
|
|
if (field === 'name') {
|
|
|
|
if (direction === DIRECTION.DESC) {
|
|
|
|
return orderBy(sketches, 'name', 'desc');
|
|
|
|
}
|
|
|
|
return orderBy(sketches, 'name', 'asc');
|
|
|
|
}
|
|
|
|
const sortedSketches = [...sketches].sort((a, b) => {
|
|
|
|
const result =
|
|
|
|
direction === DIRECTION.ASC
|
|
|
|
? differenceInMilliseconds(new Date(a[field]), new Date(b[field]))
|
|
|
|
: differenceInMilliseconds(new Date(b[field]), new Date(a[field]));
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
return sortedSketches;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
export default getSortedSketches;
|