p5.js-web-editor/client/modules/IDE/selectors/projects.js
siddhant b7df80a96c Add sorting to sketches #789 (#910)
* reselect added

* Added Reselect Sorting

* Refactor App

* added svgs

* Refactor

* Fixed Issues

* re: #789, update sorting styling, create sorting actions and reducers, add sort by sketch name

* re #789, change names of svg icons

* re: #789, use orderBy instead of sortBy, fix styling jumps
2019-06-06 17:17:33 -04:00

32 lines
1 KiB
JavaScript

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;
const getSortedSketches = createSelector(
getSketches,
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;