All files / src/store index.js

97.43% Statements 38/39
83.33% Branches 10/12
90.9% Functions 10/11
97.29% Lines 36/37

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 108 109 110 111 112 113 114 115 116                                                                  1x   1x 7x   6x   6x 6x 6x   6x   6x   6x 3x 3x     6x 2x 2x     6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x   6x     1x                                             3x     1x     3x         1x 2x 1x 1x 2x        
import slideoutStore from '@common-vue/atomic-ui/src/store_modules/slideout/store';
import createLogger from '../utils/logger';
import { setStoreForGuards } from '../guards/authGuard';
import {
  markAuthReady,
  setStoreForAuthorizationGuard,
} from '../guards/authorizationGuard';
import { setStoreForEnvironmentsGuard } from '../guards/environmentsGuard';
import audit from './modules/audit.module';
import auth from './modules/auth.module';
import domains from './modules/domains.module';
import emailTemplates from './modules/emailTemplates.module';
import meGroups from './modules/meGroups.module';
import environments from './modules/environments.module';
import experiments from './modules/experiments.module';
import notifications from './modules/notifications.module';
import splits from './modules/splits.module';
import targets from './modules/targets.module';
import templates from './modules/templates.module';
import {
  AUTH_FQ,
  DOMAINS_FQ,
  EMAIL_TEMPLATES_FQ,
  ENV_FQ,
  EXP_FQ,
  ME_GROUPS_FQ,
  SPLITS_FQ,
  TARGETS_FQ,
  TEMPLATES_FQ,
} from './types';
import BUILD_ENV_FQ from './types/env.types';
import NS from './types/modules.types';
 
const log = createLogger('store');
 
const ssrHydratePlugin = (store) => {
  if (typeof window === 'undefined') return;
 
  log.info('SSR Hydrate Plugin: Starting...');
 
  setStoreForGuards(store);
  setStoreForAuthorizationGuard(store);
  setStoreForEnvironmentsGuard(store);
 
  const initial = window.__INITIAL_STATE__ || {};
 
  log.info('Initial State:', initial);
 
  if (initial.pageData) {
    log.info('Hydrating pageData:', initial.pageData);
    store.commit('HYDRATE_PAGE_DATA', initial.pageData);
  }
 
  if (initial.envProps) {
    log.info('Hydrating envProps:', initial.envProps);
    store.commit('HYDRATE_ENV_PROPS', initial.envProps);
  }
 
  log.info('Dispatching module hydrations...');
  store.dispatch(AUTH_FQ.actions.hydrate, initial);
  markAuthReady();
  store.dispatch(ENV_FQ.actions.hydrate, initial);
  store.dispatch(EXP_FQ.actions.hydrate, initial);
  store.dispatch(DOMAINS_FQ.actions.hydrate, initial);
  store.dispatch(ME_GROUPS_FQ.actions.hydrate, initial);
  store.dispatch(SPLITS_FQ.actions.hydrate, initial);
  store.dispatch(TARGETS_FQ.actions.hydrate, initial);
  store.dispatch(TEMPLATES_FQ.actions.hydrate, initial);
  store.dispatch(EMAIL_TEMPLATES_FQ.actions.hydrate, initial);
 
  log.info('SSR Hydrate Plugin: Complete!');
};
 
const plugins = [ssrHydratePlugin];
 
export default {
  strict: process.env.NODE_ENV !== 'production',
  modules: {
    [NS.notifications]: notifications,
    [NS.audit]: audit,
    [NS.auth]: auth,
    [NS.environments]: environments,
    [NS.experiments]: experiments,
    [NS.domains]: domains,
    [NS.meGroups]: meGroups,
    [NS.splits]: splits,
    [NS.targets]: targets,
    [NS.templates]: templates,
    [NS.emailTemplates]: emailTemplates,
    slideoutStore,
  },
  state: () => ({
    envProps: {},
  }),
  mutations: {
    HYDRATE_PAGE_DATA(state, payload = {}) {
      state.pageData = { ...state.pageData, ...payload };
    },
    RESET_PAGE_DATA(state) {
      state.pageData = {};
    },
    HYDRATE_ENV_PROPS(state, payload = {}) {
      state.envProps = { ...state.envProps, ...payload };
    },
  },
  actions: {},
  getters: {
    pageData: (s) => s.pageData,
    [BUILD_ENV_FQ.getters.env]: (s) => s.envProps || {},
    [BUILD_ENV_FQ.getters.entryPoint]: (_s, g) => g[BUILD_ENV_FQ.getters.env].entryPoint,
    [BUILD_ENV_FQ.getters.assetHost]: (_s, g) => g[BUILD_ENV_FQ.getters.env].assetHost,
    [BUILD_ENV_FQ.getters.envKey]: (_s, g) => (key) => g[BUILD_ENV_FQ.getters.env]?.[key],
  },
  plugins,
};