Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

refactor: pull data fetching layer of components into hook

Brad Barbin9ac6f85c32ea
diff --git a/apps/carbon/app/components/Form/Ability.tsx b/apps/carbon/app/components/Form/Ability.tsx
index 86004fe9f7..425ee6264d 100644
--- a/apps/carbon/app/components/Form/Ability.tsx
+++ b/apps/carbon/app/components/Form/Ability.tsx
@@ -11,6 +11,18 @@ import Combobox from "./Combobox";
type AbilitySelectProps = Omit<ComboboxProps, "options">;
const Ability = (props: AbilitySelectProps) => {
+ const options = useAbilities();
+
+ return (
+ <Combobox options={options} {...props} label={props?.label ?? "Ability"} />
+ );
+};
+
+Ability.displayName = "Ability";
+
+export default Ability;
+
+export const useAbilities = () => {
const abilityFetcher =
useFetcher<Awaited<ReturnType<typeof getAbilitiesList>>>();
@@ -29,11 +41,5 @@ const Ability = (props: AbilitySelectProps) => {
[abilityFetcher.data]
);
- return (
- <Combobox options={options} {...props} label={props?.label ?? "Ability"} />
- );
+ return options;
};
-
-Ability.displayName = "Ability";
-
-export default Ability;
diff --git a/apps/carbon/app/components/Form/ConversionFactor.tsx b/apps/carbon/app/components/Form/ConversionFactor.tsx
index d83796c17e..ed06027db0 100644
--- a/apps/carbon/app/components/Form/ConversionFactor.tsx
+++ b/apps/carbon/app/components/Form/ConversionFactor.tsx
@@ -19,10 +19,12 @@ import {
VStack,
} from "@carbon/react";
import { useControlField, useField } from "@carbon/remix-validated-form";
+import { twoDecimals } from "@carbon/utils";
+import type { ElementRef } from "react";
import { forwardRef, useMemo, useRef, useState } from "react";
import { LuChevronDown, LuChevronUp } from "react-icons/lu";
import { MdTranslate } from "react-icons/md";
-import { useUnitOfMeasureOptions } from "./UnitOfMeasure";
+import { useUnitOfMeasure } from "./UnitOfMeasure";
enum ConversionDirection {
PurchasedToInventory,
@@ -40,7 +42,10 @@ type ConversionFactorProps = {
onChange?: (newValue: number) => void;
};
-const ConversionFactor = forwardRef<HTMLInputElement, ConversionFactorProps>(
+const ConversionFactor = forwardRef<
+ ElementRef<typeof CommandTrigger>,
+ ConversionFactorProps
+>(
(
{
name,
@@ -51,7 +56,6 @@ const ConversionFactor = forwardRef<HTMLInputElement, ConversionFactorProps>(
onChange,
purchasingCode,
inventoryCode,
- ...rest
},
ref
) => {
@@ -66,7 +70,7 @@ const ConversionFactor = forwardRef<HTMLInputElement, ConversionFactorProps>(
);
const [conversionDirection, setConversionDirection] = useState(
- conversionFactor <= 1
+ conversionFactor > 1
? ConversionDirection.PurchasedToInventory
: ConversionDirection.InventoryToPurchased
);
@@ -79,7 +83,7 @@ const ConversionFactor = forwardRef<HTMLInputElement, ConversionFactorProps>(
}
};
- const unitOfMeasureOptions = useUnitOfMeasureOptions();
+ const unitOfMeasureOptions = useUnitOfMeasure();
const description = useMemo(() => {
const purchaseUnit =
@@ -99,12 +103,12 @@ const ConversionFactor = forwardRef<HTMLInputElement, ConversionFactorProps>(
if (conversionDirection === ConversionDirection.InventoryToPurchased) {
return `There ${conversionFactor === 1 ? "is" : "are"} ${twoDecimals(
- conversionFactor
+ inverseOfConversion
)} ${purchaseUnit.toLocaleLowerCase()} in one ${inventoryUnit.toLocaleLowerCase()}`;
}
return `There ${conversionFactor === 1 ? "is" : "are"} ${twoDecimals(
- inverseOfConversion
+ conversionFactor
)} ${inventoryUnit.toLocaleLowerCase()} in one ${purchaseUnit.toLocaleLowerCase()}`;
}, [
conversionDirection,
@@ -114,10 +118,10 @@ const ConversionFactor = forwardRef<HTMLInputElement, ConversionFactorProps>(
unitOfMeasureOptions,
]);
- const onPurchaseUnitChange = (value: number) =>
- setConversionFactor(1 / value);
+ const onPurchaseUnitChange = (value: number) => setConversionFactor(value);
- const onInventoryUnitChange = (value: number) => setConversionFactor(value);
+ const onInventoryUnitChange = (value: number) =>
+ setConversionFactor(1 / value);
const onConfirm = () => {
setControlValue(conversionFactor);
@@ -143,18 +147,10 @@ const ConversionFactor = forwardRef<HTMLInputElement, ConversionFactorProps>(
<CommandTrigger
disabled={isReadOnly}
icon={<MdTranslate className="w-4 h-4 opacity-50" />}
- // ref={ref}
+ ref={ref}
onClick={() => setOpen(true)}
- // {...props}
>
- {/* {value ? (
- options.find((option) => option.value === value)?.label
- ) : (
- <span className="!text-muted-foreground">
- {placeholder ?? "Select"}
- </span>
- )} */}
- {twoDecimals(controlValue)}
+ {controlValue ? twoDecimals(controlValue) : "-"}
</CommandTrigger>
<ModalContent>
@@ -258,10 +254,3 @@ const ConversionFactor = forwardRef<HTMLInputElement, ConversionFactorProps>(
ConversionFactor.displayName = "ConversionFactor";
export default ConversionFactor;
-
-function twoDecimals(n: number) {
- var log10 = n ? Math.floor(Math.log10(n)) : 0,
- div = log10 < 0 ? Math.pow(10, 1 - log10) : 100;
-
- return Math.round(n * div) / div;
-}
diff --git a/apps/carbon/app/components/Form/Currency.tsx b/apps/carbon/app/components/Form/Currency.tsx
index 251a51ce66..e173b5cf36 100644
--- a/apps/carbon/app/components/Form/Currency.tsx
+++ b/apps/carbon/app/components/Form/Currency.tsx
@@ -10,6 +10,18 @@ import Combobox from "./Combobox";
type CurrencySelectProps = Omit<ComboboxProps, "options">;
const Currency = ({ ...props }: CurrencySelectProps) => {
+ const options = useCurrencies();
+
+ return (
+ <Combobox {...props} options={options} label={props?.label ?? "Currency"} />
+ );
+};
+
+Currency.displayName = "Currency";
+
+export default Currency;
+
+const useCurrencies = () => {
const currencyFetcher =
useFetcher<Awaited<ReturnType<typeof getCurrenciesList>>>();
@@ -28,11 +40,5 @@ const Currency = ({ ...props }: CurrencySelectProps) => {
[currencyFetcher.data]
);
- return (
- <Combobox {...props} options={options} label={props?.label ?? "Currency"} />
- );
+ return options;
};
-
-Currency.displayName = "Currency";
-
-export default Currency;
diff --git a/apps/carbon/app/components/Form/CustomerStatus.tsx b/apps/carbon/app/components/Form/CustomerStatus.tsx
index 46eeb6ac68..ee6b74659e 100644
--- a/apps/carbon/app/components/Form/CustomerStatus.tsx
+++ b/apps/carbon/app/components/Form/CustomerStatus.tsx
@@ -14,39 +14,11 @@ import CreatableCombobox from "./CreatableCombobox";
type CustomerStatusSelectProps = Omit<ComboboxProps, "options">;
const CustomerStatus = (props: CustomerStatusSelectProps) => {
- const customerStatusFetcher =
- useFetcher<Awaited<ReturnType<typeof getCustomerStatusesList>>>();
-
- const sharedCustomerData = useRouteData<{
- customerStatuses: CustomerStatusStatus[];
- }>(path.to.customerRoot);
-
const newCustomerStatusModal = useDisclosure();
const [created, setCreated] = useState<string>("");
const triggerRef = useRef<HTMLButtonElement>(null);
- const hasCustomerData = sharedCustomerData?.customerStatuses;
-
- useMount(() => {
- if (!hasCustomerData)
- customerStatusFetcher.load(path.to.api.customerStatuses);
- });
-
- const options = useMemo(() => {
- const dataSource =
- (hasCustomerData
- ? sharedCustomerData.customerStatuses
- : customerStatusFetcher.data?.data) ?? [];
-
- return dataSource.map((c) => ({
- value: c.id,
- label: c.name,
- }));
- }, [
- customerStatusFetcher.data?.data,
- hasCustomerData,
- sharedCustomerData?.customerStatuses,
- ]);
+ const options = useCustomerStatuses();
return (
<>
@@ -80,3 +52,37 @@ const CustomerStatus = (props: CustomerStatusSelectProps) => {
CustomerStatus.displayName = "CustomerStatus";
export default CustomerStatus;
+
+export const useCustomerStatuses = () => {
+ const customerStatusFetcher =
+ useFetcher<Awaited<ReturnType<typeof getCustomerStatusesList>>>();
+
+ const sharedCustomerData = useRouteData<{
+ customerStatuses: CustomerStatusStatus[];
+ }>(path.to.customerRoot);
+
+ const hasCustomerData = sharedCustomerData?.customerStatuses;
+
+ useMount(() => {
+ if (!hasCustomerData)
+ customerStatusFetcher.load(path.to.api.customerStatuses);
+ });
+
+ const options = useMemo(() => {
+ const dataSource =
+ (hasCustomerData
+ ? sharedCustomerData.customerStatuses
+ : customerStatusFetcher.data?.data) ?? [];
+
+ return dataSource.map((c) => ({
+ value: c.id,
+ label: c.name,
+ }));
+ }, [
+ customerStatusFetcher.data?.data,
+ hasCustomerData,
+ sharedCustomerData?.customerStatuses,
+ ]);
+
+ return options;
+};
diff --git a/apps/carbon/app/components/Form/CustomerType.tsx b/apps/carbon/app/components/Form/CustomerType.tsx
index 6edc03e9d2..b7ffe60467 100644
--- a/apps/carbon/app/components/Form/CustomerType.tsx
+++ b/apps/carbon/app/components/Form/CustomerType.tsx
@@ -12,38 +12,11 @@ import CreatableCombobox from "./CreatableCombobox";
type CustomerTypeSelectProps = Omit<ComboboxProps, "options">;
const CustomerType = (props: CustomerTypeSelectProps) => {
- const customerTypeFetcher =
- useFetcher<Awaited<ReturnType<typeof getCustomerTypesList>>>();
-
- const sharedCustomerData = useRouteData<{
- customerTypes: SupplierStatus[];
- }>(path.to.customerRoot);
-
const newCustomerTypeModal = useDisclosure();
const [created, setCreated] = useState<string>("");
const triggerRef = useRef<HTMLButtonElement>(null);
- const hasSupplierData = sharedCustomerData?.customerTypes;
-
- useMount(() => {
- if (!hasSupplierData) customerTypeFetcher.load(path.to.api.customerTypes);
- });
-
- const options = useMemo(() => {
- const dataSource =
- (hasSupplierData
- ? sharedCustomerData.customerTypes
- : customerTypeFetcher.data?.data) ?? [];
-
- return dataSource.map((c) => ({
- value: c.id,
- label: c.name,
- }));
- }, [
- customerTypeFetcher.data?.data,
- hasSupplierData,
- sharedCustomerData?.customerTypes,
- ]);
+ const options = useCustomerTypes();
return (
<>
@@ -77,3 +50,36 @@ const CustomerType = (props: CustomerTypeSelectProps) => {
CustomerType.displayName = "CustomerType";
export default CustomerType;
+
+export const useCustomerTypes = () => {
+ const customerTypeFetcher =
+ useFetcher<Awaited<ReturnType<typeof getCustomerTypesList>>>();
+
+ const sharedCustomerData = useRouteData<{
+ customerTypes: SupplierStatus[];
+ }>(path.to.customerRoot);
+
+ const hasSupplierData = sharedCustomerData?.customerTypes;
+
+ useMount(() => {
+ if (!hasSupplierData) customerTypeFetcher.load(path.to.api.customerTypes);
+ });
+
+ const options = useMemo(() => {
+ const dataSource =
+ (hasSupplierData
+ ? sharedCustomerData.customerTypes
+ : customerTypeFetcher.data?.data) ?? [];
+
+ return dataSource.map((c) => ({
+ value: c.id,
+ label: c.name,
+ }));
+ }, [
+ customerTypeFetcher.data?.data,
+ hasSupplierData,
+ sharedCustomerData?.customerTypes,
+ ]);
+
+ return options;
+};
diff --git a/apps/carbon/app/components/Form/Department.tsx b/apps/carbon/app/components/Form/Department.tsx
index d74df54c48..9613c67147 100644
--- a/apps/carbon/app/components/Form/Department.tsx
+++ b/apps/carbon/app/components/Form/Department.tsx
@@ -10,27 +10,11 @@ import CreatableCombobox from "./CreatableCombobox";
type DepartmentSelectProps = Omit<ComboboxProps, "options">;
const Department = (props: DepartmentSelectProps) => {
- const departmentFetcher =
- useFetcher<Awaited<ReturnType<typeof getDepartmentsList>>>();
-
const newDepartmentModal = useDisclosure();
const [created, setCreated] = useState<string>("");
const triggerRef = useRef<HTMLButtonElement>(null);
- useMount(() => {
- departmentFetcher.load(path.to.api.departments);
- });
-
- const options = useMemo(
- () =>
- departmentFetcher.data?.data
- ? departmentFetcher.data?.data.map((c) => ({
- value: c.id,
- label: c.name,
- }))
- : [],
- [departmentFetcher.data]
- );
+ const options = useDepartments();
return (
<>
@@ -64,3 +48,25 @@ const Department = (props: DepartmentSelectProps) => {
Department.displayName = "Department";
export default Department;
+
+export const useDepartments = () => {
+ const departmentFetcher =
+ useFetcher<Awaited<ReturnType<typeof getDepartmentsList>>>();
+
+ useMount(() => {
+ departmentFetcher.load(path.to.api.departments);
+ });
+
+ const options = useMemo(
+ () =>
+ departmentFetcher.data?.data
+ ? departmentFetcher.data?.data.map((c) => ({
+ value: c.id,
+ label: c.name,
+ }))
+ : [],
+ [departmentFetcher.data]
+ );
+
+ return options;
+};
diff --git a/apps/carbon/app/components/Form/EquipmentType.tsx b/apps/carbon/app/components/Form/EquipmentType.tsx
index 8cacb88ff2..c333fab5c5 100644
--- a/apps/carbon/app/components/Form/EquipmentType.tsx
+++ b/apps/carbon/app/components/Form/EquipmentType.tsx
@@ -10,27 +10,11 @@ import CreatableCombobox from "./CreatableCombobox";
type EquipmentTypeSelectProps = Omit<ComboboxProps, "options">;
const EquipmentType = (props: EquipmentTypeSelectProps) => {
- const equipmentTypeFetcher =
- useFetcher<Awaited<ReturnType<typeof getEquipmentTypesList>>>();
-
+ const triggerRef = useRef<HTMLButtonElement>(null);
const newEquipmentTypeModal = useDisclosure();
const [created, setCreated] = useState<string>("");
- const triggerRef = useRef<HTMLButtonElement>(null);
- useMount(() => {
- equipmentTypeFetcher.load(path.to.api.equipmentTypes);
- });
-
- const options = useMemo(
- () =>
- equipmentTypeFetcher.data?.data
- ? equipmentTypeFetcher.data?.data.map((c) => ({
- value: c.id,
- label: c.name,
- }))
- : [],
- [equipmentTypeFetcher.data]
- );
+ const options = useEquipmentTypes();
return (
<>
@@ -62,7 +46,27 @@ const EquipmentType = (props: EquipmentTypeSelectProps) => {
</>
);
};
-
+export default EquipmentType;
EquipmentType.displayName = "EquipmentType";
-export default EquipmentType;
+const useEquipmentTypes = () => {
+ const equipmentTypeFetcher =
+ useFetcher<Awaited<ReturnType<typeof getEquipmentTypesList>>>();
+
+ useMount(() => {
+ equipmentTypeFetcher.load(path.to.api.equipmentTypes);
+ });
+
+ const options = useMemo(
+ () =>
+ equipmentTypeFetcher.data?.data
+ ? equipmentTypeFetcher.data?.data.map((c) => ({
+ value: c.id,
+ label: c.name,
+ }))
+ : [],
+ [equipmentTypeFetcher.data]
+ );
+
+ return options;
+};
diff --git a/apps/carbon/app/components/Form/Location.tsx b/apps/carbon/app/components/Form/Location.tsx
index 6d62562675..ede939e7e6 100644
--- a/apps/carbon/app/components/Form/Location.tsx
+++ b/apps/carbon/app/components/Form/Location.tsx
@@ -11,27 +11,11 @@ import CreatableCombobox from "./CreatableCombobox";
type LocationSelectProps = Omit<ComboboxProps, "options">;
const Location = (props: LocationSelectProps) => {
- const locationFetcher =
- useFetcher<Awaited<ReturnType<typeof getLocationsList>>>();
-
const newLocationModal = useDisclosure();
const [created, setCreated] = useState<string>("");
const triggerRef = useRef<HTMLButtonElement>(null);
- useMount(() => {
- locationFetcher.load(path.to.api.locations);
- });
-
- const options = useMemo(
- () =>
- locationFetcher.data?.data
- ? locationFetcher.data?.data.map((c) => ({
- value: c.id,
- label: c.name,
- }))
- : [],
- [locationFetcher.data]
- );
+ const options = useLocations();
return (
<>
@@ -71,3 +55,25 @@ const Location = (props: LocationSelectProps) => {
Location.displayName = "Location";
export default Location;
+
+const useLocations = () => {
+ const locationFetcher =
+ useFetcher<Awaited<ReturnType<typeof getLocationsList>>>();
+
+ useMount(() => {
+ locationFetcher.load(path.to.api.locations);
+ });
+
+ const options = useMemo(
+ () =>
+ locationFetcher.data?.data
+ ? locationFetcher.data?.data.map((c) => ({
+ value: c.id,
+ label: c.name,
+ }))
+ : [],
+ [locationFetcher.data]
+ );
+
+ return options;
+};
diff --git a/apps/carbon/app/components/Form/PartGroup.tsx b/apps/carbon/app/components/Form/PartGroup.tsx
index 960b1c570d..7357615ac8 100644
--- a/apps/carbon/app/components/Form/PartGroup.tsx
+++ b/apps/carbon/app/components/Form/PartGroup.tsx
@@ -10,27 +10,11 @@ import CreatableCombobox from "./CreatableCombobox";
type PartGroupSelectProps = Omit<ComboboxProps, "options">;
const PartGroup = (props: PartGroupSelectProps) => {
- const partGroupFetcher =
- useFetcher<Awaited<ReturnType<typeof getPartGroupsList>>>();
-
const newPartGroupModal = useDisclosure();
const [created, setCreated] = useState<string>("");
const triggerRef = useRef<HTMLButtonElement>(null);
- useMount(() => {
- partGroupFetcher.load(path.to.api.partGroups);
- });
-
- const options = useMemo(
- () =>
- partGroupFetcher.data?.data
- ? partGroupFetcher.data?.data.map((c) => ({
- value: c.id,
- label: c.name,
- }))
- : [],
- [partGroupFetcher.data]
- );
+ const options = usePartGroups();
return (
<>
@@ -64,3 +48,25 @@ const PartGroup = (props: PartGroupSelectProps) => {
PartGroup.displayName = "PartGroup";
export default PartGroup;
+
+export const usePartGroups = () => {
+ const partGroupFetcher =
+ useFetcher<Awaited<ReturnType<typeof getPartGroupsList>>>();
+
+ useMount(() => {
+ partGroupFetcher.load(path.to.api.partGroups);
+ });
+
+ const options = useMemo(
+ () =>
+ partGroupFetcher.data?.data
+ ? partGroupFetcher.data?.data.map((c) => ({
+ value: c.id,
+ label: c.name,
+ }))
+ : [],
+ [partGroupFetcher.data]
+ );
+
+ return options;
+};
diff --git a/apps/carbon/app/components/Form/Service.tsx b/apps/carbon/app/components/Form/Service.tsx
index 2caab9e053..e98e95f3ec 100644
--- a/apps/carbon/app/components/Form/Service.tsx
+++ b/apps/carbon/app/components/Form/Service.tsx
@@ -11,6 +11,18 @@ type ServiceSelectProps = Omit<ComboboxProps, "options"> & {
};
const Service = ({ serviceType, ...props }: ServiceSelectProps) => {
+ const options = useServices(serviceType);
+
+ return (
+ <Combobox options={options} {...props} label={props?.label ?? "Service"} />
+ );
+};
+
+Service.displayName = "Service";
+
+export default Service;
+
+export const useServices = (serviceType: ServiceType) => {
const servicesFetcher =
useFetcher<Awaited<ReturnType<typeof getServicesList>>>();
@@ -30,11 +42,5 @@ const Service = ({ serviceType, ...props }: ServiceSelectProps) => {
[servicesFetcher.data]
);
- return (
- <Combobox options={options} {...props} label={props?.label ?? "Service"} />
- );
+ return options;
};
-
-Service.displayName = "Service";
-
-export default Service;
diff --git a/apps/carbon/app/components/Form/Shift.tsx b/apps/carbon/app/components/Form/Shift.tsx
index bc10a68425..1fab8a9736 100644
--- a/apps/carbon/app/components/Form/Shift.tsx
+++ b/apps/carbon/app/components/Form/Shift.tsx
@@ -11,6 +11,18 @@ type ShiftSelectProps = Omit<SelectProps, "options"> & {
};
const Shift = (props: ShiftSelectProps) => {
+ const options = useShifts({
+ location: props.location,
+ });
+
+ return (
+ <Select options={options} {...props} label={props?.label ?? "Shift"} />
+ );
+};
+
+export default Shift;
+
+export const useShifts = (props?: { location?: string }) => {
const shiftFetcher = useFetcher<Awaited<ReturnType<typeof getShiftsList>>>();
useEffect(() => {
@@ -18,7 +30,7 @@ const Shift = (props: ShiftSelectProps) => {
shiftFetcher.load(path.to.api.shifts(props.location));
}
// eslint-disable-next-line react-hooks/exhaustive-deps
- }, [props.location]);
+ }, [props?.location]);
const options = useMemo(
() =>
@@ -30,9 +42,5 @@ const Shift = (props: ShiftSelectProps) => {
[shiftFetcher.data]
);
- return (
- <Select options={options} {...props} label={props?.label ?? "Shift"} />
- );
+ return options;
};
-
-export default Shift;
diff --git a/apps/carbon/app/components/Form/SupplierLocation.tsx b/apps/carbon/app/components/Form/SupplierLocation.tsx
index f443b15ece..4c9f7eafd9 100644
--- a/apps/carbon/app/components/Form/SupplierLocation.tsx
+++ b/apps/carbon/app/components/Form/SupplierLocation.tsx
@@ -20,12 +20,11 @@ type SupplierLocationSelectProps = Omit<
};
const SupplierLocation = (props: SupplierLocationSelectProps) => {
- const supplierLocationsFetcher =
- useFetcher<Awaited<ReturnType<typeof getSupplierLocations>>>();
-
const newLocationModal = useDisclosure();
// const [created, setCreated] = useState<string>("");
const triggerRef = useRef<HTMLButtonElement>(null);
+ const supplierLocationsFetcher =
+ useFetcher<Awaited<ReturnType<typeof getSupplierLocations>>>();
useEffect(() => {
if (props?.supplier) {
diff --git a/apps/carbon/app/components/Form/SupplierStatus.tsx b/apps/carbon/app/components/Form/SupplierStatus.tsx
index 119864dae9..eec32f6282 100644
--- a/apps/carbon/app/components/Form/SupplierStatus.tsx
+++ b/apps/carbon/app/components/Form/SupplierStatus.tsx
@@ -14,39 +14,11 @@ import CreatableCombobox from "./CreatableCombobox";
type SupplierStatusSelectProps = Omit<ComboboxProps, "options">;
const SupplierStatus = (props: SupplierStatusSelectProps) => {
- const supplierStatusFetcher =
- useFetcher<Awaited<ReturnType<typeof getSupplierStatusesList>>>();
-
- const sharedSupplierData = useRouteData<{
- supplierStatuses: SupplierStatusStatus[];
- }>(path.to.supplierRoot);
-
const newSupplierStatusModal = useDisclosure();
const [created, setCreated] = useState<string>("");
const triggerRef = useRef<HTMLButtonElement>(null);
- const hasSupplierData = sharedSupplierData?.supplierStatuses;
-
- useMount(() => {
- if (!hasSupplierData)
- supplierStatusFetcher.load(path.to.api.supplierStatuses);
- });
-
- const options = useMemo(() => {
- const dataSource =
- (hasSupplierData
- ? sharedSupplierData.supplierStatuses
- : supplierStatusFetcher.data?.data) ?? [];
-
- return dataSource.map((c) => ({
- value: c.id,
- label: c.name,
- }));
- }, [
- supplierStatusFetcher.data?.data,
- hasSupplierData,
- sharedSupplierData?.supplierStatuses,
- ]);
+ const options = useSupplierStatuses();
return (
<>
@@ -80,3 +52,37 @@ const SupplierStatus = (props: SupplierStatusSelectProps) => {
SupplierStatus.displayName = "SupplierStatus";
export default SupplierStatus;
+
+export const useSupplierStatuses = () => {
+ const supplierStatusFetcher =
+ useFetcher<Awaited<ReturnType<typeof getSupplierStatusesList>>>();
+
+ const sharedSupplierData = useRouteData<{
+ supplierStatuses: SupplierStatusStatus[];
+ }>(path.to.supplierRoot);
+
+ const hasSupplierData = sharedSupplierData?.supplierStatuses;
+
+ useMount(() => {
+ if (!hasSupplierData)
+ supplierStatusFetcher.load(path.to.api.supplierStatuses);
+ });
+
+ const options = useMemo(() => {
+ const dataSource =
+ (hasSupplierData
+ ? sharedSupplierData.supplierStatuses
+ : supplierStatusFetcher.data?.data) ?? [];
+
+ return dataSource.map((c) => ({
+ value: c.id,
+ label: c.name,
+ }));
+ }, [
+ supplierStatusFetcher.data?.data,
+ hasSupplierData,
+ sharedSupplierData?.supplierStatuses,
+ ]);
+
+ return options;
+};
diff --git a/apps/carbon/app/components/Form/SupplierType.tsx b/apps/carbon/app/components/Form/SupplierType.tsx
index d35a7b6ffb..40e8f406e6 100644
--- a/apps/carbon/app/components/Form/SupplierType.tsx
+++ b/apps/carbon/app/components/Form/SupplierType.tsx
@@ -14,38 +14,11 @@ import CreatableCombobox from "./CreatableCombobox";
type SupplierTypeSelectProps = Omit<ComboboxProps, "options">;
const SupplierType = (props: SupplierTypeSelectProps) => {
- const supplierTypeFetcher =
- useFetcher<Awaited<ReturnType<typeof getSupplierTypesList>>>();
-
- const sharedSupplierData = useRouteData<{
- supplierTypes: SupplierTypeType[];
- }>(path.to.supplierRoot);
-
const newSupplierTypeModal = useDisclosure();
const [created, setCreated] = useState<string>("");
const triggerRef = useRef<HTMLButtonElement>(null);
- const hasSupplierData = sharedSupplierData?.supplierTypes;
-
- useMount(() => {
- if (!hasSupplierData) supplierTypeFetcher.load(path.to.api.supplierTypes);
- });
-
- const options = useMemo(() => {
- const dataSource =
- (hasSupplierData
- ? sharedSupplierData.supplierTypes
- : supplierTypeFetcher.data?.data) ?? [];
-
- return dataSource.map((c) => ({
- value: c.id,
- label: c.name,
- }));
- }, [
- supplierTypeFetcher.data?.data,
- hasSupplierData,
- sharedSupplierData?.supplierTypes,
- ]);
+ const options = useSupplierTypes();
return (
<>
@@ -79,3 +52,36 @@ const SupplierType = (props: SupplierTypeSelectProps) => {
SupplierType.displayName = "SupplierType";
export default SupplierType;
+
+export const useSupplierTypes = () => {
+ const supplierTypeFetcher =
+ useFetcher<Awaited<ReturnType<typeof getSupplierTypesList>>>();
+
+ const sharedSupplierData = useRouteData<{
+ supplierTypes: SupplierTypeType[];
+ }>(path.to.supplierRoot);
+
+ const hasSupplierData = sharedSupplierData?.supplierTypes;
+
+ useMount(() => {
+ if (!hasSupplierData) supplierTypeFetcher.load(path.to.api.supplierTypes);
+ });
+
+ const options = useMemo(() => {
+ const dataSource =
+ (hasSupplierData
+ ? sharedSupplierData.supplierTypes
+ : supplierTypeFetcher.data?.data) ?? [];
+
+ return dataSource.map((c) => ({
+ value: c.id,
+ label: c.name,
+ }));
+ }, [
+ supplierTypeFetcher.data?.data,
+ hasSupplierData,
+ sharedSupplierData?.supplierTypes,
+ ]);
+
+ return options;
+};
diff --git a/apps/carbon/app/components/Form/UnitOfMeasure.tsx b/apps/carbon/app/components/Form/UnitOfMeasure.tsx
index c6581ae507..dab5684e63 100644
--- a/apps/carbon/app/components/Form/UnitOfMeasure.tsx
+++ b/apps/carbon/app/components/Form/UnitOfMeasure.tsx
@@ -14,7 +14,7 @@ import CreatableCombobox from "./CreatableCombobox";
type UnitOfMeasureSelectProps = Omit<ComboboxProps, "options">;
const UnitOfMeasure = (props: UnitOfMeasureSelectProps) => {
- const options = useUnitOfMeasureOptions();
+ const options = useUnitOfMeasure();
const newUnitOfMeasureModal = useDisclosure();
const [created, setCreated] = useState<string>("");
@@ -54,7 +54,7 @@ UnitOfMeasure.displayName = "UnitOfMeasure";
export default UnitOfMeasure;
-export const useUnitOfMeasureOptions = () => {
+export const useUnitOfMeasure = () => {
const uomFetcher =
useFetcher<Awaited<ReturnType<typeof getUnitOfMeasuresList>>>();
diff --git a/apps/carbon/app/components/Form/WorkCellType.tsx b/apps/carbon/app/components/Form/WorkCellType.tsx
index ad181319fe..60f0bacd54 100644
--- a/apps/carbon/app/components/Form/WorkCellType.tsx
+++ b/apps/carbon/app/components/Form/WorkCellType.tsx
@@ -10,27 +10,11 @@ import CreatableCombobox from "./CreatableCombobox";
type WorkCellTypeSelectProps = Omit<ComboboxProps, "options">;
const WorkCellType = (props: WorkCellTypeSelectProps) => {
- const workCellTypeFetcher =
- useFetcher<Awaited<ReturnType<typeof getWorkCellTypesList>>>();
-
const newWorkCellTypeModal = useDisclosure();
const [created, setCreated] = useState<string>("");
const triggerRef = useRef<HTMLButtonElement>(null);
- useMount(() => {
- workCellTypeFetcher.load(path.to.api.workCellTypes);
- });
-
- const options = useMemo(
- () =>
- workCellTypeFetcher.data?.data
- ? workCellTypeFetcher.data?.data.map((c) => ({
- value: c.id,
- label: c.name,
- }))
- : [],
- [workCellTypeFetcher.data]
- );
+ const options = useWorkCellTypes();
return (
<>
@@ -69,3 +53,25 @@ const WorkCellType = (props: WorkCellTypeSelectProps) => {
WorkCellType.displayName = "WorkCellType";
export default WorkCellType;
+
+export const useWorkCellTypes = () => {
+ const workCellTypeFetcher =
+ useFetcher<Awaited<ReturnType<typeof getWorkCellTypesList>>>();
+
+ useMount(() => {
+ workCellTypeFetcher.load(path.to.api.workCellTypes);
+ });
+
+ const options = useMemo(
+ () =>
+ workCellTypeFetcher.data?.data
+ ? workCellTypeFetcher.data?.data.map((c) => ({
+ value: c.id,
+ label: c.name,
+ }))
+ : [],
+ [workCellTypeFetcher.data]
+ );
+
+ return options;
+};
diff --git a/packages/utils/src/math.ts b/packages/utils/src/math.ts
index 8c1bb49dc6..9382b143fc 100644
--- a/packages/utils/src/math.ts
+++ b/packages/utils/src/math.ts
@@ -1,2 +1,9 @@
export const clip = (value: number, min: number, max: number) =>
Math.min(Math.max(value, min), max);
+
+export const twoDecimals = (n: number) => {
+ var log10 = n ? Math.floor(Math.log10(n)) : 0,
+ div = log10 < 0 ? Math.pow(10, 1 - log10) : 100;
+
+ return Math.round(n * div) / div;
+};