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

64.28% Statements 18/28
66.66% Branches 8/12
55.55% Functions 5/9
64.28% Lines 18/28

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                                        15x     15x   15x   15x 15x   15x 1x     15x           15x 1x             1x                                           15x 1x     1x                 15x 1x     1x               15x                                                                                      
import { ObjectPropertySelector } from "@/ahuora-design-system/inputs/PropertySelection";
import DebouncedInput from "@/ahuora-design-system/ui/debounced-input";
import {
  api,
  OptimizationDegreesOfFreedomRead,
  ScenarioRead,
  useCoreOptimizationDegreesOfFreedomDestroyMutation,
  useCoreOptimizationDegreesOfFreedomPartialUpdateMutation,
  useUnitopsSimulationobjectsRetrieveQuery,
} from "@/api/apiStore.gen";
import { useAppDispatch } from "@/store/hooks";
import { useState } from "react";
 
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 handleSimulationObjectChange = (id: number) => {
    setSimulationObjectId(id);
  };
 
  const handleDelete = () => {
    deleteDegreeOfFreedom({
      id: degreeOfFreedom.id,
    });
  };
 
  const handlePropertyChange = (id: number) => {
    updateDegreeOfFreedom({
      id: degreeOfFreedom.id,
      patchedOptimizationDegreesOfFreedom: {
        propertyValue: id,
      },
    });
    // optimistic update
    dispatch(
      api.util.updateQueryData(
        "coreOptimizationList",
        { simulationObjectId: optimization.simulationObject },
        (prev) => {
          const prevOptimization = prev.find(
            (opt) => opt.id === optimization.id,
          );
          Iif (prevOptimization) {
            const prevDegreeOfFreedom = prevOptimization.degreesOfFreedom?.find(
              (dof) => dof.id === degreeOfFreedom.id,
            );
            Iif (prevDegreeOfFreedom) {
              prevDegreeOfFreedom.propertyValue = id;
            }
          }
        },
      ),
    );
  };
 
  // Handle changes for upper bound
  const handleUpperBoundChange = (updatedValue: number | "") => {
    Iif (updatedValue === "") {
      updatedValue = null;
    }
    updateDegreeOfFreedom({
      id: degreeOfFreedom.id,
      patchedOptimizationDegreesOfFreedom: {
        upper_bound: updatedValue,
      },
    });
  };
 
  // Handle changes for lower bound
  const handleLowerBoundChange = (updatedValue: number | "") => {
    Iif (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 ">
      <ObjectPropertySelector
        simulationObjectId={simulationObjectId}
        propertyId={propertyValueId}
        onSimulationObjectChange={handleSimulationObjectChange}
        onPropertyChange={handlePropertyChange}
        onDelete={handleDelete}
        useValueId={true}
      />
      <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>
  );
}