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

79.77% Statements 71/89
87.87% Branches 29/33
61.9% Functions 13/21
92.06% Lines 58/63

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 158 159 160 161 162                          13x         13x   14x             1x     14x       14x   14x 14x 14x     14x 1x 1x 1x   1x       15x   14x 1x 1x           18x     2x 14x   2x 2x 2x   2x 2x 2x                             22x   16x       24x           24x 24x     24x 11x 11x 11x   11x       25x   24x 11x 66x   11x           48x   24x   132x                             60x       24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 156x      
import React from "react";
import { AccessControlledInput as Input } from "../../../../../ahuora-design-system/ui/accessControlled";
import { Label } from "../../../../../ahuora-design-system/ui/label";
import {
  PackagePropertyKeyEnum,
  PropertyPackageKeyEnum,
  useCoreCustomCompoundsListQuery,
  useCoreCustomPackagePropertiesPartialUpdateMutation,
  useCoreCustomPropertyPackagesRetrieveQuery,
  useCoreKappasPartialUpdateMutation,
} from "../../../../../api/apiStore.gen";
import { useCustomPropertyPackage } from "./useCustomPropertyPackage";
 
export function CustomPropertyPackageProperties({
  customPackageId,
}: {
  customPackageId: number;
}) {
  const { data: propertyPackage } = useCoreCustomPropertyPackagesRetrieveQuery({
    id: customPackageId,
  });
 
  if (!propertyPackage) {
    return <div>Loading...</div>;
  }
 
  return (
    <div>
      <PackagePropertyInput customPackageId={propertyPackage.id} />
      <KappaInput customPackageId={propertyPackage.id} />
    </div>
  );
}
 
export function KappaInput({ customPackageId }: { customPackageId: number }) {
  const [propertyPackage, optimisticUpdate] =
    useCustomPropertyPackage(customPackageId);
  const { data: compounds } = useCoreCustomCompoundsListQuery({});
  const [saveKappa] = useCoreKappasPartialUpdateMutation();
 
  const updateKappaValue =
    (kappaId: number) => (e: React.ChangeEvent<HTMLInputElement>) => {
      const newValue: string = e.target.value;
      optimisticUpdate((cachedPackage) => {
        cachedPackage?.kappas.forEach((kappa) => {
          if (kappa.id === kappaId) {
            kappa.value = newValue;
          }
        });
      });
    };
 
  const saveKappaValue = (kappaId: number) => () => {
    const kappa = propertyPackage?.kappas.find((k) => k.id === kappaId);
    saveKappa({
      id: kappaId,
      patchedKappa: {
        value: kappa?.value,
      },
    });
  };
 
  return (
    <div>
      <h3>Peng-Robinson Kappa Parameters</h3>
      <div className="grid grid-cols-2 gap-2">
        {propertyPackage?.kappas.map((kappa) => {
          const compound1 = compounds?.find(
            (c) => c.id == kappa.compound1,
          )?.name;
          const compound2 = compounds?.find(
            (c) => c.id == kappa.compound2,
          )?.name;
          return (
            <div key={kappa.id}>
              <Label>
                {compound1}-{compound2}
              </Label>
              <Input
                aria-label={`${compound1}-${compound2} kappa value`}
                type="text"
                value={kappa.value == null ? "" : kappa.value + ""}
                onChange={updateKappaValue(kappa.id)}
                onBlur={saveKappaValue(kappa.id)}
              />
            </div>
          );
        })}
      </div>
    </div>
  );
}
 
export function PackagePropertyInput({
  customPackageId,
}: {
  customPackageId: number;
}) {
  const [propertyPackage, optimisticUpdate] =
    useCustomPropertyPackage(customPackageId);
  const [saveProperty] = useCoreCustomPackagePropertiesPartialUpdateMutation();
 
  const updatePropertyValue =
    (propertyId: number) => (e: React.ChangeEvent<HTMLInputElement>) => {
      const newValue: string = e.target.value;
      optimisticUpdate((cachedPackage) => {
        cachedPackage?.properties.forEach((property) => {
          if (property.id === propertyId) {
            property.value = newValue;
          }
        });
      });
    };
 
  const savePropertyValue = (propertyId: number) => () => {
    const property = propertyPackage?.properties.find(
      (property) => property.id === propertyId,
    );
    saveProperty({
      id: propertyId,
      patchedCustomPropertyPackageProperty: {
        value: +property?.value,
      },
    });
  };
 
  const map = propertyPackage?.properties.reduce(
    (acc, property) => {
      acc[property.package_property_key] = (
        <div key={property.id}>
          <Label>{property.package_property_key}</Label>
          <Input
            aria-label={`${property.package_property_key}`}
            type="text"
            value={property.value == null ? "" : property.value + ""}
            onChange={updatePropertyValue(property.id)}
            onBlur={savePropertyValue(property.id)}
          />
        </div>
      );
      return acc;
    },
    {} as Record<PropertyPackageKeyEnum, JSX.Element>,
  );
 
  return (
    <div className="grid grid-cols-2 gap-2">
      {map?.[PackagePropertyKeyEnum.FlowMolMax]}
      {map?.[PackagePropertyKeyEnum.FlowMolNominal]}
      {map?.[PackagePropertyKeyEnum.FlowMolMin]}
      {map?.[PackagePropertyKeyEnum.PressureMax]}
      {map?.[PackagePropertyKeyEnum.PressureNominal]}
      {map?.[PackagePropertyKeyEnum.PressureMin]}
      {map?.[PackagePropertyKeyEnum.TemperatureMax]}
      {map?.[PackagePropertyKeyEnum.TemperatureNominal]}
      {map?.[PackagePropertyKeyEnum.TemperatureMin]}
      {map?.[PackagePropertyKeyEnum.TemperatureRef]}
      {map?.[PackagePropertyKeyEnum.PressureRef]}
    </div>
  );
}