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

90% Statements 27/30
88.23% Branches 15/17
88.88% Functions 8/9
90% Lines 27/30

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                                          38x     38x   38x   38x 38x 38x 38x   38x 3x     38x           38x     3x         3x 3x   3x 3x 4x   3x 3x             3x                 38x 1x     1x                 38x 1x     1x               38x                                                                                                                
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";
import { Badge } from "@/ahuora-design-system/ui/badge";
 
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 | "") => {
    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 ">
      <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}
      />
      <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>
  );
}