All files / src/pages/flowsheet-page/flowsheet/LeftSideBar/Scenarios DegreesOfFreedom.tsx

80% Statements 48/60
87.23% Branches 41/47
57.14% Functions 4/7
93.47% Lines 43/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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157                            36x             36x     36x   36x   36x 36x 36x 36x   36x 4x 36x   36x       44x   36x   4x         4x 4x     4x 5x     4x             4x           56x     36x       2x           44x     36x       2x           44x       11x 15x                   81x 47x 12x             78x 84x     36x     36x                 48x   36x     36x                 48x 52x 93x      
import { useState } from "react";
import { ObjectPropertySelector } from "@/ahuora-design-system/inputs/PropertySelection";
import { Badge } from "@/ahuora-design-system/ui/badge";
import DebouncedInput from "@/ahuora-design-system/ui/debounced-input";
import {
  api,
  OptimizationDegreesOfFreedomRead,
  ScenarioRead,
  useCoreOptimizationDegreesOfFreedomDestroyMutation,
  useCoreOptimizationDegreesOfFreedomPartialUpdateMutation,
} from "@/api/apiStore.gen";
import { useAppDispatch } from "@/store/hooks";
import { isFormulaBackedValue } from "../../PropertiesSidebar/PropertyPanel/propertyState";
 
export function DegreeOfFreedom({
  degreeOfFreedom,
  optimization,
}: {
  degreeOfFreedom: OptimizationDegreesOfFreedomRead;
  optimization: ScenarioRead;
}) {
  const [simulationObjectId, setSimulationObjectId] = useState<
    number | undefined
  >(degreeOfFreedom.simulationObjectId);
  const propertyValueId = degreeOfFreedom.propertyValue;
  const [updateDegreeOfFreedom] =
    useCoreOptimizationDegreesOfFreedomPartialUpdateMutation();
  const [deleteDegreeOfFreedom] =
    useCoreOptimizationDegreesOfFreedomDestroyMutation();
  const dispatch = useAppDispatch();
  const isDof = degreeOfFreedom.isFreeVariable;
  const dofType = isDof ? "DOF" : "BOUND";
 
  const handleSimulationObjectChange = (id: number) => {
    setSimulationObjectId(id);
  };
 
  const handleDelete = () => {
    deleteDegreeOfFreedom({
      id: degreeOfFreedom.id,
    });
  };
 
  const handlePropertyChange = (id: number) => {
    // optimistic update
    dispatch(
      api.util.updateQueryData(
        "coreScenarioList",
        { simulationObjectId: optimization.simulationObject },
        (prev) => {
          const prevOptimization = prev.find(
            (opt) => opt.id === optimization.id,
          );
          if (prevOptimization) {
            const prevDegreeOfFreedom = prevOptimization.degreesOfFreedom?.find(
              (dof) => dof.id === degreeOfFreedom.id,
            );
            if (prevDegreeOfFreedom) {
              prevDegreeOfFreedom.propertyValue = id;
            }
          }
        },
      ),
    );
    //Update in db
    updateDegreeOfFreedom({
      id: degreeOfFreedom.id,
      patchedOptimizationDegreesOfFreedom: {
        propertyValue: id,
      },
    });
  };
 
  // Handle changes for upper bound
  const handleUpperBoundChange = (updatedValue: number | "") => {
    if (updatedValue === "") {
      updatedValue = null;
    }
    updateDegreeOfFreedom({
      id: degreeOfFreedom.id,
      patchedOptimizationDegreesOfFreedom: {
        upper_bound: updatedValue,
      },
    });
  };
 
  // Handle changes for lower bound
  const handleLowerBoundChange = (updatedValue: number | "") => {
    if (updatedValue === "") {
      updatedValue = null;
    }
    updateDegreeOfFreedom({
      id: degreeOfFreedom.id,
      patchedOptimizationDegreesOfFreedom: {
        lower_bound: updatedValue,
      },
    });
  };
 
  return (
    <div className="flex flex-col gap-2 border bg-muted rounded-md p-2 ">
      <div className="flex flex-row justify-between items-center">
        {simulationObjectId !== undefined && propertyValueId !== null && (
          <Badge
            variant="default"
            borderRadius="round"
            size="xs"
            className="text-muted-foreground font-light"
            aria-label={`optimisation-${dofType}`}
          >
            {dofType}
          </Badge>
        )}
      </div>
      <ObjectPropertySelector
        simulationObjectId={simulationObjectId}
        propertyId={propertyValueId}
        onSimulationObjectChange={handleSimulationObjectChange}
        onPropertyChange={handlePropertyChange}
        onDelete={handleDelete}
        useValueId={true}
        filter={({ propertyValue }) => !isFormulaBackedValue(propertyValue)}
      />
      <div className="flex flex-row gap-4 ">
        <div className="flex-1">
          <p className="text-sm font-light">Lower bound: </p>
          <DebouncedInput
            value={
              degreeOfFreedom.lower_bound === undefined ||
              degreeOfFreedom.lower_bound === null
                ? ""
                : degreeOfFreedom.lower_bound
            }
            type="number"
            onUpdate={handleLowerBoundChange}
            aria-label="lower bound"
          />
        </div>
        <div className="flex-1 ">
          <p className="text-sm font-light">Upper bound: </p>
          <DebouncedInput
            value={
              degreeOfFreedom.upper_bound === undefined ||
              degreeOfFreedom.upper_bound === null
                ? ""
                : degreeOfFreedom.upper_bound
            }
            type="number"
            onUpdate={handleUpperBoundChange}
            aria-label="upper bound"
          />
        </div>
      </div>
    </div>
  );
}