All files / src/pages/flowsheet-page/flowsheet/PropertiesSidebar/PropertyPackages PropertyPackage.tsx

93.33% Statements 14/15
87.5% Branches 7/8
100% Functions 3/3
100% Lines 14/14

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                                  569x 569x 569x 569x           569x     569x   5530x     8x   8x         8x           8x                 1x                       5137x                                             483x                
import SelectInput from "@/ahuora-design-system/inputs/SelectInput";
import {
  api,
  useUnitopsSimulationobjectsPartialUpdateMutation,
} from "@/api/apiStore.gen";
import allPropertyPackages from "@/data/propertyPackages.json";
import { useCurrentObject } from "@/hooks/flowsheetObjects";
import { useAppDispatch } from "@/store/hooks";
import { FeatureFlag, useFeatureFlag } from "../../../../../lib/featureFlags";
import { CustomPackageChooser } from "./CustomPackageChooser";
import { CustomPackageDialog } from "./CustomPackageDialog";
 
/**
 * a single property package slot
 */
function PropertyPackageSlot() {
  const [updateSimulationObject] =
    useUnitopsSimulationobjectsPartialUpdateMutation();
  const dispatch = useAppDispatch();
  const unitOp = useCurrentObject();
  const currentSelectedName = unitOp?.propertyPackageType;
 
  // We use feature flags to determine wheather to show the custom property package option in the dropdown,
  //  but we want to keep the option to open the modal to view the custom property package for existing users
  // who have already set a property package to custom.
 
  const [customPropertyPackagesEnabled] = useFeatureFlag(
    FeatureFlag.CUSTOM_PROPERTY_PACKAGES,
  );
  const availablePropertyPackages = customPropertyPackagesEnabled
    ? allPropertyPackages
    : allPropertyPackages.filter((pkg) => pkg.key !== "custom");
 
  function onChange(pkgKey: string) {
    Iif (!unitOp) return;
    // optimistic update
    dispatch(
      api.util.updateQueryData(
        "unitopsSimulationobjectsRetrieve",
        { id: unitOp!.id },
        (cachedSimulationObject) => {
          cachedSimulationObject.propertyPackageType = pkgKey;
        },
      ),
    );
 
    //Update in backend
    updateSimulationObject({
      id: unitOp!.id,
      patchedSimulationObject: {
        propertyPackageType: pkgKey,
      },
    });
  }
 
  function onCustomPackageChange(newCustomPackageId: number) {
    updateSimulationObject({
      id: unitOp!.id,
      patchedSimulationObject: {
        customPackage: newCustomPackageId,
      },
    });
  }
 
  return (
    <div className="flex flex-col gap-3">
      <SelectInput
        title="Select property package"
        data={availablePropertyPackages.map((pkg) => ({
          label: pkg.name,
          value: pkg.key,
        }))}
        value={currentSelectedName}
        handleChange={onChange}
      />
      {currentSelectedName === "custom" && (
        <CustomPackageChooser
          customPackageId={unitOp?.customPackage}
          onCustomPackageChange={onCustomPackageChange}
        />
      )}
      {currentSelectedName === "custom" && unitOp?.customPackage && (
        <CustomPackageDialog
          propertyPackageId={unitOp.customPackage}
        ></CustomPackageDialog>
      )}
    </div>
  );
}
 
function PropertyPackageSettings() {
  return (
    <div className="flex flex-col gap-3">
      <PropertyPackageSlot />
    </div>
  );
}
 
export default PropertyPackageSettings;