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                                      608x 687x   687x 687x     686x   686x     686x   680x   680x     608x 697x 697x     608x 687x 687x     608x         807x 807x 807x 807x   807x 3x     804x     608x 686x 686x 686x   686x 463x 463x     6x     680x     608x 725x 725x 725x   725x 452x   273x     608x 770x 770x 770x 770x   770x 11x 11x   759x       608x 10x 10x    
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> => {
  let success = await loadSettings();
 
  Eif (process.env.SETTINGS !== "production" && config.client().showTestValues) {
    success = (await loadTestSettings()) && success;
  }
 
  success = (await recoverSession()) && success;
 
  success = (await loadUser()) && success;
 
  // After the user, since loading program items reads the login state
  success = (await loadProgramItems({ forceUpdate: false })) && success;
 
  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());
};