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 supplier location

Brad Barbin21b74c108f93
diff --git a/apps/carbon/app/components/Form/SupplierLocation.tsx b/apps/carbon/app/components/Form/SupplierLocation.tsx
index a7b8312710..f443b15ece 100644
--- a/apps/carbon/app/components/Form/SupplierLocation.tsx
+++ b/apps/carbon/app/components/Form/SupplierLocation.tsx
@@ -1,13 +1,15 @@
import { useFetcher } from "@remix-run/react";
-import { useEffect, useMemo } from "react";
-import type {
- SupplierLocation as SupplierLocationType,
- getSupplierLocations,
+import { useEffect, useMemo, useRef } from "react";
+import {
+ SupplierLocationForm,
+ type SupplierLocation as SupplierLocationType,
+ type getSupplierLocations,
} from "~/modules/purchasing";
import { path } from "~/utils/path";
+import { useDisclosure } from "@carbon/react";
import type { ComboboxProps } from "./Combobox";
-import Combobox from "./Combobox";
+import CreatableCombobox from "./CreatableCombobox";
type SupplierLocationSelectProps = Omit<
ComboboxProps,
@@ -21,6 +23,10 @@ const SupplierLocation = (props: SupplierLocationSelectProps) => {
const supplierLocationsFetcher =
useFetcher<Awaited<ReturnType<typeof getSupplierLocations>>>();
+ const newLocationModal = useDisclosure();
+ // const [created, setCreated] = useState<string>("");
+ const triggerRef = useRef<HTMLButtonElement>(null);
+
useEffect(() => {
if (props?.supplier) {
supplierLocationsFetcher.load(
@@ -50,12 +56,31 @@ const SupplierLocation = (props: SupplierLocationSelectProps) => {
};
return (
- <Combobox
- options={options}
- {...props}
- onChange={onChange}
- label={props?.label ?? "Supplier Location"}
- />
+ <>
+ <CreatableCombobox
+ ref={triggerRef}
+ options={options}
+ {...props}
+ label={props?.label ?? "Supplier Location"}
+ onChange={onChange}
+ onCreateOption={(option) => {
+ newLocationModal.onOpen();
+ // setCreated(option);
+ }}
+ />
+ {newLocationModal.isOpen && (
+ <SupplierLocationForm
+ supplierId={props.supplier!}
+ type="modal"
+ onClose={() => {
+ // setCreated("");
+ newLocationModal.onClose();
+ triggerRef.current?.click();
+ }}
+ initialValues={{}}
+ />
+ )}
+ </>
);
};
diff --git a/apps/carbon/app/modules/purchasing/ui/Supplier/SupplierLocationForm.tsx b/apps/carbon/app/modules/purchasing/ui/Supplier/SupplierLocationForm.tsx
index f5f49b2da9..32893df82b 100644
--- a/apps/carbon/app/modules/purchasing/ui/Supplier/SupplierLocationForm.tsx
+++ b/apps/carbon/app/modules/purchasing/ui/Supplier/SupplierLocationForm.tsx
@@ -11,7 +11,7 @@ import {
} from "@carbon/react";
import { ValidatedForm } from "@carbon/remix-validated-form";
-import { useFetcher, useNavigate, useParams } 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";
@@ -19,15 +19,21 @@ import { supplierLocationValidator } from "~/modules/purchasing";
import { path } from "~/utils/path";
type SupplierLocationFormProps = {
+ supplierId: string;
initialValues: z.infer<typeof supplierLocationValidator>;
+ type?: "modal" | "drawer";
+ open?: boolean;
+ onClose: () => void;
};
-const SupplierLocationForm = ({ initialValues }: SupplierLocationFormProps) => {
+const SupplierLocationForm = ({
+ supplierId,
+ initialValues,
+ open = true,
+ type = "drawer",
+ onClose,
+}: SupplierLocationFormProps) => {
const fetcher = useFetcher();
- const navigate = useNavigate();
- const { supplierId } = useParams();
-
- if (!supplierId) throw new Error("supplierId not found");
const permissions = usePermissions();
const isEditing = !!initialValues?.id;
@@ -35,13 +41,11 @@ const SupplierLocationForm = ({ initialValues }: SupplierLocationFormProps) => {
? !permissions.can("update", "purchasing")
: !permissions.can("create", "purchasing");
- const onClose = () => navigate(path.to.supplierLocations(supplierId));
-
return (
<Drawer
- open
+ open={open}
onOpenChange={(open) => {
- if (!open) onClose();
+ if (!open) onClose?.();
}}
>
<DrawerContent>
@@ -54,8 +58,12 @@ const SupplierLocationForm = ({ initialValues }: SupplierLocationFormProps) => {
: path.to.newSupplierLocation(supplierId)
}
defaultValues={initialValues}
- onSubmit={onClose}
fetcher={fetcher}
+ onSubmit={() => {
+ if (type === "modal") {
+ onClose?.();
+ }
+ }}
className="flex flex-col h-full"
>
<DrawerHeader>
@@ -63,6 +71,7 @@ const SupplierLocationForm = ({ initialValues }: SupplierLocationFormProps) => {
</DrawerHeader>
<DrawerBody>
<Hidden name="id" />
+ <Hidden name="type" value={type} />
<Hidden name="addressId" />
<VStack spacing={4}>
<Input name="addressLine1" label="Address Line 1" />
diff --git a/apps/carbon/app/modules/sales/ui/Customer/CustomerLocationForm.tsx b/apps/carbon/app/modules/sales/ui/Customer/CustomerLocationForm.tsx
index f05abc4fb2..b35388c3dd 100644
--- a/apps/carbon/app/modules/sales/ui/Customer/CustomerLocationForm.tsx
+++ b/apps/carbon/app/modules/sales/ui/Customer/CustomerLocationForm.tsx
@@ -43,7 +43,7 @@ const CustomerLocationForm = ({
return (
<Drawer
- open
+ open={open}
onOpenChange={(open) => {
if (!open) onClose?.();
}}
diff --git a/apps/carbon/app/routes/x+/supplier+/$supplierId.locations.$supplierLocationId.tsx b/apps/carbon/app/routes/x+/supplier+/$supplierId.locations.$supplierLocationId.tsx
index b9681492c4..89c68c4076 100644
--- a/apps/carbon/app/routes/x+/supplier+/$supplierId.locations.$supplierLocationId.tsx
+++ b/apps/carbon/app/routes/x+/supplier+/$supplierId.locations.$supplierLocationId.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, useParams } from "@remix-run/react";
import {
SupplierLocationForm,
getSupplierLocation,
@@ -84,6 +84,10 @@ export async function action({ request, params }: ActionFunctionArgs) {
export default function EditSupplierLocationRoute() {
const { location } = useLoaderData<typeof loader>();
+ const navigate = useNavigate();
+
+ const { supplierId } = useParams();
+ if (!supplierId) throw new Error("supplierId not found");
const initialValues = {
id: location?.id ?? undefined,
@@ -99,7 +103,9 @@ export default function EditSupplierLocationRoute() {
return (
<SupplierLocationForm
key={initialValues.id}
+ supplierId={supplierId}
initialValues={initialValues}
+ onClose={() => navigate(path.to.supplierLocations(supplierId))}
/>
);
}
diff --git a/apps/carbon/app/routes/x+/supplier+/$supplierId.locations.new.tsx b/apps/carbon/app/routes/x+/supplier+/$supplierId.locations.new.tsx
index fc0d7e646e..bafb8f5c25 100644
--- a/apps/carbon/app/routes/x+/supplier+/$supplierId.locations.new.tsx
+++ b/apps/carbon/app/routes/x+/supplier+/$supplierId.locations.new.tsx
@@ -1,6 +1,7 @@
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, useParams } from "@remix-run/react";
import {
SupplierLocationForm,
insertSupplierLocation,
@@ -18,11 +19,14 @@ export async function action({ request, params }: ActionFunctionArgs) {
create: "purchasing",
});
+ const formData = await request.formData();
+ const modal = formData.get("type") === "modal";
+
const { supplierId } = params;
if (!supplierId) throw notFound("supplierId not found");
const validation = await validator(supplierLocationValidator).validate(
- await request.formData()
+ formData
);
if (validation.error) {
@@ -36,26 +40,41 @@ export async function action({ request, params }: ActionFunctionArgs) {
address,
});
if (createSupplierLocation.error) {
- return redirect(
- path.to.supplierLocations(supplierId),
- await flash(
- request,
- error(
- createSupplierLocation.error,
- "Failed to create supplier location"
- )
- )
- );
+ return modal
+ ? json(createSupplierLocation)
+ : redirect(
+ path.to.supplierLocations(supplierId),
+ await flash(
+ request,
+ error(
+ createSupplierLocation.error,
+ "Failed to create supplier location"
+ )
+ )
+ );
}
- return redirect(
- path.to.supplierLocations(supplierId),
- await flash(request, success("Supplier location created"))
- );
+ return modal
+ ? json(createSupplierLocation, { status: 201 })
+ : redirect(
+ path.to.supplierLocations(supplierId),
+ await flash(request, success("Supplier location created"))
+ );
}
export default function SupplierLocationsNewRoute() {
+ const navigate = useNavigate();
+
+ const { supplierId } = useParams();
+ if (!supplierId) throw new Error("supplierId not found");
+
const initialValues = {};
- return <SupplierLocationForm initialValues={initialValues} />;
+ return (
+ <SupplierLocationForm
+ initialValues={initialValues}
+ supplierId={supplierId}
+ onClose={() => navigate(path.to.supplierLocations(supplierId))}
+ />
+ );
}