|
| 1 | +/** |
| 2 | + * @module "reducers.looker" |
| 3 | + * @desc Reducer for {@link module:actions.looker} actions. |
| 4 | + * |
| 5 | + * State segment managed by this reducer has the following structure: |
| 6 | + * @param {Object} dataSet={}, index by lookerId. |
| 7 | + */ |
| 8 | +import _ from 'lodash'; |
| 9 | +import { handleActions } from 'redux-actions'; |
| 10 | +import actions from '../actions/looker'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Handles LOOKER/GET_LOOKER_DONE action. |
| 14 | + * @param {Object} state |
| 15 | + * @param {Object} action Payload will be JSON from api call |
| 16 | + * @return {Object} New state |
| 17 | + */ |
| 18 | +function onGetLookerDone(state, { payload }) { |
| 19 | + const { |
| 20 | + data: { |
| 21 | + res, |
| 22 | + error, |
| 23 | + }, |
| 24 | + lookerId, |
| 25 | + } = payload; |
| 26 | + |
| 27 | + const newDataSet = { |
| 28 | + ...state.dataSet, |
| 29 | + }; |
| 30 | + newDataSet[lookerId] = { |
| 31 | + lookerData: res, |
| 32 | + error, |
| 33 | + msg: res.message, |
| 34 | + }; |
| 35 | + return ({ |
| 36 | + ...state, |
| 37 | + dataSet: newDataSet, |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Creates a new Looker reducer with the specified initial state. |
| 43 | + * @param {Object} initialState Optional. Initial state. |
| 44 | + * @return {Function} Looker reducer. |
| 45 | + */ |
| 46 | +function create(initialState = {}) { |
| 47 | + const a = actions.looker; |
| 48 | + return handleActions({ |
| 49 | + [a.getLookerDone]: onGetLookerDone, |
| 50 | + }, _.defaults(initialState, { |
| 51 | + dataSet: {}, |
| 52 | + })); |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Factory which creates a new reducer. |
| 57 | + * @return {Promise} |
| 58 | + * @resolves {Function(state, action): state} New reducer. |
| 59 | + */ |
| 60 | +export function factory() { |
| 61 | + return Promise.resolve(create()); |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * @static |
| 66 | + * @member default |
| 67 | + * @desc Reducer with default initial state. |
| 68 | + */ |
| 69 | +export default create(); |
0 commit comments