All files / src/store/modules environments.module.js

100% Statements 32/32
85.71% Branches 12/14
100% Functions 9/9
100% Lines 32/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107            2x           23x             1x       1x           4x 1x     3x 3x     2x 1x     1x         4x 4x 1x       1x     3x   3x 3x 3x         1x       3x       3x   3x 3x 3x   1x             2x   2x     4x 1x     3x               3x        
import { STORAGE_KEYS } from '../../composables/useLocalStorage';
import createLogger from '../../utils/logger';
import {
  ENVIRONMENTS, ENV_ACT, ENV_GET, ENV_MUT,
} from '../types';
 
const log = createLogger('environments');
 
export default {
  namespaced: true,
 
  state() {
    return {
      selected: null,
    };
  },
 
  mutations: {
    [ENV_MUT.setSelected](state, environment) {
      state.selected = environment;
    },
 
    [ENV_MUT.clear](state) {
      state.selected = null;
    },
  },
 
  actions: {
    [ENV_ACT.hydrate]({ commit }) {
      if (typeof window === 'undefined') {
        return;
      }
 
      try {
        const stored = window.localStorage.getItem(
          STORAGE_KEYS.SELECTED_ENVIRONMENT,
        );
        if (stored) {
          commit(ENV_MUT.setSelected, stored);
        }
      } catch (error) {
        log.warn('Error hydrating from localStorage:', error);
      }
    },
 
    [ENV_ACT.select]({ commit }, environment) {
      const validEnvironments = Object.values(ENVIRONMENTS);
      if (!validEnvironments.includes(environment)) {
        log.warn(
          `Invalid environment: ${environment}. Valid options:`,
          validEnvironments,
        );
        return false;
      }
 
      commit(ENV_MUT.setSelected, environment);
 
      Eif (typeof window !== 'undefined') {
        try {
          window.localStorage.setItem(
            STORAGE_KEYS.SELECTED_ENVIRONMENT,
            environment,
          );
        } catch (error) {
          log.warn('Error saving to localStorage:', error);
        }
      }
 
      return true;
    },
 
    [ENV_ACT.clear]({ commit }) {
      commit(ENV_MUT.clear);
 
      Eif (typeof window !== 'undefined') {
        try {
          window.localStorage.removeItem(STORAGE_KEYS.SELECTED_ENVIRONMENT);
        } catch (error) {
          log.warn('Error clearing localStorage:', error);
        }
      }
    },
  },
 
  getters: {
    [ENV_GET.selected]: (state) => state.selected,
 
    [ENV_GET.isSelected]: (state) => !!state.selected,
 
    [ENV_GET.displayName]: (state) => {
      if (!state.selected) {
        return 'No Environment Selected';
      }
 
      const displayNames = {
        [ENVIRONMENTS.LOCAL]: 'Local',
        [ENVIRONMENTS.QA_MCOM]: "Macy's QA",
        [ENVIRONMENTS.QA_BCOM]: "Bloomingdale's QA",
        [ENVIRONMENTS.PROD_MCOM]: "Macy's Production",
        [ENVIRONMENTS.PROD_BCOM]: "Bloomingdale's Production",
      };
 
      return displayNames[state.selected] || state.selected;
    },
  },
};