|
| 1 | +/** |
| 2 | + * @module "reducers.profile" |
| 3 | + * @desc Reducer for Profile API data |
| 4 | + * @todo Document reducer state structure. |
| 5 | + */ |
| 6 | +import _ from 'lodash'; |
| 7 | +import { handleActions } from 'redux-actions'; |
| 8 | +import actions from '../actions/education'; |
| 9 | +import logger from '../utils/logger'; |
| 10 | +import { fireErrorMessage } from '../utils/errors'; |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +/** |
| 15 | + * Handles PROFILE/GET_EXTERNAL_ACCOUNTS_DONE action. |
| 16 | + * @param {Object} state |
| 17 | + * @param {Object} action Payload will be JSON from api call |
| 18 | + * @return {Object} New state |
| 19 | + */ |
| 20 | +function onGetEducationDone(state, { payload, error }) { |
| 21 | + console.log("payload", payload); |
| 22 | + if (error) { |
| 23 | + return { ...state }; |
| 24 | + } |
| 25 | + console.log("Education", payload.result.content[0]); |
| 26 | + return ({ |
| 27 | + ...state, education: payload.result.content[0] |
| 28 | + }); |
| 29 | +} |
| 30 | + |
| 31 | +function onUpdateEducationDone(state, { payload, error }) { |
| 32 | + const newState = { ...state, updatingEducation: false }; |
| 33 | + |
| 34 | + if (error) { |
| 35 | + logger.error('Failed to update user education', payload); |
| 36 | + fireErrorMessage('ERROR: Failed to update user education!'); |
| 37 | + return newState; |
| 38 | + } |
| 39 | +// console.log("{Payload Update basic info/ basic info reducers: ", payload); |
| 40 | + // if (!newState.info || newState.info.handle !== payload.handle) { |
| 41 | + // return newState; |
| 42 | + // } |
| 43 | +// console.log("{New State Update basic info/ basic info reducers: ", newState); |
| 44 | + return { |
| 45 | + ...newState, |
| 46 | + education: { |
| 47 | + ...newState.education, |
| 48 | + ...payload, |
| 49 | + }, |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Creates a new Profile reducer with the specified initial state. |
| 55 | + * @param {Object} initialState Optional. Initial state. |
| 56 | + * @return {Function} Profile reducer. |
| 57 | + */ |
| 58 | +function create(initialState) { |
| 59 | + console.log("Entered create of edu reducer"); |
| 60 | + const a = actions.education; |
| 61 | + return handleActions({ |
| 62 | + [a.getEducationInit]: state => state, |
| 63 | + [a.getEducationDone]: onGetEducationDone, |
| 64 | + [a.updateEducationInit]: state => ({ ...state, updatingEducation: true }), |
| 65 | + [a.updateEducationDone]: onUpdateEducationDone, |
| 66 | + }, _.defaults(initialState, {education: null})); |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Factory which creates a new reducer with its initial state tailored to the |
| 71 | + * given options object, if specified (for server-side rendering). If options |
| 72 | + * object is not specified, it creates just the default reducer. Accepted options are: |
| 73 | + * @returns {Promise} |
| 74 | + * @resolves {Function(state, action): state} New reducer. |
| 75 | + */ |
| 76 | +export function factory() { |
| 77 | + return Promise.resolve(create()); |
| 78 | +} |
| 79 | + |
| 80 | +/* Reducer with the default initial state. */ |
| 81 | +export default create(); |
0 commit comments