All files / client/src/utils loadData.ts

100% Statements 51/51
96.87% Branches 31/32
100% Functions 8/8
100% Lines 51/51

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 117                                      520x   598x     597x 597x       597x     597x       597x     590x   590x     520x 607x 607x     520x 597x 597x     520x         713x 713x 713x 713x   713x 3x     710x     520x 597x 597x 597x   597x 381x 381x     6x     591x     520x 637x 637x 637x   637x 383x   254x     520x 682x 682x 682x 682x   682x 10x 10x   672x       520x 9x 9x    
import { config } from "shared/config";
import { UserGroup } from "shared/types/models/user";
import { submitGetTestSettings } from "client/test/test-settings/testSettingsThunks";
import { AppDispatch } from "client/types/reduxTypes";
import { store } from "client/utils/store";
import {
  submitGetSettings,
  submitGetSignupMessages,
} from "client/views/admin/adminThunks";
import { submitGetProgramItems } from "client/views/all-program-items/allProgramItemsThunks";
import { submitGetGroup } from "client/views/group/groupThunks";
import { submitSessionRecovery } from "client/views/login/loginThunks";
import { submitGetUser } from "client/views/my-program-items/myProgramItemsThunks";
 
// Returns whether every load that actually ran succeeded, so the caller can
// tell a load that delivered fresh data from one whose requests failed.
// Requests made here must be registered in the error-display policy's
// background-request list, or their failures around a device wake toast
// immediately instead of being suppressed
export const loadData = async (): Promise<boolean> => {
  // Get app settings
  let success = await loadSettings();
 
  // Get test settings
  Eif (process.env.SETTINGS !== "production" && config.client().showTestValues) {
    success = (await loadTestSettings()) && success;
  }
 
  // Check if existing user session
  success = (await recoverSession()) && success;
 
  // Get user data
  success = (await loadUser()) && success;
 
  // Get program items data
  // Must be loaded after user to be able to access state.login
  success = (await loadProgramItems({ forceUpdate: false })) && success;
 
  // Get group members
  success = (await loadGroupMembers()) && success;
 
  return success;
};
 
export const loadSettings = async (): Promise<boolean> => {
  const dispatch: AppDispatch = store.dispatch;
  return await dispatch(submitGetSettings());
};
 
const loadTestSettings = async (): Promise<boolean> => {
  const dispatch: AppDispatch = store.dispatch;
  return await dispatch(submitGetTestSettings());
};
 
export const loadProgramItems = async ({
  forceUpdate,
}: {
  forceUpdate: boolean;
}): Promise<boolean> => {
  const state = store.getState();
  const dispatch: AppDispatch = store.dispatch;
  const { appOpen } = state.admin;
  const { loggedIn } = state.login;
 
  if (!appOpen && !loggedIn) {
    return true;
  }
 
  return await dispatch(submitGetProgramItems({ forceUpdate }));
};
 
const recoverSession = async (): Promise<boolean> => {
  const state = store.getState();
  const dispatch: AppDispatch = store.dispatch;
  const { loggedIn, jwt } = state.login;
 
  if (!loggedIn && jwt) {
    const error = await dispatch(submitSessionRecovery(jwt));
    if (error) {
      // A rejected session raises its own app-level error, so there is nothing
      // to report here beyond the load having failed
      return false;
    }
  }
  return true;
};
 
export const loadUser = async (): Promise<boolean> => {
  const state = store.getState();
  const dispatch: AppDispatch = store.dispatch;
  const { loggedIn, userGroup, username } = state.login;
 
  if (loggedIn && userGroup === UserGroup.USER) {
    return await dispatch(submitGetUser(username));
  }
  return true;
};
 
export const loadGroupMembers = async (): Promise<boolean> => {
  const state = store.getState();
  const dispatch: AppDispatch = store.dispatch;
  const { loggedIn } = state.login;
  const { groupCode } = state.group;
 
  if (loggedIn && groupCode !== "0") {
    const error = await dispatch(submitGetGroup(groupCode));
    return error === undefined;
  }
  return true;
};
 
// This includes public and private sign-up messages
export const loadSignupMessages = async (): Promise<void> => {
  const dispatch: AppDispatch = store.dispatch;
  await dispatch(submitGetSignupMessages());
};