All files / src/utils programStore.ts

0% Statements 0/26
0% Branches 0/6
0% Functions 0/13
0% Lines 0/20

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                                                                                                                                                                           
import {create} from "zustand";
 
// the type of a reduced program
export type ReducedProgram = { phases: Record<string, unknown>[] };
 
/**
 * the type definition of the programStore
 */
export type ProgramState = {
  // Basic store functionality:
  currentProgram: ReducedProgram;
  setProgramState: (state: ReducedProgram) => void;
  getProgramState: () => ReducedProgram;
 
  // Utility functions:
  // to avoid having to manually go through the entire state for every instance where data is required
  getPhaseIds: () => string[];
  getPhaseNames: () => string[];
  getNormsInPhase: (currentPhaseId: string) => Record<string, unknown>[];
  getGoalsInPhase: (currentPhaseId: string) => Record<string, unknown>[];
  getTriggersInPhase: (currentPhaseId: string) => Record<string, unknown>[];
  // if more specific utility functions are needed they can be added here:
}
 
/**
 * the ProgramStore can be used to access all information of the most recently sent program,
 * it contains basic functions to set and get the current program.
 * And it contains some utility functions that allow you to easily gain access
 * to the norms, triggers and goals of a specific phase.
 */
const useProgramStore = create<ProgramState>((set, get) => ({
  currentProgram: { phases: [] as Record<string, unknown>[]},
  /**
   * sets the current program by cloning the provided program using a structuredClone
   */
  setProgramState: (program: ReducedProgram) => set({currentProgram: structuredClone(program)}),
  /**
   * gets the current program
   */
  getProgramState: () => get().currentProgram,
 
  // utility functions:
  /**
   * gets the ids of all phases in the program
   */
  getPhaseIds: () => get().currentProgram.phases.map(entry => entry["id"] as string),
   /**
   * gets the names of all phases in the program
   */
  getPhaseNames: () => get().currentProgram.phases.map((entry) => (entry["name"] as string)),
  /**
   * gets the norms for the provided phase
   */
  getNormsInPhase: (currentPhaseId) => {
    const program = get().currentProgram;
    const phase = program.phases.find(val => val["id"] === currentPhaseId);
    if (phase) {
      return phase["norms"] as Record<string, unknown>[];
    }
    throw new Error(`phase with id:"${currentPhaseId}" not found`)
  },
  /**
   * gets the goals for the provided phase
   */
  getGoalsInPhase: (currentPhaseId) => {
    const program = get().currentProgram;
    const phase = program.phases.find(val => val["id"] === currentPhaseId);
    if (phase) {
      return phase["goals"] as Record<string, unknown>[];
    }
    throw new Error(`phase with id:"${currentPhaseId}" not found`)
  },
  /**
   * gets the triggers for the provided phase
   */
  getTriggersInPhase: (currentPhaseId) => {
    const program = get().currentProgram;
    const phase = program.phases.find(val => val["id"] === currentPhaseId);
    if (phase) {
      return phase["triggers"] as Record<string, unknown>[];
    }
    throw new Error(`phase with id:"${currentPhaseId}" not found`)
  }
}));
 
export default useProgramStore;