All files / src/pages/flowsheet-page/economics/page/model economicsPageState.ts

85% Statements 17/20
63.63% Branches 7/11
100% Functions 1/1
84.61% Lines 11/13

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                13x   10x 10x   6x       3x 3x         7x 5x 5x 3x       3x      
import type { EconomicsStudyRead } from "@/api/apiStore.gen";
 
export type EconomicsLoadState = "loading" | "error" | "empty" | "ready";
 
export function selectedEconomicsStudyId(
  studies: EconomicsStudyRead[],
  studyParam?: string,
) {
  const requestedId = Number(studyParam);
  if (Number.isFinite(requestedId)) {
    const requestedStudy = studies.find((study) => study.id === requestedId);
    if (requestedStudy) return requestedStudy.id;
  }
  return studies[0]?.id;
}
 
export function selectedEconomicsUnitId(unitParam?: string) {
  const unitId = Number(unitParam);
  if (!Number.isInteger(unitId) || unitId <= 0) return undefined;
  return unitId;
}
 
export function selectorPlaceholder(state: EconomicsLoadState) {
  if (state === "loading") return "Loading studies";
  Iif (state === "error") return "Unavailable";
  if (state === "empty") return "No studies";
  return "Select study";
}
 
export function queryErrorStatus(error: unknown) {
  if (!error || typeof error !== "object" || !("status" in error)) return;
  return String(error.status);
}