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: item suppliers

Brad Barbin34dd5c83240d
diff --git a/apps/carbon/app/modules/parts/ui/Item/ItemSupplierForm.tsx b/apps/carbon/app/modules/parts/ui/Item/ItemSupplierForm.tsx
index 753d242a0f..92a97a8383 100644
--- a/apps/carbon/app/modules/parts/ui/Item/ItemSupplierForm.tsx
+++ b/apps/carbon/app/modules/parts/ui/Item/ItemSupplierForm.tsx
@@ -30,9 +30,10 @@ import { path } from "~/utils/path";
type ItemSupplierFormProps = {
initialValues: z.infer<typeof itemSupplierValidator>;
+ type: "Part" | "Service";
};
-const ItemSupplierForm = ({ initialValues }: ItemSupplierFormProps) => {
+const ItemSupplierForm = ({ initialValues, type }: ItemSupplierFormProps) => {
const permissions = usePermissions();
const navigate = useNavigate();
const { itemId } = useParams();
@@ -52,6 +53,8 @@ const ItemSupplierForm = ({ initialValues }: ItemSupplierFormProps) => {
const onClose = () => navigate(-1);
+ const action = getAction(isEditing, type, itemId, initialValues.id);
+
return (
<Drawer
open
@@ -64,11 +67,7 @@ const ItemSupplierForm = ({ initialValues }: ItemSupplierFormProps) => {
defaultValues={initialValues}
validator={itemSupplierValidator}
method="post"
- action={
- isEditing
- ? path.to.partSupplier(itemId, initialValues.id!)
- : path.to.newPartSupplier(itemId)
- }
+ action={action}
className="flex flex-col h-full"
>
<DrawerHeader>
@@ -122,3 +121,26 @@ const ItemSupplierForm = ({ initialValues }: ItemSupplierFormProps) => {
};
export default ItemSupplierForm;
+
+function getAction(
+ isEditing: boolean,
+ type: "Part" | "Service",
+ itemId: string,
+ id?: string
+) {
+ if (type === "Part") {
+ if (isEditing) {
+ return path.to.partSupplier(itemId, id!);
+ } else {
+ return path.to.newPartSupplier(itemId);
+ }
+ }
+ if (type === "Service") {
+ if (isEditing) {
+ return path.to.serviceSupplier(itemId, id!);
+ } else {
+ return path.to.newServiceSupplier(itemId);
+ }
+ }
+ throw new Error("Invalid type");
+}
diff --git a/apps/carbon/app/routes/x+/part+/$itemId.suppliers.new.tsx b/apps/carbon/app/routes/x+/part+/$itemId.suppliers.new.tsx
index f62a198d64..c6edf5b0d8 100644
--- a/apps/carbon/app/routes/x+/part+/$itemId.suppliers.new.tsx
+++ b/apps/carbon/app/routes/x+/part+/$itemId.suppliers.new.tsx
@@ -67,5 +67,5 @@ export default function NewPartSupplierRoute() {
conversionFactor: 1,
};
- return <ItemSupplierForm initialValues={initialValues} />;
+ return <ItemSupplierForm type="Part" initialValues={initialValues} />;
}
diff --git a/apps/carbon/app/routes/x+/part+/$itemId.suppliers.tsx b/apps/carbon/app/routes/x+/part+/$itemId.suppliers.tsx
index e062888bbe..6249b2ea8a 100644
--- a/apps/carbon/app/routes/x+/part+/$itemId.suppliers.tsx
+++ b/apps/carbon/app/routes/x+/part+/$itemId.suppliers.tsx
@@ -1,7 +1,7 @@
import type { LoaderFunctionArgs } from "@remix-run/node";
import { json, redirect } from "@remix-run/node";
import { useLoaderData } from "@remix-run/react";
-import { PartSuppliers, getItemSuppliers } from "~/modules/parts";
+import { ItemSuppliers, getItemSuppliers } from "~/modules/parts";
import { requirePermissions } from "~/services/auth/auth.server";
import { flash } from "~/services/session.server";
import { path } from "~/utils/path";
@@ -15,27 +15,27 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
const { itemId } = params;
if (!itemId) throw new Error("Could not find itemId");
- const [partSuppliers] = await Promise.all([
+ const [suppliers] = await Promise.all([
getItemSuppliers(client, itemId, companyId),
]);
- if (partSuppliers.error) {
+ if (suppliers.error) {
throw redirect(
path.to.part(itemId),
await flash(
request,
- error(partSuppliers.error, "Failed to load part suppliers")
+ error(suppliers.error, "Failed to load part suppliers")
)
);
}
return json({
- partSuppliers: partSuppliers.data ?? [],
+ suppliers: suppliers.data ?? [],
});
}
export default function PartSuppliersRoute() {
- const { partSuppliers } = useLoaderData<typeof loader>();
+ const { suppliers } = useLoaderData<typeof loader>();
- return <PartSuppliers partSuppliers={partSuppliers} />;
+ return <ItemSuppliers suppliers={suppliers} />;
}
diff --git a/apps/carbon/app/routes/x+/service+/$itemId.suppliers.new.tsx b/apps/carbon/app/routes/x+/service+/$itemId.suppliers.new.tsx
index b7edc65d0a..d1dda326f8 100644
--- a/apps/carbon/app/routes/x+/service+/$itemId.suppliers.new.tsx
+++ b/apps/carbon/app/routes/x+/service+/$itemId.suppliers.new.tsx
@@ -32,19 +32,19 @@ export async function action({ request, params }: ActionFunctionArgs) {
const { id, ...data } = validation.data;
- const createItemSupplier = await upsertItemSupplier(client, {
+ const createPartSupplier = await upsertItemSupplier(client, {
...data,
companyId,
createdBy: userId,
customFields: setCustomFields(formData),
});
- if (createItemSupplier.error) {
+ if (createPartSupplier.error) {
throw redirect(
path.to.serviceSuppliers(itemId),
await flash(
request,
- error(createItemSupplier.error, "Failed to create service supplier.")
+ error(createPartSupplier.error, "Failed to create service supplier")
)
);
}
@@ -52,7 +52,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
throw redirect(path.to.serviceSuppliers(itemId));
}
-export default function NewItemSupplierRoute() {
+export default function NewPartSupplierRoute() {
const { itemId } = useParams();
if (!itemId) throw new Error("itemId not found");
@@ -67,5 +67,5 @@ export default function NewItemSupplierRoute() {
conversionFactor: 1,
};
- return <ItemSupplierForm initialValues={initialValues} />;
+ return <ItemSupplierForm type="Service" initialValues={initialValues} />;
}