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 equipment type

Brad Barbinbe456dfcaba6
diff --git a/apps/carbon/app/components/Form/EquipmentType.tsx b/apps/carbon/app/components/Form/EquipmentType.tsx
index 4061ee7629..fe621145c9 100644
--- a/apps/carbon/app/components/Form/EquipmentType.tsx
+++ b/apps/carbon/app/components/Form/EquipmentType.tsx
@@ -1,38 +1,66 @@
-import { useMount } from "@carbon/react";
+import { useDisclosure, useMount } from "@carbon/react";
import { useFetcher } from "@remix-run/react";
-import { useMemo } from "react";
+import { useMemo, useRef, useState } from "react";
import type { getEquipmentTypesList } from "~/modules/resources";
+import { EquipmentTypeForm } from "~/modules/resources";
import { path } from "~/utils/path";
import type { ComboboxProps } from "./Combobox";
-import Combobox from "./Combobox";
+import CreatableCombobox from "./CreatableCombobox";
type EquipmentTypeSelectProps = Omit<ComboboxProps, "options">;
const EquipmentType = (props: EquipmentTypeSelectProps) => {
- const equipmentTypesFetcher =
+ const equipmentTypeFetcher =
useFetcher<Awaited<ReturnType<typeof getEquipmentTypesList>>>();
+ const newEquipmentTypeModal = useDisclosure();
+ const [created, setCreated] = useState<string>("");
+ const triggerRef = useRef<HTMLButtonElement>(null);
+
useMount(() => {
- equipmentTypesFetcher.load(path.to.api.equipmentTypes);
+ equipmentTypeFetcher.load(path.to.api.equipmentTypes);
});
const options = useMemo(
() =>
- equipmentTypesFetcher.data?.data
- ? equipmentTypesFetcher.data?.data.map((c) => ({
+ equipmentTypeFetcher.data?.data
+ ? equipmentTypeFetcher.data?.data.map((c) => ({
value: c.id,
label: c.name,
}))
: [],
- [equipmentTypesFetcher.data]
+ [equipmentTypeFetcher.data]
);
return (
- <Combobox
- options={options}
- {...props}
- label={props?.label ?? "Equipment Type"}
- />
+ <>
+ <CreatableCombobox
+ ref={triggerRef}
+ options={options}
+ {...props}
+ label={props?.label ?? "EquipmentType"}
+ onCreateOption={(option) => {
+ newEquipmentTypeModal.onOpen();
+ setCreated(option);
+ }}
+ />
+ {newEquipmentTypeModal.isOpen && (
+ <EquipmentTypeForm
+ type="modal"
+ onClose={() => {
+ setCreated("");
+ newEquipmentTypeModal.onClose();
+ triggerRef.current?.click();
+ }}
+ initialValues={{
+ name: created,
+ description: "",
+ color: "#000000",
+ setupHours: 0,
+ }}
+ />
+ )}
+ </>
);
};
diff --git a/apps/carbon/app/modules/resources/ui/Equipment/EquipmentTypeForm.tsx b/apps/carbon/app/modules/resources/ui/Equipment/EquipmentTypeForm.tsx
index e5297f9231..094a494426 100644
--- a/apps/carbon/app/modules/resources/ui/Equipment/EquipmentTypeForm.tsx
+++ b/apps/carbon/app/modules/resources/ui/Equipment/EquipmentTypeForm.tsx
@@ -1,15 +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 } from "@remix-run/react";
import type { z } from "zod";
import {
Ability,
@@ -26,68 +28,83 @@ import { path } from "~/utils/path";
type EquipmentTypeFormProps = {
initialValues: z.infer<typeof equipmentTypeValidator>;
+ type?: "modal" | "drawer";
+ open?: boolean;
onClose: () => void;
};
const EquipmentTypeForm = ({
initialValues,
+ open = true,
+ type = "drawer",
onClose,
}: EquipmentTypeFormProps) => {
const permissions = usePermissions();
+ const fetcher = useFetcher();
+
const isEditing = initialValues.id !== undefined;
const isDisabled = isEditing
? !permissions.can("update", "resources")
: !permissions.can("create", "resources");
return (
- <Drawer
- open
- onOpenChange={(open) => {
- if (!open) onClose();
- }}
- >
- <DrawerContent>
- <ValidatedForm
- validator={equipmentTypeValidator}
- method="post"
- action={
- isEditing
- ? path.to.equipmentType(initialValues.id!)
- : path.to.newEquipmentType
- }
- defaultValues={initialValues}
- className="flex flex-col h-full"
- >
- <DrawerHeader>
- <DrawerTitle>
- {isEditing ? "Edit" : "New"} Equipment Type
- </DrawerTitle>
- </DrawerHeader>
- <DrawerBody>
- <Hidden name="id" />
- <VStack spacing={4}>
- <Input name="name" label="Name" />
- <TextArea name="description" label="Description" />
- <Color name="color" label="Color" />
- <Ability
- name="requiredAbility"
- label="Required Ability"
- isClearable
- />
- <Number name="setupHours" label="Setup Hours" />
- </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={equipmentTypeValidator}
+ method="post"
+ action={
+ isEditing
+ ? path.to.equipmentType(initialValues.id!)
+ : path.to.newEquipmentType
+ }
+ defaultValues={initialValues}
+ fetcher={fetcher}
+ className="flex flex-col h-full"
+ onSubmit={() => {
+ if (type === "modal") {
+ onClose();
+ }
+ }}
+ >
+ <ModalDrawerHeader>
+ <ModalDrawerTitle>
+ {isEditing ? "Edit" : "New"} Equipment Type
+ </ModalDrawerTitle>
+ </ModalDrawerHeader>
+ <ModalDrawerBody>
+ <Hidden name="id" />
+ <Hidden name="type" value={type} />
+ <VStack spacing={4}>
+ <Input name="name" label="Name" />
+ <TextArea name="description" label="Description" />
+ <Color name="color" label="Color" />
+ <Ability
+ name="requiredAbility"
+ label="Required Ability"
+ isClearable
+ />
+ <Number name="setupHours" label="Setup Hours" />
+ </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/x+/resources+/equipment.new.tsx b/apps/carbon/app/routes/x+/resources+/equipment.new.tsx
index bb003a1dd6..1d32f2a0d4 100644
--- a/apps/carbon/app/routes/x+/resources+/equipment.new.tsx
+++ b/apps/carbon/app/routes/x+/resources+/equipment.new.tsx
@@ -1,6 +1,6 @@
import { validationError, validator } from "@carbon/remix-validated-form";
import type { ActionFunctionArgs } from "@remix-run/node";
-import { redirect } from "@remix-run/node";
+import { json, redirect } from "@remix-run/node";
import { useNavigate } from "@remix-run/react";
import {
EquipmentTypeForm,
@@ -19,9 +19,10 @@ export async function action({ request }: ActionFunctionArgs) {
update: "resources",
});
- const validation = await validator(equipmentTypeValidator).validate(
- await request.formData()
- );
+ const formData = await request.formData();
+ const modal = formData.get("type") === "modal";
+
+ const validation = await validator(equipmentTypeValidator).validate(formData);
if (validation.error) {
return validationError(validation.error);
@@ -34,16 +35,20 @@ export async function action({ request }: ActionFunctionArgs) {
createdBy: userId,
});
if (createEquipmentType.error) {
- return redirect(
- path.to.equipment,
- await flash(
- request,
- error(createEquipmentType.error, "Failed to create equipment type")
- )
- );
+ return modal
+ ? json(createEquipmentType)
+ : redirect(
+ path.to.equipment,
+ await flash(
+ request,
+ error(createEquipmentType.error, "Failed to create equipment type")
+ )
+ );
}
- return redirect(path.to.equipment);
+ return modal
+ ? json(createEquipmentType, { status: 201 })
+ : redirect(path.to.equipment);
}
export default function NewEquipmentTypeRoute() {