|
| 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/language'; |
| 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 onGetLanguageDone(state, { payload, error }) { |
| 21 | + console.log("payload", payload); |
| 22 | + if (error) { |
| 23 | + return { ...state }; |
| 24 | + } |
| 25 | + console.log("Language", payload.result.content[0]); |
| 26 | + return ({ |
| 27 | + ...state, language: payload.result.content[0] |
| 28 | + }); |
| 29 | +} |
| 30 | + |
| 31 | +// function onUpdateLanguageDone(state, { payload, error }) { |
| 32 | +// const newState = { ...state, updatingLanguage: false }; |
| 33 | + |
| 34 | +// if (error) { |
| 35 | +// logger.error('Failed to update user language', payload); |
| 36 | +// fireErrorMessage('ERROR: Failed to update user language!'); |
| 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 | +// language: { |
| 47 | +// ...newState.language, |
| 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 | + const a = actions.language; |
| 60 | + return handleActions({ |
| 61 | + [a.getLanguageInit]: state => state, |
| 62 | + [a.getLanguageDone]: onGetLanguageDone, |
| 63 | + // [a.updateLanguageInit]: state => ({ ...state, updatingBasicInfo: true }), |
| 64 | + // [a.updateLanguageDone]: onUpdateBasicInfoDone, |
| 65 | + }, _.defaults(initialState, {language: null})); |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Factory which creates a new reducer with its initial state tailored to the |
| 70 | + * given options object, if specified (for server-side rendering). If options |
| 71 | + * object is not specified, it creates just the default reducer. Accepted options are: |
| 72 | + * @returns {Promise} |
| 73 | + * @resolves {Function(state, action): state} New reducer. |
| 74 | + */ |
| 75 | +export function factory() { |
| 76 | + return Promise.resolve(create()); |
| 77 | +} |
| 78 | + |
| 79 | +/* Reducer with the default initial state. */ |
| 80 | +export default create(); |
0 commit comments