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: remove the need for separate routes for modal and card creation

Brad Barbin030e2024cabc
diff --git a/apps/carbon/app/modules/parts/ui/Part/PartForm.tsx b/apps/carbon/app/modules/parts/ui/Part/PartForm.tsx
index d96bba0f84..9f094ad916 100644
--- a/apps/carbon/app/modules/parts/ui/Part/PartForm.tsx
+++ b/apps/carbon/app/modules/parts/ui/Part/PartForm.tsx
@@ -17,6 +17,7 @@ import type { z } from "zod";
import {
Boolean,
Combobox,
+ Hidden,
Input,
InputControlled,
Select,
@@ -124,13 +125,7 @@ const PartForm = ({ initialValues, type = "card", onClose }: PartFormProps) => {
<ModalCard onClose={onClose}>
<ModalCardContent>
<ValidatedForm
- action={
- isEditing
- ? undefined
- : type === "card"
- ? path.to.newPart
- : path.to.api.newPart
- }
+ action={isEditing ? undefined : path.to.newPart}
method="post"
validator={partValidator}
defaultValues={initialValues}
@@ -149,6 +144,7 @@ const PartForm = ({ initialValues, type = "card", onClose }: PartFormProps) => {
)}
</ModalCardHeader>
<ModalCardBody>
+ <Hidden name="type" value={type} />
<div
className={cn(
"grid w-full gap-x-8 gap-y-2",
diff --git a/apps/carbon/app/modules/purchasing/ui/Supplier/SupplierForm.tsx b/apps/carbon/app/modules/purchasing/ui/Supplier/SupplierForm.tsx
index b923789162..1b8b9670c4 100644
--- a/apps/carbon/app/modules/purchasing/ui/Supplier/SupplierForm.tsx
+++ b/apps/carbon/app/modules/purchasing/ui/Supplier/SupplierForm.tsx
@@ -62,13 +62,7 @@ const SupplierForm = ({
<ModalCardContent>
<ValidatedForm
method="post"
- action={
- isEditing
- ? undefined
- : type === "card"
- ? path.to.newSupplier
- : path.to.api.newSupplier
- }
+ action={isEditing ? undefined : path.to.newSupplier}
validator={supplierValidator}
defaultValues={initialValues}
onSubmit={onClose}
@@ -87,6 +81,7 @@ const SupplierForm = ({
</ModalCardHeader>
<ModalCardBody>
<Hidden name="id" />
+ <Hidden name="type" value={type} />
<div
className={cn(
"grid w-full gap-x-8 gap-y-2",
diff --git a/apps/carbon/app/modules/resources/ui/Departments/DepartmentForm.tsx b/apps/carbon/app/modules/resources/ui/Departments/DepartmentForm.tsx
index 23f27103b2..6f88196f69 100644
--- a/apps/carbon/app/modules/resources/ui/Departments/DepartmentForm.tsx
+++ b/apps/carbon/app/modules/resources/ui/Departments/DepartmentForm.tsx
@@ -52,8 +52,6 @@ const DepartmentForm = ({
action={
isEditing
? path.to.department(initialValues.id!)
- : type === "modal"
- ? path.to.api.newDepartment
: path.to.newDepartment
}
defaultValues={initialValues}
@@ -70,6 +68,7 @@ const DepartmentForm = ({
</DrawerHeader>
<DrawerBody>
<Hidden name="id" />
+ <Hidden name="type" value={type} />
<VStack spacing={4}>
<Input name="name" label="Department Name" />
<Color name="color" label="Color" />
diff --git a/apps/carbon/app/modules/sales/ui/Customer/CustomerForm.tsx b/apps/carbon/app/modules/sales/ui/Customer/CustomerForm.tsx
index 97211caef8..723c43942b 100644
--- a/apps/carbon/app/modules/sales/ui/Customer/CustomerForm.tsx
+++ b/apps/carbon/app/modules/sales/ui/Customer/CustomerForm.tsx
@@ -69,13 +69,7 @@ const CustomerForm = ({
<ModalCardContent>
<ValidatedForm
method="post"
- action={
- isEditing
- ? undefined
- : type === "card"
- ? path.to.newCustomer
- : path.to.api.newCustomer
- }
+ action={isEditing ? undefined : path.to.newCustomer}
validator={customerValidator}
defaultValues={initialValues}
onSubmit={onClose}
@@ -94,6 +88,7 @@ const CustomerForm = ({
</ModalCardHeader>
<ModalCardBody>
<Hidden name="id" />
+ <Hidden name="type" value={type} />
<div
className={cn(
"grid w-full gap-x-8 gap-y-2",
diff --git a/apps/carbon/app/routes/api+/parts.new.ts b/apps/carbon/app/routes/api+/parts.new.ts
deleted file mode 100644
index 6ea85c0db2..0000000000
--- a/apps/carbon/app/routes/api+/parts.new.ts
+++ /dev/null
@@ -1,40 +0,0 @@
-import { validationError, validator } from "@carbon/remix-validated-form";
-import type { ActionFunctionArgs } from "@remix-run/node";
-import { json } from "@remix-run/node";
-import { partValidator, upsertPart } from "~/modules/parts";
-import { requirePermissions } from "~/services/auth";
-import { flash } from "~/services/session.server";
-import { assertIsPost } from "~/utils/http";
-import { error } from "~/utils/result";
-
-export async function action({ request }: ActionFunctionArgs) {
- assertIsPost(request);
- const { client, userId } = await requirePermissions(request, {
- create: "parts",
- });
-
- const validation = await validator(partValidator).validate(
- await request.formData()
- );
-
- if (validation.error) {
- return validationError(validation.error);
- }
-
- const createPart = await upsertPart(client, {
- ...validation.data,
- active: true,
- createdBy: userId,
- });
- if (createPart.error) {
- return json(
- createPart,
- await flash(request, error(createPart.error, "Failed to insert part"))
- );
- }
-
- const partId = createPart.data?.id;
- if (!partId) throw new Error("Part ID not found");
-
- return json(createPart, { status: 201 });
-}
diff --git a/apps/carbon/app/routes/api+/purchasing.suppliers.new.tsx b/apps/carbon/app/routes/api+/purchasing.suppliers.new.tsx
deleted file mode 100644
index 66b0092d06..0000000000
--- a/apps/carbon/app/routes/api+/purchasing.suppliers.new.tsx
+++ /dev/null
@@ -1,41 +0,0 @@
-import { validationError, validator } from "@carbon/remix-validated-form";
-import type { ActionFunctionArgs } from "@remix-run/node";
-import { json } from "@remix-run/node";
-import { insertSupplier, supplierValidator } from "~/modules/purchasing";
-import { requirePermissions } from "~/services/auth";
-import { flash } from "~/services/session.server";
-import { assertIsPost } from "~/utils/http";
-import { error } from "~/utils/result";
-
-export async function action({ request }: ActionFunctionArgs) {
- assertIsPost(request);
- const { client, userId } = await requirePermissions(request, {
- create: "purchasing",
- });
-
- const validation = await validator(supplierValidator).validate(
- await request.formData()
- );
-
- if (validation.error) {
- return validationError(validation.error);
- }
-
- const { id, ...data } = validation.data;
-
- const createSupplier = await insertSupplier(client, {
- ...data,
- createdBy: userId,
- });
- if (createSupplier.error) {
- return json(
- createSupplier,
- await flash(
- request,
- error(createSupplier.error, "Failed to insert supplier")
- )
- );
- }
-
- return json(createSupplier);
-}
diff --git a/apps/carbon/app/routes/api+/resources.departments.new.tsx b/apps/carbon/app/routes/api+/resources.departments.new.tsx
deleted file mode 100644
index 28c4d96273..0000000000
--- a/apps/carbon/app/routes/api+/resources.departments.new.tsx
+++ /dev/null
@@ -1,45 +0,0 @@
-import { validationError, validator } from "@carbon/remix-validated-form";
-import type { ActionFunctionArgs } from "@remix-run/node";
-import { json } from "@remix-run/node";
-import { departmentValidator, upsertDepartment } from "~/modules/resources";
-import { requirePermissions } from "~/services/auth";
-import { flash } from "~/services/session.server";
-import { assertIsPost } from "~/utils/http";
-import { error } from "~/utils/result";
-
-export async function action({ request }: ActionFunctionArgs) {
- assertIsPost(request);
- const { client, userId } = await requirePermissions(request, {
- create: "resources",
- });
-
- const validation = await validator(departmentValidator).validate(
- await request.formData()
- );
-
- if (validation.error) {
- return validationError(validation.error);
- }
-
- const { id, ...data } = validation.data;
-
- const createDepartment = await upsertDepartment(client, {
- ...data,
- createdBy: userId,
- });
-
- if (createDepartment.error) {
- return json(
- createDepartment,
- await flash(
- request,
- error(createDepartment.error, "Failed to insert department")
- )
- );
- }
-
- const departmentId = createDepartment.data?.id;
- if (!departmentId) throw new Error("Department ID not found");
-
- return json(createDepartment, { status: 201 });
-}
diff --git a/apps/carbon/app/routes/api+/sales.customers.new.ts b/apps/carbon/app/routes/api+/sales.customers.new.ts
deleted file mode 100644
index 4ff9d59a94..0000000000
--- a/apps/carbon/app/routes/api+/sales.customers.new.ts
+++ /dev/null
@@ -1,41 +0,0 @@
-import { validationError, validator } from "@carbon/remix-validated-form";
-import type { ActionFunctionArgs } from "@remix-run/node";
-import { json } from "@remix-run/node";
-import { customerValidator, insertCustomer } from "~/modules/sales";
-import { requirePermissions } from "~/services/auth";
-import { flash } from "~/services/session.server";
-import { assertIsPost } from "~/utils/http";
-import { error } from "~/utils/result";
-
-export async function action({ request }: ActionFunctionArgs) {
- assertIsPost(request);
- const { client, userId } = await requirePermissions(request, {
- create: "sales",
- });
-
- const validation = await validator(customerValidator).validate(
- await request.formData()
- );
-
- if (validation.error) {
- return validationError(validation.error);
- }
-
- const { id, ...data } = validation.data;
-
- const createCustomer = await insertCustomer(client, {
- ...data,
- createdBy: userId,
- });
- if (createCustomer.error) {
- return json(
- createCustomer,
- await flash(
- request,
- error(createCustomer.error, "Failed to insert customer")
- )
- );
- }
-
- return json(createCustomer, { status: 201 });
-}
diff --git a/apps/carbon/app/routes/x+/customer+/new.tsx b/apps/carbon/app/routes/x+/customer+/new.tsx
index 4f2fc75b62..701d35d0b7 100644
--- a/apps/carbon/app/routes/x+/customer+/new.tsx
+++ b/apps/carbon/app/routes/x+/customer+/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 {
CustomerForm,
customerValidator,
@@ -18,9 +18,10 @@ export async function action({ request }: ActionFunctionArgs) {
create: "sales",
});
- const validation = await validator(customerValidator).validate(
- await request.formData()
- );
+ const formData = await request.formData();
+ const modal = formData.get("type") === "modal";
+
+ const validation = await validator(customerValidator).validate(formData);
if (validation.error) {
return validationError(validation.error);
@@ -33,18 +34,26 @@ export async function action({ request }: ActionFunctionArgs) {
createdBy: userId,
});
if (createCustomer.error) {
- return redirect(
- path.to.customers,
- await flash(
- request,
- error(createCustomer.error, "Failed to insert customer")
- )
- );
+ return modal
+ ? json(
+ createCustomer,
+ await flash(
+ request,
+ error(createCustomer.error, "Failed to insert customer")
+ )
+ )
+ : redirect(
+ path.to.customers,
+ await flash(
+ request,
+ error(createCustomer.error, "Failed to insert customer")
+ )
+ );
}
const customerId = createCustomer.data?.id;
- return redirect(path.to.customer(customerId));
+ return modal ? json(createCustomer) : redirect(path.to.customer(customerId));
}
export default function CustomersNewRoute() {
diff --git a/apps/carbon/app/routes/x+/part+/new.tsx b/apps/carbon/app/routes/x+/part+/new.tsx
index 19726c7803..064be0dd07 100644
--- a/apps/carbon/app/routes/x+/part+/new.tsx
+++ b/apps/carbon/app/routes/x+/part+/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 { PartForm, partValidator, upsertPart } from "~/modules/parts";
import { requirePermissions } from "~/services/auth";
import { flash } from "~/services/session.server";
@@ -14,9 +14,10 @@ export async function action({ request }: ActionFunctionArgs) {
create: "parts",
});
- const validation = await validator(partValidator).validate(
- await request.formData()
- );
+ const formData = await request.formData();
+ const modal = formData.get("type") === "modal";
+
+ const validation = await validator(partValidator).validate(formData);
if (validation.error) {
return validationError(validation.error);
@@ -28,16 +29,23 @@ export async function action({ request }: ActionFunctionArgs) {
createdBy: userId,
});
if (createPart.error) {
- return redirect(
- path.to.partsSearch,
- await flash(request, error(createPart.error, "Failed to insert part"))
- );
+ return modal
+ ? json(
+ createPart,
+ await flash(request, error(createPart.error, "Failed to insert part"))
+ )
+ : redirect(
+ path.to.partsSearch,
+ await flash(request, error(createPart.error, "Failed to insert part"))
+ );
}
const partId = createPart.data?.id;
if (!partId) throw new Error("Part ID not found");
- return redirect(path.to.part(partId));
+ return modal
+ ? json(createPart, { status: 201 })
+ : redirect(path.to.part(partId));
}
export default function PartsNewRoute() {
diff --git a/apps/carbon/app/routes/x+/resources+/departments.new.tsx b/apps/carbon/app/routes/x+/resources+/departments.new.tsx
index e35575242e..1289bfb2d3 100644
--- a/apps/carbon/app/routes/x+/resources+/departments.new.tsx
+++ b/apps/carbon/app/routes/x+/resources+/departments.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 {
DepartmentForm,
@@ -19,9 +19,10 @@ export async function action({ request }: ActionFunctionArgs) {
create: "resources",
});
- const validation = await validator(departmentValidator).validate(
- await request.formData()
- );
+ const formData = await request.formData();
+ const modal = formData.get("type") === "modal";
+
+ const validation = await validator(departmentValidator).validate(formData);
if (validation.error) {
return validationError(validation.error);
@@ -35,19 +36,29 @@ export async function action({ request }: ActionFunctionArgs) {
});
if (createDepartment.error) {
- return redirect(
- path.to.departments,
- await flash(
- request,
- error(createDepartment.error, "Failed to create department.")
- )
- );
+ return modal
+ ? json(
+ createDepartment,
+ await flash(
+ request,
+ error(createDepartment.error, "Failed to insert department")
+ )
+ )
+ : redirect(
+ path.to.departments,
+ await flash(
+ request,
+ error(createDepartment.error, "Failed to create department.")
+ )
+ );
}
- return redirect(
- path.to.departments,
- await flash(request, success("Department created."))
- );
+ return modal
+ ? json(createDepartment, { status: 201 })
+ : redirect(
+ path.to.departments,
+ await flash(request, success("Department created."))
+ );
}
export default function NewDepartmentRoute() {
diff --git a/apps/carbon/app/routes/x+/supplier+/new.tsx b/apps/carbon/app/routes/x+/supplier+/new.tsx
index 3bebcf0c9d..94877d0fee 100644
--- a/apps/carbon/app/routes/x+/supplier+/new.tsx
+++ b/apps/carbon/app/routes/x+/supplier+/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 {
SupplierForm,
insertSupplier,
@@ -18,9 +18,10 @@ export async function action({ request }: ActionFunctionArgs) {
create: "purchasing",
});
- const validation = await validator(supplierValidator).validate(
- await request.formData()
- );
+ const formData = await request.formData();
+ const modal = formData.get("type") === "modal";
+
+ const validation = await validator(supplierValidator).validate(formData);
if (validation.error) {
return validationError(validation.error);
@@ -33,18 +34,28 @@ export async function action({ request }: ActionFunctionArgs) {
createdBy: userId,
});
if (createSupplier.error) {
- return redirect(
- path.to.suppliers,
- await flash(
- request,
- error(createSupplier.error, "Failed to insert supplier")
- )
- );
+ return modal
+ ? json(
+ createSupplier,
+ await flash(
+ request,
+ error(createSupplier.error, "Failed to insert supplier")
+ )
+ )
+ : redirect(
+ path.to.suppliers,
+ await flash(
+ request,
+ error(createSupplier.error, "Failed to insert supplier")
+ )
+ );
}
const supplierId = createSupplier.data?.id;
- return redirect(path.to.supplier(supplierId));
+ return modal
+ ? json(createSupplier, { status: 201 })
+ : redirect(path.to.supplier(supplierId));
}
export default function SuppliersNewRoute() {
diff --git a/apps/carbon/app/utils/path.ts b/apps/carbon/app/utils/path.ts
index 7b13c38c26..02b6e4eb9d 100644
--- a/apps/carbon/app/utils/path.ts
+++ b/apps/carbon/app/utils/path.ts
@@ -26,10 +26,6 @@ export const path = {
groupsByType: (type?: string) =>
generatePath(`${api}/users/groups?type=${type}`),
locations: `${api}/resources/locations`,
- newCustomer: `${api}/sales/customers/new`,
- newDepartment: `${api}/resources/departments/new`,
- newPart: `${api}/parts/new`,
- newSupplier: `${api}/purchasing/suppliers/new`,
rollback: (table: string, id: string) =>
generatePath(
`${api}/settings/sequence/rollback?table=${table}&currentSequence=${id}`