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 location

Brad Barbin58758bb7acd7
diff --git a/apps/carbon/app/components/Form/Location.tsx b/apps/carbon/app/components/Form/Location.tsx
index a6355cbe0d..fd15a3f355 100644
--- a/apps/carbon/app/components/Form/Location.tsx
+++ b/apps/carbon/app/components/Form/Location.tsx
@@ -1,10 +1,12 @@
-import { useMount } from "@carbon/react";
+import { useDisclosure, useMount } from "@carbon/react";
+import { getLocalTimeZone } from "@internationalized/date";
import { useFetcher } from "@remix-run/react";
-import { useMemo } from "react";
+import { useMemo, useRef, useState } from "react";
import type { getLocationsList } from "~/modules/resources";
+import { LocationForm } from "~/modules/resources";
import { path } from "~/utils/path";
import type { ComboboxProps } from "./Combobox";
-import Combobox from "./Combobox";
+import CreatableCombobox from "./CreatableCombobox";
type LocationSelectProps = Omit<ComboboxProps, "options">;
@@ -12,6 +14,10 @@ const Location = (props: LocationSelectProps) => {
const locationFetcher =
useFetcher<Awaited<ReturnType<typeof getLocationsList>>>();
+ const newLocationModal = useDisclosure();
+ const [created, setCreated] = useState<string>("");
+ const triggerRef = useRef<HTMLButtonElement>(null);
+
useMount(() => {
locationFetcher.load(path.to.api.locations);
});
@@ -27,8 +33,40 @@ const Location = (props: LocationSelectProps) => {
[locationFetcher.data]
);
+ console.log("value", props.value);
+
return (
- <Combobox options={options} {...props} label={props?.label ?? "Location"} />
+ <>
+ <CreatableCombobox
+ ref={triggerRef}
+ options={options}
+ {...props}
+ label={props?.label ?? "Location"}
+ onCreateOption={(option) => {
+ newLocationModal.onOpen();
+ setCreated(option);
+ }}
+ />
+ {newLocationModal.isOpen && (
+ <LocationForm
+ type="modal"
+ onClose={() => {
+ setCreated("");
+ newLocationModal.onClose();
+ triggerRef.current?.click();
+ }}
+ initialValues={{
+ name: created,
+ timezone: getLocalTimeZone(),
+ addressLine1: "",
+ addressLine2: "",
+ city: "",
+ state: "",
+ postalCode: "",
+ }}
+ />
+ )}
+ </>
);
};
diff --git a/apps/carbon/app/modules/resources/ui/Locations/LocationForm.tsx b/apps/carbon/app/modules/resources/ui/Locations/LocationForm.tsx
index 0ca0338121..f18b07ea57 100644
--- a/apps/carbon/app/modules/resources/ui/Locations/LocationForm.tsx
+++ b/apps/carbon/app/modules/resources/ui/Locations/LocationForm.tsx
@@ -1,16 +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 { useNavigate } from "@remix-run/react";
+import { useFetcher } from "@remix-run/react";
import type { z } from "zod";
import { Hidden, Input, Submit, Timezone } from "~/components/Form";
import { usePermissions } from "~/hooks";
@@ -19,12 +20,19 @@ import { path } from "~/utils/path";
type LocationFormProps = {
initialValues: z.infer<typeof locationValidator>;
+ type?: "modal" | "drawer";
+ open?: boolean;
+ onClose: () => void;
};
-const LocationForm = ({ initialValues }: LocationFormProps) => {
+const LocationForm = ({
+ initialValues,
+ open = true,
+ type = "drawer",
+ onClose,
+}: LocationFormProps) => {
const permissions = usePermissions();
- const navigate = useNavigate();
- const onClose = () => navigate(-1);
+ const fetcher = useFetcher();
const isEditing = initialValues.id !== undefined;
const isDisabled = isEditing
@@ -32,53 +40,64 @@ const LocationForm = ({ initialValues }: LocationFormProps) => {
: !permissions.can("create", "resources");
return (
- <Drawer
- open
- onOpenChange={(open) => {
- if (!open) onClose();
- }}
- >
- <DrawerContent>
- <ValidatedForm
- validator={locationValidator}
- method="post"
- action={
- isEditing
- ? path.to.location(initialValues.id!)
- : path.to.newLocation
- }
- defaultValues={initialValues}
- className="flex flex-col h-full"
- >
- <DrawerHeader>
- <DrawerTitle>{isEditing ? "Edit" : "New"} Location</DrawerTitle>
- </DrawerHeader>
- <DrawerBody>
- <Hidden name="id" />
- <VStack spacing={4}>
- <Input name="name" label="Location Name" />
- <Input name="addressLine1" label="Address Line 1" />
- <Input name="addressLine2" label="Address Line 2" />
- <Input name="city" label="City" />
- <Input name="state" label="State" />
- <Input name="postalCode" label="Postal Code" />
- {/* <Country name="country" label="Country" /> */}
- <Timezone name="timezone" label="Timezone" />
- {/* <Number name="latitude" label="Latitude" minValue={-90} maxValue={90} />
+ <ModalDrawerProvider type={type}>
+ <ModalDrawer
+ open={open}
+ onOpenChange={(open) => {
+ if (!open) onClose?.();
+ }}
+ >
+ <ModalDrawerContent>
+ <ValidatedForm
+ validator={locationValidator}
+ method="post"
+ action={
+ isEditing
+ ? path.to.location(initialValues.id!)
+ : path.to.newLocation
+ }
+ defaultValues={initialValues}
+ fetcher={fetcher}
+ className="flex flex-col h-full"
+ onSubmit={() => {
+ if (type === "modal") {
+ onClose();
+ }
+ }}
+ >
+ <ModalDrawerHeader>
+ <ModalDrawerTitle>
+ {isEditing ? "Edit" : "New"} Location
+ </ModalDrawerTitle>
+ </ModalDrawerHeader>
+ <ModalDrawerBody>
+ <Hidden name="id" />
+ <Hidden name="type" value={type} />
+ <VStack spacing={4}>
+ <Input name="name" label="Location Name" />
+ <Input name="addressLine1" label="Address Line 1" />
+ <Input name="addressLine2" label="Address Line 2" />
+ <Input name="city" label="City" />
+ <Input name="state" label="State" />
+ <Input name="postalCode" label="Postal Code" />
+ {/* <Country name="country" label="Country" /> */}
+ <Timezone name="timezone" label="Timezone" />
+ {/* <Number name="latitude" label="Latitude" minValue={-90} maxValue={90} />
<Number name="longitude" label="Longitude" minVale={-180} maxValue={180} /> */}
- </VStack>
- </DrawerBody>
- <DrawerFooter>
- <HStack>
- <Submit isDisabled={isDisabled}>Save</Submit>
- <Button size="md" variant="solid" onClick={onClose}>
- Cancel
- </Button>
- </HStack>
- </DrawerFooter>
- </ValidatedForm>
- </DrawerContent>
- </Drawer>
+ </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+/quote+/$id.details.tsx b/apps/carbon/app/routes/x+/quote+/$id.details.tsx
index c62fa7f546..a71c8e585a 100644
--- a/apps/carbon/app/routes/x+/quote+/$id.details.tsx
+++ b/apps/carbon/app/routes/x+/quote+/$id.details.tsx
@@ -76,6 +76,7 @@ export default function QuotationBasicRoute() {
customerContactId: quoteData?.quotation?.customerContactId ?? "",
customerLocationId: quoteData?.quotation?.customerLocationId ?? "",
customerReference: quoteData?.quotation?.customerReference ?? "",
+ locationId: quoteData?.quotation?.locationId,
quoteDate: quoteData?.quotation?.quoteDate ?? "",
expirationDate: quoteData?.quotation.expirationDate ?? undefined,
status: quoteData?.quotation?.status ?? ("Draft" as "Draft"),
diff --git a/apps/carbon/app/routes/x+/resources+/locations.$locationId.tsx b/apps/carbon/app/routes/x+/resources+/locations.$locationId.tsx
index 2484152464..908dc55b5b 100644
--- a/apps/carbon/app/routes/x+/resources+/locations.$locationId.tsx
+++ b/apps/carbon/app/routes/x+/resources+/locations.$locationId.tsx
@@ -2,7 +2,7 @@ import { validationError, validator } from "@carbon/remix-validated-form";
import { getLocalTimeZone } from "@internationalized/date";
import type { ActionFunctionArgs, LoaderFunctionArgs } from "@remix-run/node";
import { json, redirect } from "@remix-run/node";
-import { useLoaderData } from "@remix-run/react";
+import { useLoaderData, useNavigate } from "@remix-run/react";
import {
LocationForm,
getLocation,
@@ -78,6 +78,8 @@ export async function action({ request }: ActionFunctionArgs) {
export default function LocationRoute() {
const { location } = useLoaderData<typeof loader>();
+ const navigate = useNavigate();
+ const onClose = () => navigate(path.to.workCells);
const initialValues = {
id: location.id,
@@ -92,5 +94,5 @@ export default function LocationRoute() {
longitude: location.longitude ?? undefined,
};
- return <LocationForm initialValues={initialValues} />;
+ return <LocationForm initialValues={initialValues} onClose={onClose} />;
}
diff --git a/apps/carbon/app/routes/x+/resources+/locations.new.tsx b/apps/carbon/app/routes/x+/resources+/locations.new.tsx
index 4f3b5b6745..bc95c682ce 100644
--- a/apps/carbon/app/routes/x+/resources+/locations.new.tsx
+++ b/apps/carbon/app/routes/x+/resources+/locations.new.tsx
@@ -1,7 +1,8 @@
import { validationError, validator } from "@carbon/remix-validated-form";
import { getLocalTimeZone } from "@internationalized/date";
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 {
LocationForm,
locationValidator,
@@ -19,9 +20,10 @@ export async function action({ request }: ActionFunctionArgs) {
create: "resources",
});
- const validation = await validator(locationValidator).validate(
- await request.formData()
- );
+ const formData = await request.formData();
+ const modal = formData.get("type") === "modal";
+
+ const validation = await validator(locationValidator).validate(formData);
if (validation.error) {
return validationError(validation.error);
@@ -35,22 +37,29 @@ export async function action({ request }: ActionFunctionArgs) {
});
if (createLocation.error) {
- return redirect(
- path.to.locations,
- await flash(
- request,
- error(createLocation.error, "Failed to create location.")
- )
- );
+ return modal
+ ? json(createLocation)
+ : redirect(
+ path.to.locations,
+ await flash(
+ request,
+ error(createLocation.error, "Failed to create location.")
+ )
+ );
}
- return redirect(
- path.to.locations,
- await flash(request, success("Location created"))
- );
+ return modal
+ ? json(createLocation, { status: 201 })
+ : redirect(
+ path.to.locations,
+ await flash(request, success("Location created"))
+ );
}
export default function NewLocationRoute() {
+ const navigate = useNavigate();
+ const onClose = () => navigate(path.to.locations);
+
const initialValues = {
name: "",
timezone: getLocalTimeZone(),
@@ -61,5 +70,5 @@ export default function NewLocationRoute() {
postalCode: "",
};
- return <LocationForm initialValues={initialValues} />;
+ return <LocationForm initialValues={initialValues} onClose={onClose} />;
}