All files / src/pages/flowsheet-page/flowsheet/PropertiesSidebar/LogicBlocks TurbineSettings.tsx

100% Statements 17/17
50% Branches 1/2
100% Functions 3/3
100% Lines 16/16

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                              49x                   23x   23x   23x 23x     23x   138x           23x 1x 1x           1x 1x 1x 1x 1x       23x                    
import React, { useState } from "react";
import { useSearchParams } from "react-router-dom";
import SelectInput from "@/ahuora-design-system/inputs/SelectInput";
import { useCoreSchemapropertysetUpdateTurbineTypeCreateMutation } from "@/api/apiStore.gen";
import {
  bs_turbine,
  cs_turbine,
  dts_turbine,
  pl_turbine,
  turbine,
  willans_turbine,
} from "@/data/objects.json";
import { useCurrentObject } from "@/hooks/flowsheetObjects";
import { ContentTypes } from "../../LeftSideBar/LeftSideBarTabDefinitions";
 
const objMap = {
  turbine: turbine.displayType,
  willans_turbine: willans_turbine.displayType,
  pl_turbine: pl_turbine.displayType,
  dts_turbine: dts_turbine.displayType,
  bs_turbine: bs_turbine.displayType,
  cs_turbine: cs_turbine.displayType,
};
 
export function TurbineSettings() {
  const currentSimulationObject = useCurrentObject();
  const [changeTurbineType] =
    useCoreSchemapropertysetUpdateTurbineTypeCreateMutation();
  //Declaring array and veriables for selecting turbine:
  const configNameArray = Object.entries(objMap);
  const [selectedType, setSelectedType] = React.useState(
    currentSimulationObject.objectType,
  );
  const [searchParams, setSearchParams] = useSearchParams();
  // Format configArray for SelectInput
  const selectData = configNameArray.map((obj, index) => ({
    value: obj[0],
    label: obj[1],
  }));
 
  //Create handle for selecting turbine type:
  const handleSelectChange = async (value: string) => {
    setSelectedType(value);
    const result = await changeTurbineType({
      updateTurbineRequest: {
        turbine_type: value,
        simulation_object: currentSimulationObject.id,
      },
    });
    const newSearchParams = new URLSearchParams(searchParams);
    const id = result.data;
    newSearchParams.set("object", id.toString());
    newSearchParams.set("content", ContentTypes.objectDetails);
    setSearchParams(newSearchParams);
  };
 
  //Display formatting:
  return (
    <SelectInput
      title="Change Model Type"
      value={selectedType}
      data={selectData}
      handleChange={handleSelectChange}
      className={!selectedType ? "border-rose-700" : " "}
    />
  );
}