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: add copy functionality to new procedure modal

Brad Barbindc3490d2dd41
diff --git a/apps/erp/app/modules/production/production.models.ts b/apps/erp/app/modules/production/production.models.ts
index 4bc62deda0..c091277de0 100644
--- a/apps/erp/app/modules/production/production.models.ts
+++ b/apps/erp/app/modules/production/production.models.ts
@@ -413,6 +413,7 @@ export const procedureValidator = z.object({
name: z.string().min(1, { message: "Name is required" }),
version: zfd.numeric(z.number().min(0)),
processId: zfd.text(z.string().optional()),
+ content: zfd.text(z.string().optional()),
});
export const productionEventValidator = z
diff --git a/apps/erp/app/modules/production/ui/Procedures/ProcedureForm.tsx b/apps/erp/app/modules/production/ui/Procedures/ProcedureForm.tsx
index 4aadbb244f..d274d94b7e 100644
--- a/apps/erp/app/modules/production/ui/Procedures/ProcedureForm.tsx
+++ b/apps/erp/app/modules/production/ui/Procedures/ProcedureForm.tsx
@@ -20,12 +20,14 @@ import { procedureValidator } from "../../production.models";
type ProcedureFormProps = {
initialValues: z.infer<typeof procedureValidator>;
+ type: "new" | "copy";
open?: boolean;
onClose: (data?: { id: string; name: string }) => void;
};
const ProcedureForm = ({
initialValues,
+ type = "new",
open = true,
onClose,
}: ProcedureFormProps) => {
@@ -60,25 +62,43 @@ const ProcedureForm = ({
>
<ModalDrawerHeader>
<ModalDrawerTitle>
- {isEditing ? "Edit" : "New"} Procedure
+ {type === "copy" ? "Copy" : "New"} Procedure
</ModalDrawerTitle>
</ModalDrawerHeader>
<ModalDrawerBody>
<Hidden name="id" />
+ {type === "copy" && (
+ <>
+ <Hidden name="name" />
+ <Hidden name="processId" />
+ <Hidden name="content" />
+ </>
+ )}
<VStack spacing={4}>
- <Input name="name" label="Name" />
+ {type === "new" && <Input name="name" label="Name" />}
<Number
name="version"
- label="Version"
+ label={type === "copy" ? "New Version" : "Version"}
minValue={0}
- helperText="The version of the procedure"
+ helperText={
+ type === "copy"
+ ? "The new version number of the procedure"
+ : "The version of the new procedure"
+ }
/>
- <Process name="processId" label="Process" isOptional />
+ {type === "new" && (
+ <Process name="processId" label="Process" isOptional />
+ )}
</VStack>
</ModalDrawerBody>
<ModalDrawerFooter>
<HStack>
- <Submit isDisabled={isDisabled}>Save</Submit>
+ <Submit
+ isLoading={fetcher.state !== "idle"}
+ isDisabled={fetcher.state !== "idle" || isDisabled}
+ >
+ Save
+ </Submit>
</HStack>
</ModalDrawerFooter>
</ValidatedForm>
diff --git a/apps/erp/app/routes/x+/procedure+/$id.tsx b/apps/erp/app/routes/x+/procedure+/$id.tsx
index d2b6c73df9..c80271633e 100644
--- a/apps/erp/app/routes/x+/procedure+/$id.tsx
+++ b/apps/erp/app/routes/x+/procedure+/$id.tsx
@@ -1,22 +1,18 @@
import { error, useCarbon } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
-import type { JSONContent} from "@carbon/react";
+import type { JSONContent } from "@carbon/react";
import { Input, toast, useDebounce } from "@carbon/react";
-import { generateHTML , Editor } from "@carbon/react/Editor";
-import { today , getLocalTimeZone } from "@internationalized/date";
+import { generateHTML, Editor } from "@carbon/react/Editor";
+import { today, getLocalTimeZone } from "@internationalized/date";
import { Outlet, useFetcher, useParams } from "@remix-run/react";
import type { LoaderFunctionArgs } from "@vercel/remix";
import { defer, redirect } from "@vercel/remix";
import { nanoid } from "nanoid";
import { useState } from "react";
import { usePermissions, useRouteData, useUser } from "~/hooks";
-import type {
- Procedure} from "~/modules/production";
-import {
- getProcedure,
- getProcedureVersions
-} from "~/modules/production";
+import type { Procedure } from "~/modules/production";
+import { getProcedure, getProcedureVersions } from "~/modules/production";
import ProcedureHeader from "~/modules/production/ui/Procedures/ProcedureHeader";
import ProcedureProperties from "~/modules/production/ui/Procedures/ProcedureProperties";
import type { action } from "~/routes/x+/procedure+/update";
@@ -110,22 +106,17 @@ function ProcedureEditor() {
const fetcher = useFetcher<typeof action>();
- const updateProcedureName = useDebounce(
- async (name: string) => {
- const formData = new FormData();
+ const updateProcedureName = async (name: string) => {
+ const formData = new FormData();
+ formData.append("ids", id);
+ formData.append("field", "name");
+ formData.append("value", name);
- formData.append("ids", id);
- formData.append("field", "name");
-
- formData.append("value", name);
- fetcher.submit(formData, {
- method: "post",
- action: path.to.bulkUpdateProcedure,
- });
- },
- 1000,
- true
- );
+ fetcher.submit(formData, {
+ method: "post",
+ action: path.to.bulkUpdateProcedure,
+ });
+ };
const onUploadImage = async (file: File) => {
const fileType = file.name.split(".").pop();
@@ -151,10 +142,8 @@ function ProcedureEditor() {
className="md:text-3xl text-2xl font-semibold leading-none tracking-tight text-foreground"
value={procedureName}
borderless
- onChange={(e) => {
- setProcedureName(e.target.value);
- updateProcedureName(e.target.value);
- }}
+ onChange={(e) => setProcedureName(e.target.value)}
+ onBlur={(e) => updateProcedureName(e.target.value)}
/>
{permissions.can("update", "production") ? (
diff --git a/apps/erp/app/routes/x+/production+/procedures.new.tsx b/apps/erp/app/routes/x+/production+/procedures.new.tsx
index 735142f4eb..81677a12a4 100644
--- a/apps/erp/app/routes/x+/production+/procedures.new.tsx
+++ b/apps/erp/app/routes/x+/production+/procedures.new.tsx
@@ -23,7 +23,6 @@ export async function action({ request }: ActionFunctionArgs) {
const { client, companyId, userId } = await requirePermissions(request, {
create: "production",
});
-
const formData = await request.formData();
const validation = await validator(procedureValidator).validate(formData);
@@ -31,13 +30,31 @@ export async function action({ request }: ActionFunctionArgs) {
return validationError(validation.error);
}
- const { id, ...data } = validation.data;
+ const { id, content, ...data } = validation.data;
+
+ let contentJSON;
+ try {
+ contentJSON = content ? JSON.parse(content) : undefined;
+ } catch (e) {
+ return json(
+ {},
+ await flash(
+ request,
+ error(
+ "Invalid procedure content format",
+ "Failed to parse procedure content"
+ )
+ )
+ );
+ }
const insertProcedure = await upsertProcedure(client, {
...data,
+ content: contentJSON,
companyId,
createdBy: userId,
});
+
if (insertProcedure.error || !insertProcedure.data?.id) {
return json(
{},
@@ -63,6 +80,10 @@ export default function NewProcedureRoute() {
};
return (
- <ProcedureForm initialValues={initialValues} onClose={() => navigate(-1)} />
+ <ProcedureForm
+ initialValues={initialValues}
+ type="new"
+ onClose={() => navigate(-1)}
+ />
);
}