department-of-veterans-affairs/vets-website

View on GitHub
src/applications/mhv-secure-messaging/reducers/recipients.js

Summary

Maintainability
A
1 hr
Test Coverage
import { Actions } from '../util/actionTypes';
import { findBlockedFacilities, formatRecipient } from '../util/helpers';

const initialState = {
  /**
   * List of ALL triage teams recipients
   * @type {array}
   */
  allRecipients: [],
  allowedRecipients: [],
  blockedRecipients: [],
  blockedFacilities: [],
  allFacilities: [],
  associatedTriageGroupsQty: undefined,
  associatedBlockedTriageGroupsQty: undefined,
  noAssociations: undefined,
  allTriageGroupsBlocked: undefined,
  error: undefined,
};

export const recipientsReducer = (state = initialState, action) => {
  switch (action.type) {
    case Actions.AllRecipients.GET_LIST: {
      const {
        associatedTriageGroups,
        associatedBlockedTriageGroups,
      } = action.response.meta;
      const noAssociations = associatedTriageGroups === 0;
      const allTriageGroupsBlocked =
        !noAssociations &&
        associatedTriageGroups === associatedBlockedTriageGroups;

      const facilities = findBlockedFacilities(action.response.data);

      return {
        ...state,
        associatedTriageGroupsQty: associatedTriageGroups,

        associatedBlockedTriageGroupsQty: associatedBlockedTriageGroups,

        allRecipients: action.response.data.map(recipient =>
          formatRecipient(recipient),
        ),

        allowedRecipients: action.response.data
          .filter(
            recipient =>
              recipient.attributes.blockedStatus === false &&
              recipient.attributes.preferredTeam === true,
          )
          .map(recipient => formatRecipient(recipient)),

        blockedRecipients: action.response.data
          .filter(recipient => recipient.attributes.blockedStatus === true)
          .map(recipient => formatRecipient(recipient)),

        blockedFacilities: facilities.fullyBlockedFacilities,

        allFacilities: facilities.allFacilities,

        noAssociations,

        allTriageGroupsBlocked,
      };
    }
    case Actions.AllRecipients.GET_LIST_ERROR:
      return {
        ...state,
        error: true,
      };
    default:
      return state;
  }
};