Skip to content

Instantly share code, notes, and snippets.

@thenormalsquid
Created December 8, 2016 18:45
Show Gist options
  • Save thenormalsquid/c3d3adf65b9b55857b257e62289cbd44 to your computer and use it in GitHub Desktop.
Save thenormalsquid/c3d3adf65b9b55857b257e62289cbd44 to your computer and use it in GitHub Desktop.
// @flow
import { createReducer, createHandlers, compose } from '_util/redux-utils';
import { createSelector } from 'reselect';
import { Set, Map } from 'immutable';
import {
ASYNC_GET_FULFILLMENTS,
SET_SELECTED_FULFILLMENT
} from '_constants';
const initialState = {
byId: Map(),
selectedId: null
};
export type State = {
byId: Immutable.Map,
selectedId: ?number
};
//TODO: fix flowtypes
const actionHandlers = {
...createHandlers(ASYNC_GET_FULFILLMENTS, [
() => ({}),
(state: State, { payload }): { byId: Immutable.Map } => ({
byId: state.byId.mergeDeep(payload.entities.fulfillments)
}),
() => ({})
]),
[SET_SELECTED_FULFILLMENT]: (state, { payload }) => ({
selectedId: payload
})
};
const byId = state => state.byId;
export const getFulfillmentById = createSelector(
[byId, (state, id) => id],
(fulfillments, id) => fulfillments.get(String(id)) || Map()
);
export const getGroupedFulfillments = createSelector(
[byId],
fulfillments => fulfillments.groupBy(item => item.toJS().method).map(
group => group.sort(
(item1, item2) =>
item1.statusUrgency - item2.statusUrgency // -n if item1 before item2 n for vice versa
).toList()
)
);
export default createReducer(initialState, actionHandlers);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment