|
| 1 | +/** |
| 2 | + * Reducer for TC Academy |
| 3 | + */ |
| 4 | + |
| 5 | +import { handleActions } from 'redux-actions'; |
| 6 | +import actions from 'actions/tc-academy'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Handles TCACADEMY/GET_TCA_CERTIFICATIONS_INIT action. |
| 10 | + * @param {Object} state |
| 11 | + * @return {Object} New state |
| 12 | + */ |
| 13 | +function onGetTcaCertificationsInit(state) { |
| 14 | + return { |
| 15 | + ...state, |
| 16 | + certifications: [], |
| 17 | + failed: false, |
| 18 | + loading: true, |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Handles TCACADEMY/GET_TCA_CERTIFICATIONS_DONE actions. |
| 24 | + * @param {Object} state Previous state. |
| 25 | + * @param {Object} action Action. |
| 26 | + */ |
| 27 | +function onGetTcaCertificationsDone(state, action) { |
| 28 | + return { |
| 29 | + ...state, |
| 30 | + certifications: [], |
| 31 | + ...(action.error ? {} : action.payload), |
| 32 | + failed: action.error, |
| 33 | + loading: false, |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Creates a new challenges reducer with the specified initial state. |
| 39 | + * @param {Object} initialState Optional. Initial state. |
| 40 | + * @return challenges reducer. |
| 41 | + */ |
| 42 | +function create(initialState) { |
| 43 | + const a = actions.tcAcademy; |
| 44 | + |
| 45 | + return handleActions({ |
| 46 | + [a.getTcaCertificationsInit]: onGetTcaCertificationsInit, |
| 47 | + [a.getTcaCertificationsDone]: onGetTcaCertificationsDone, |
| 48 | + }, initialState || {}); |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Factory which creates a new reducer with its initial state tailored to the |
| 53 | + * given options object, if specified (for server-side rendering). If options |
| 54 | + * object is not specified, it creates just the default reducer. Accepted options are: |
| 55 | + * @return {Promise} |
| 56 | + * @resolves {Function(state, action): state} New reducer. |
| 57 | + */ |
| 58 | +export function factory() { |
| 59 | + return Promise.resolve(create()); |
| 60 | +} |
| 61 | + |
| 62 | +/* Default reducer with empty initial state. */ |
| 63 | +export default create(); |
0 commit comments