department-of-veterans-affairs/vets-website

View on GitHub
src/platform/user/profile/reducers/index.js

Summary

Maintainability
A
1 hr
Test Coverage
import set from '../../../utilities/data/set';

import { UPDATE_LOGGEDIN_STATUS } from '../../authentication/actions';
import { mapRawUserDataToState } from '../utilities';

import {
  UPDATE_PROFILE_FIELDS,
  PROFILE_LOADING_FINISHED,
  FETCHING_MHV_ACCOUNT,
  FETCH_MHV_ACCOUNT_FAILURE,
  FETCH_MHV_ACCOUNT_SUCCESS,
  REMOVING_SAVED_FORM_SUCCESS,
  PROFILE_ERROR,
} from '../actions';

const initialState = {
  userFullName: {
    first: null,
    middle: null,
    last: null,
    suffix: null,
  },
  email: null,
  dob: null,
  gender: null,
  accountType: null,
  accountUuid: null,
  isCernerPatient: false,
  loa: {
    current: null,
    highest: null,
  },
  verified: false,
  mhvAccount: {
    accountLevel: null,
    accountState: null,
    errors: null,
    loading: false,
    termsAndConditionsAccepted: false,
  },
  vapContactInfo: {},
  savedForms: [],
  prefillsAvailable: [],
  loading: true,
  services: [],
  session: {},
  mhvTransitionEligible: false,
  errors: false,
};

const updateMhvAccountState = (state, mhvAccount) =>
  set(
    'mhvAccount',
    {
      ...state.mhvAccount,
      ...mhvAccount,
      errors: null,
      loading: false,
    },
    state,
  );

function profileInformation(state = initialState, action) {
  switch (action.type) {
    case UPDATE_PROFILE_FIELDS: {
      const newState = mapRawUserDataToState(action.payload);
      return { ...state, ...newState };
    }

    case PROFILE_LOADING_FINISHED:
    case UPDATE_LOGGEDIN_STATUS:
      return set('loading', false, state);

    case FETCHING_MHV_ACCOUNT:
      return set('mhvAccount.loading', true, state);

    case FETCH_MHV_ACCOUNT_FAILURE:
      return set(
        'mhvAccount',
        {
          ...state.mhvAccount,
          errors: action.errors,
          loading: false,
        },
        state,
      );

    // We are no longer creating or upgrading MHV accounts
    case FETCH_MHV_ACCOUNT_SUCCESS:
      return updateMhvAccountState(state, action.data.attributes);

    case REMOVING_SAVED_FORM_SUCCESS: {
      const forms = state.savedForms.filter(el => el.form !== action.formId);
      return set('savedForms', forms, state);
    }

    case PROFILE_ERROR:
      return set('errors', true, state);

    default:
      return state;
  }
}

export default profileInformation;