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.

feat: inline creation for unit of measure

Brad Barbin8d3a0ce5fe8f
diff --git a/apps/carbon/app/components/Form/UnitOfMeasure.tsx b/apps/carbon/app/components/Form/UnitOfMeasure.tsx
new file mode 100644
index 0000000000..a36ab72f6d
--- /dev/null
+++ b/apps/carbon/app/components/Form/UnitOfMeasure.tsx
@@ -0,0 +1,67 @@
+import { useDisclosure, useMount } from "@carbon/react";
+import { useFetcher } from "@remix-run/react";
+import { useMemo, useRef, useState } from "react";
+import type { getUnitOfMeasuresList } from "~/modules/parts";
+import { UnitOfMeasureForm } from "~/modules/parts";
+import { path } from "~/utils/path";
+import type { ComboboxProps } from "./Combobox";
+import CreatableCombobox from "./CreatableCombobox";
+
+type UnitOfMeasureSelectProps = Omit<ComboboxProps, "options">;
+
+const UnitOfMeasure = (props: UnitOfMeasureSelectProps) => {
+ const uomFetcher =
+ useFetcher<Awaited<ReturnType<typeof getUnitOfMeasuresList>>>();
+
+ const newUnitOfMeasureModal = useDisclosure();
+ const [created, setCreated] = useState<string>("");
+ const triggerRef = useRef<HTMLButtonElement>(null);
+
+ useMount(() => {
+ uomFetcher.load(path.to.api.unitOfMeasures);
+ });
+
+ const options = useMemo(
+ () =>
+ uomFetcher.data?.data
+ ? uomFetcher.data?.data.map((c) => ({
+ value: c.code,
+ label: c.name,
+ }))
+ : [],
+ [uomFetcher.data]
+ );
+
+ return (
+ <>
+ <CreatableCombobox
+ ref={triggerRef}
+ options={options}
+ {...props}
+ label={props?.label ?? "Part Group"}
+ onCreateOption={(option) => {
+ newUnitOfMeasureModal.onOpen();
+ setCreated(option);
+ }}
+ />
+ {newUnitOfMeasureModal.isOpen && (
+ <UnitOfMeasureForm
+ type="modal"
+ onClose={() => {
+ setCreated("");
+ newUnitOfMeasureModal.onClose();
+ triggerRef.current?.click();
+ }}
+ initialValues={{
+ name: created,
+ code: "",
+ }}
+ />
+ )}
+ </>
+ );
+};
+
+UnitOfMeasure.displayName = "UnitOfMeasure";
+
+export default UnitOfMeasure;
diff --git a/apps/carbon/app/components/Form/index.ts b/apps/carbon/app/components/Form/index.ts
index 7a6d0ed8e8..fc99e28616 100644
--- a/apps/carbon/app/components/Form/index.ts
+++ b/apps/carbon/app/components/Form/index.ts
@@ -43,6 +43,7 @@ import SupplierLocation from "./SupplierLocation";
import TextArea from "./Textarea";
import TimePicker from "./TimePicker";
import Timezone from "./Timezone";
+import UnitOfMeasure from "./UnitOfMeasure";
import User from "./User";
import Users from "./Users";
import WorkCellType from "./WorkCellType";
@@ -93,6 +94,7 @@ export {
TextArea,
TimePicker,
Timezone,
+ UnitOfMeasure,
User,
Users,
WorkCellType,
diff --git a/apps/carbon/app/modules/parts/ui/Part/PartForm.tsx b/apps/carbon/app/modules/parts/ui/Part/PartForm.tsx
index 54a0c1a76d..d98d3b0f0c 100644
--- a/apps/carbon/app/modules/parts/ui/Part/PartForm.tsx
+++ b/apps/carbon/app/modules/parts/ui/Part/PartForm.tsx
@@ -16,7 +16,6 @@ import { useState } from "react";
import type { z } from "zod";
import {
Boolean,
- Combobox,
Hidden,
Input,
InputControlled,
@@ -24,10 +23,10 @@ import {
Select,
Submit,
TextArea,
+ UnitOfMeasure,
} from "~/components/Form";
-import { usePermissions, useRouteData } from "~/hooks";
+import { usePermissions } from "~/hooks";
import { useSupabase } from "~/lib/supabase";
-import type { PartGroupListItem, UnitOfMeasureListItem } from "~/modules/parts";
import {
partReplenishmentSystems,
partTypes,
@@ -87,11 +86,6 @@ const useNextPartIdShortcut = () => {
};
const PartForm = ({ initialValues, type = "card", onClose }: PartFormProps) => {
- const sharedPartsData = useRouteData<{
- partGroups: PartGroupListItem[];
- unitOfMeasures: UnitOfMeasureListItem[];
- }>(path.to.partRoot);
-
const fetcher = useFetcher();
const { partId, onPartIdChange, loading } = useNextPartIdShortcut();
const permissions = usePermissions();
@@ -109,12 +103,6 @@ const PartForm = ({ initialValues, type = "card", onClose }: PartFormProps) => {
value: partReplenishmentSystem,
})) ?? [];
- const unitOfMeasureOptions =
- sharedPartsData?.unitOfMeasures.map((uom) => ({
- label: uom.name,
- value: uom.code,
- })) ?? [];
-
return (
<ModalCardProvider type={type}>
<ModalCard onClose={onClose}>
@@ -174,10 +162,9 @@ const PartForm = ({ initialValues, type = "card", onClose }: PartFormProps) => {
label="Part Type"
options={partTypeOptions}
/>
- <Combobox
+ <UnitOfMeasure
name="unitOfMeasureCode"
label="Unit of Measure"
- options={unitOfMeasureOptions}
/>
</VStack>
<VStack>
diff --git a/apps/carbon/app/modules/parts/ui/UnitOfMeasure/UnitOfMeasureForm.tsx b/apps/carbon/app/modules/parts/ui/UnitOfMeasure/UnitOfMeasureForm.tsx
index 2dddd8edf9..993bcb4976 100644
--- a/apps/carbon/app/modules/parts/ui/UnitOfMeasure/UnitOfMeasureForm.tsx
+++ b/apps/carbon/app/modules/parts/ui/UnitOfMeasure/UnitOfMeasureForm.tsx
@@ -1,17 +1,17 @@
import {
Button,
- Drawer,
- DrawerBody,
- DrawerContent,
- DrawerFooter,
- DrawerHeader,
- DrawerTitle,
HStack,
+ ModalDrawer,
+ ModalDrawerBody,
+ ModalDrawerContent,
+ ModalDrawerFooter,
+ ModalDrawerHeader,
+ ModalDrawerProvider,
+ ModalDrawerTitle,
VStack,
} from "@carbon/react";
-
import { ValidatedForm } from "@carbon/remix-validated-form";
-import { useFetcher, useNavigate } from "@remix-run/react";
+import { useFetcher } from "@remix-run/react";
import type { z } from "zod";
import { Hidden, Input, Submit } from "~/components/Form";
import { usePermissions } from "~/hooks";
@@ -20,13 +20,19 @@ import { path } from "~/utils/path";
type UnitOfMeasureFormProps = {
initialValues: z.infer<typeof unitOfMeasureValidator>;
+ type?: "modal" | "drawer";
+ open?: boolean;
+ onClose: (data?: { id: string; name: string }) => void;
};
-const UnitOfMeasureForm = ({ initialValues }: UnitOfMeasureFormProps) => {
- const fetcher = useFetcher();
+const UnitOfMeasureForm = ({
+ initialValues,
+ open = true,
+ type = "drawer",
+ onClose,
+}: UnitOfMeasureFormProps) => {
const permissions = usePermissions();
- const navigate = useNavigate();
- const onClose = () => navigate(-1);
+ const fetcher = useFetcher();
const isEditing = initialValues.id !== undefined;
const isDisabled = isEditing
@@ -34,48 +40,56 @@ const UnitOfMeasureForm = ({ initialValues }: UnitOfMeasureFormProps) => {
: !permissions.can("create", "parts");
return (
- <Drawer
- open
- onOpenChange={(open) => {
- if (!open) onClose();
- }}
- >
- <DrawerContent>
- <ValidatedForm
- validator={unitOfMeasureValidator}
- method="post"
- action={isEditing ? path.to.uom(initialValues.id!) : path.to.newUom}
- defaultValues={initialValues}
- fetcher={fetcher}
- className="flex flex-col h-full"
- >
- <DrawerHeader>
- <DrawerTitle>
- {isEditing ? "Edit" : "New"} Unit of Measure
- </DrawerTitle>
- </DrawerHeader>
- <DrawerBody>
- <Hidden name="id" />
- <VStack spacing={4}>
- <Input name="name" label="Unit of Measure" />
- <Input
- name="code"
- label="Code"
- helperText="Unique, uppercase, without spaces"
- />
- </VStack>
- </DrawerBody>
- <DrawerFooter>
- <HStack>
- <Submit isDisabled={isDisabled}>Save</Submit>
- <Button size="md" variant="solid" onClick={onClose}>
- Cancel
- </Button>
- </HStack>
- </DrawerFooter>
- </ValidatedForm>
- </DrawerContent>
- </Drawer>
+ <ModalDrawerProvider type={type}>
+ <ModalDrawer
+ open={open}
+ onOpenChange={(open) => {
+ if (!open) onClose?.();
+ }}
+ >
+ <ModalDrawerContent>
+ <ValidatedForm
+ validator={unitOfMeasureValidator}
+ method="post"
+ action={isEditing ? path.to.uom(initialValues.id!) : path.to.newUom}
+ defaultValues={initialValues}
+ fetcher={fetcher}
+ className="flex flex-col h-full"
+ onSubmit={() => {
+ if (type === "modal") {
+ onClose();
+ }
+ }}
+ >
+ <ModalDrawerHeader>
+ <ModalDrawerTitle>
+ {isEditing ? "Edit" : "New"} Unit of Measure
+ </ModalDrawerTitle>
+ </ModalDrawerHeader>
+ <ModalDrawerBody>
+ <Hidden name="id" />
+ <Hidden name="type" value={type} />
+ <VStack spacing={4}>
+ <Input name="name" label="Unit of Measure" />
+ <Input
+ name="code"
+ label="Code"
+ helperText="Unique, uppercase, without spaces"
+ />
+ </VStack>
+ </ModalDrawerBody>
+ <ModalDrawerFooter>
+ <HStack>
+ <Submit isDisabled={isDisabled}>Save</Submit>
+ <Button size="md" variant="solid" onClick={() => onClose()}>
+ Cancel
+ </Button>
+ </HStack>
+ </ModalDrawerFooter>
+ </ValidatedForm>
+ </ModalDrawerContent>
+ </ModalDrawer>
+ </ModalDrawerProvider>
);
};
diff --git a/apps/carbon/app/routes/api+/parts.uoms.ts b/apps/carbon/app/routes/api+/parts.uoms.ts
new file mode 100644
index 0000000000..4f9bf335c9
--- /dev/null
+++ b/apps/carbon/app/routes/api+/parts.uoms.ts
@@ -0,0 +1,10 @@
+import type { LoaderFunctionArgs } from "@remix-run/node";
+import { json } from "@remix-run/node";
+import { getUnitOfMeasuresList } from "~/modules/parts";
+import { requirePermissions } from "~/services/auth";
+
+export async function loader({ request }: LoaderFunctionArgs) {
+ const authorized = await requirePermissions(request, {});
+
+ return json(await getUnitOfMeasuresList(authorized.client));
+}
diff --git a/apps/carbon/app/routes/x+/part+/_layout.tsx b/apps/carbon/app/routes/x+/part+/_layout.tsx
index 9b98b9ba05..504a48f0c2 100644
--- a/apps/carbon/app/routes/x+/part+/_layout.tsx
+++ b/apps/carbon/app/routes/x+/part+/_layout.tsx
@@ -1,7 +1,7 @@
import { VStack } from "@carbon/react";
import type { LoaderFunctionArgs, MetaFunction } from "@remix-run/node";
import { Outlet } from "@remix-run/react";
-import { getPartGroupsList, getUnitOfMeasuresList } from "~/modules/parts";
+import { getUnitOfMeasuresList } from "~/modules/parts";
import { getLocationsList } from "~/modules/resources";
import { requirePermissions } from "~/services/auth";
import type { Handle } from "~/utils/handle";
@@ -22,15 +22,13 @@ export async function loader({ request }: LoaderFunctionArgs) {
view: "parts",
});
- const [partGroups, unitOfMeasures, locations] = await Promise.all([
- getPartGroupsList(client),
+ const [unitOfMeasures, locations] = await Promise.all([
getUnitOfMeasuresList(client),
getLocationsList(client),
]);
return {
locations: locations?.data ?? [],
- partGroups: partGroups?.data ?? [],
unitOfMeasures: unitOfMeasures?.data ?? [],
};
}
diff --git a/apps/carbon/app/routes/x+/parts+/uom.$uomId.tsx b/apps/carbon/app/routes/x+/parts+/uom.$uomId.tsx
index eac3785035..9ea2b55912 100644
--- a/apps/carbon/app/routes/x+/parts+/uom.$uomId.tsx
+++ b/apps/carbon/app/routes/x+/parts+/uom.$uomId.tsx
@@ -1,7 +1,7 @@
import { validationError, validator } from "@carbon/remix-validated-form";
import type { ActionFunctionArgs, LoaderFunctionArgs } from "@remix-run/node";
import { json, redirect } from "@remix-run/node";
-import { useLoaderData } from "@remix-run/react";
+import { useLoaderData, useNavigate } from "@remix-run/react";
import {
UnitOfMeasureForm,
getUnitOfMeasure,
@@ -72,6 +72,7 @@ export async function action({ request }: ActionFunctionArgs) {
export default function EditUnitOfMeasuresRoute() {
const { unitOfMeasure } = useLoaderData<typeof loader>();
+ const navigate = useNavigate();
const initialValues = {
id: unitOfMeasure?.id ?? undefined,
@@ -80,6 +81,10 @@ export default function EditUnitOfMeasuresRoute() {
};
return (
- <UnitOfMeasureForm key={initialValues.id} initialValues={initialValues} />
+ <UnitOfMeasureForm
+ key={initialValues.id}
+ initialValues={initialValues}
+ onClose={() => navigate(-1)}
+ />
);
}
diff --git a/apps/carbon/app/routes/x+/parts+/uom.new.tsx b/apps/carbon/app/routes/x+/parts+/uom.new.tsx
index e90ef50ddf..cd91de9cbb 100644
--- a/apps/carbon/app/routes/x+/parts+/uom.new.tsx
+++ b/apps/carbon/app/routes/x+/parts+/uom.new.tsx
@@ -1,6 +1,7 @@
import { validationError, validator } from "@carbon/remix-validated-form";
import type { ActionFunctionArgs, LoaderFunctionArgs } from "@remix-run/node";
import { json, redirect } from "@remix-run/node";
+import { useNavigate } from "@remix-run/react";
import {
UnitOfMeasureForm,
unitOfMeasureValidator,
@@ -26,9 +27,10 @@ export async function action({ request }: ActionFunctionArgs) {
create: "parts",
});
- const validation = await validator(unitOfMeasureValidator).validate(
- await request.formData()
- );
+ const formData = await request.formData();
+ const modal = formData.get("type") === "modal";
+
+ const validation = await validator(unitOfMeasureValidator).validate(formData);
if (validation.error) {
return validationError(validation.error);
@@ -62,17 +64,25 @@ export async function action({ request }: ActionFunctionArgs) {
);
}
- return redirect(
- path.to.uoms,
- await flash(request, success("Unit of measure created"))
- );
+ return modal
+ ? json(insertUnitOfMeasure, { status: 201 })
+ : redirect(
+ path.to.uoms,
+ await flash(request, success("Unit of measure created"))
+ );
}
export default function NewUnitOfMeasuresRoute() {
+ const navigate = useNavigate();
const initialValues = {
name: "",
code: "",
};
- return <UnitOfMeasureForm initialValues={initialValues} />;
+ return (
+ <UnitOfMeasureForm
+ onClose={() => navigate(-1)}
+ initialValues={initialValues}
+ />
+ );
}
diff --git a/apps/carbon/app/utils/path.ts b/apps/carbon/app/utils/path.ts
index 80b09ac3e7..cac1255a41 100644
--- a/apps/carbon/app/utils/path.ts
+++ b/apps/carbon/app/utils/path.ts
@@ -41,6 +41,7 @@ export const path = {
generatePath(`${api}/purchasing/supplier-contacts?supplierId=${id}`),
supplierLocations: (id: string) =>
generatePath(`${api}/purchasing/supplier-locations?supplierId=${id}`),
+ unitOfMeasures: `${api}/parts/uoms`,
workCells: (id: string) =>
generatePath(`${api}/resources/work-cells?location=${id}`),
workCellTypes: `${api}/resources/work-cell-types`,