All files / client/src/utils loadData.ts

97.82% Statements 45/46
90.9% Branches 20/22
100% Functions 8/8
97.82% Lines 45/46

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                            356x   358x     358x 358x       358x     358x       358x     358x     356x 366x 366x     356x 358x 358x     356x         429x 429x 429x 429x   429x 3x     426x     356x 358x 358x 358x   358x 279x 279x           356x 393x 393x 393x   393x 263x       356x 423x 423x 423x 423x   423x 7x         356x 8x 8x    
import { submitGetProgramItems } from "client/views/all-program-items/allProgramItemsThunks";
import {
  submitGetSettings,
  submitGetSignupMessages,
} from "client/views/admin/adminThunks";
import { submitGetUser } from "client/views/my-program-items/myProgramItemsThunks";
import { submitGetGroup } from "client/views/group/groupThunks";
import { submitSessionRecovery } from "client/views/login/loginThunks";
import { store } from "client/utils/store";
import { AppDispatch } from "client/types/reduxTypes";
import { submitGetTestSettings } from "client/test/test-settings/testSettingsThunks";
import { config } from "shared/config";
import { UserGroup } from "shared/types/models/user";
 
export const loadData = async (): Promise<void> => {
  // Get app settings
  await loadSettings();
 
  // Get test settings
  Eif (process.env.SETTINGS !== "production" && config.client().showTestValues) {
    await loadTestSettings();
  }
 
  // Check if existing user session
  await recoverSession();
 
  // Get user data
  await loadUser();
 
  // Get program items data
  // Must be loaded after user to be able to access state.login
  await loadProgramItems({ forceUpdate: false });
 
  // Get group members
  await loadGroupMembers();
};
 
export const loadSettings = async (): Promise<void> => {
  const dispatch: AppDispatch = store.dispatch;
  await dispatch(submitGetSettings());
};
 
const loadTestSettings = async (): Promise<void> => {
  const dispatch: AppDispatch = store.dispatch;
  await dispatch(submitGetTestSettings());
};
 
export const loadProgramItems = async ({
  forceUpdate,
}: {
  forceUpdate: boolean;
}): Promise<void> => {
  const state = store.getState();
  const dispatch: AppDispatch = store.dispatch;
  const { appOpen } = state.admin;
  const { loggedIn } = state.login;
 
  if (!appOpen && !loggedIn) {
    return;
  }
 
  await dispatch(submitGetProgramItems({ forceUpdate }));
};
 
const recoverSession = async (): Promise<void> => {
  const state = store.getState();
  const dispatch: AppDispatch = store.dispatch;
  const { loggedIn, jwt } = state.login;
 
  if (!loggedIn && jwt) {
    const error = await dispatch(submitSessionRecovery(jwt));
    Iif (error) {
      console.log("Error loading saved session, reset session..."); // eslint-disable-line no-console
    }
  }
};
 
export const loadUser = async (): Promise<void> => {
  const state = store.getState();
  const dispatch: AppDispatch = store.dispatch;
  const { loggedIn, userGroup, username } = state.login;
 
  if (loggedIn && userGroup === UserGroup.USER) {
    await dispatch(submitGetUser(username));
  }
};
 
export const loadGroupMembers = async (): Promise<void> => {
  const state = store.getState();
  const dispatch: AppDispatch = store.dispatch;
  const { loggedIn } = state.login;
  const { groupCode } = state.group;
 
  if (loggedIn && groupCode !== "0") {
    await dispatch(submitGetGroup(groupCode));
  }
};
 
// This includes public and private signup messages
export const loadSignupMessages = async (): Promise<void> => {
  const dispatch: AppDispatch = store.dispatch;
  await dispatch(submitGetSignupMessages());
};