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 customer types

Brad Barbin60b068f461af
diff --git a/apps/carbon/app/components/Form/CustomerType.tsx b/apps/carbon/app/components/Form/CustomerType.tsx
new file mode 100644
index 0000000000..8c6bb3910d
--- /dev/null
+++ b/apps/carbon/app/components/Form/CustomerType.tsx
@@ -0,0 +1,80 @@
+import { useDisclosure, useMount } from "@carbon/react";
+import { useFetcher } from "@remix-run/react";
+import { useMemo, useRef, useState } from "react";
+import { useRouteData } from "~/hooks";
+import type { SupplierStatus } from "~/modules/purchasing";
+import type { getCustomerTypesList } from "~/modules/sales";
+import { CustomerTypeForm } from "~/modules/sales";
+import { path } from "~/utils/path";
+import type { ComboboxProps } from "./Combobox";
+import CreatableCombobox from "./CreatableCombobox";
+
+type CustomerTypeSelectProps = Omit<ComboboxProps, "options">;
+
+const CustomerType = (props: CustomerTypeSelectProps) => {
+ const customerTypeFetcher =
+ useFetcher<Awaited<ReturnType<typeof getCustomerTypesList>>>();
+
+ const sharedCustomerData = useRouteData<{
+ customerTypes: SupplierStatus[];
+ }>(path.to.customerRoot);
+
+ const newCustomerTypeModal = useDisclosure();
+ const [created, setCreated] = useState<string>("");
+ const triggerRef = useRef<HTMLButtonElement>(null);
+
+ const hasSupplierData = sharedCustomerData?.customerTypes;
+
+ useMount(() => {
+ if (!hasSupplierData) customerTypeFetcher.load(path.to.api.customerTypes);
+ });
+
+ const options = useMemo(() => {
+ const dataSource =
+ (hasSupplierData
+ ? sharedCustomerData.customerTypes
+ : customerTypeFetcher.data?.data) ?? [];
+
+ return dataSource.map((c) => ({
+ value: c.id,
+ label: c.name,
+ }));
+ }, [
+ customerTypeFetcher.data?.data,
+ hasSupplierData,
+ sharedCustomerData?.customerTypes,
+ ]);
+
+ return (
+ <>
+ <CreatableCombobox
+ ref={triggerRef}
+ options={options}
+ {...props}
+ label={props?.label ?? "CustomerType"}
+ onCreateOption={(option) => {
+ newCustomerTypeModal.onOpen();
+ setCreated(option);
+ }}
+ />
+ {newCustomerTypeModal.isOpen && (
+ <CustomerTypeForm
+ type="modal"
+ onClose={() => {
+ setCreated("");
+ newCustomerTypeModal.onClose();
+ triggerRef.current?.click();
+ }}
+ initialValues={{
+ name: created,
+ color: "#000000",
+ }}
+ />
+ )}
+ </>
+ );
+};
+
+CustomerType.displayName = "CustomerType";
+
+export default CustomerType;
diff --git a/apps/carbon/app/components/Form/SupplierType.tsx b/apps/carbon/app/components/Form/SupplierType.tsx
index 99537f7c2c..6cdce500ed 100644
--- a/apps/carbon/app/components/Form/SupplierType.tsx
+++ b/apps/carbon/app/components/Form/SupplierType.tsx
@@ -1,7 +1,11 @@
import { useDisclosure, useMount } from "@carbon/react";
import { useFetcher } from "@remix-run/react";
import { useMemo, useRef, useState } from "react";
-import type { getSupplierTypesList } from "~/modules/purchasing";
+import { useRouteData } from "~/hooks";
+import type {
+ SupplierStatus,
+ getSupplierTypesList,
+} from "~/modules/purchasing";
import { SupplierTypeForm } from "~/modules/purchasing";
import { path } from "~/utils/path";
import type { ComboboxProps } from "./Combobox";
@@ -13,24 +17,35 @@ const SupplierType = (props: SupplierTypeSelectProps) => {
const supplierTypeFetcher =
useFetcher<Awaited<ReturnType<typeof getSupplierTypesList>>>();
+ const sharedSupplierData = useRouteData<{
+ supplierTypes: SupplierStatus[];
+ }>(path.to.supplierRoot);
+
const newSupplierTypeModal = useDisclosure();
const [created, setCreated] = useState<string>("");
const triggerRef = useRef<HTMLButtonElement>(null);
+ const hasSupplierData = sharedSupplierData?.supplierTypes;
+
useMount(() => {
- supplierTypeFetcher.load(path.to.api.supplierTypes);
+ if (!hasSupplierData) supplierTypeFetcher.load(path.to.api.supplierTypes);
});
- const options = useMemo(
- () =>
- supplierTypeFetcher.data?.data
- ? supplierTypeFetcher.data?.data.map((c) => ({
- value: c.id,
- label: c.name,
- }))
- : [],
- [supplierTypeFetcher.data]
- );
+ const options = useMemo(() => {
+ const dataSource =
+ (hasSupplierData
+ ? sharedSupplierData.supplierTypes
+ : supplierTypeFetcher.data?.data) ?? [];
+
+ return dataSource.map((c) => ({
+ value: c.id,
+ label: c.name,
+ }));
+ }, [
+ supplierTypeFetcher.data?.data,
+ hasSupplierData,
+ sharedSupplierData?.supplierTypes,
+ ]);
return (
<>
diff --git a/apps/carbon/app/components/Form/index.ts b/apps/carbon/app/components/Form/index.ts
index a55145d7ad..235a5a981e 100644
--- a/apps/carbon/app/components/Form/index.ts
+++ b/apps/carbon/app/components/Form/index.ts
@@ -13,6 +13,7 @@ import Currency from "./Currency";
import Customer from "./Customer";
import CustomerContact from "./CustomerContact";
import CustomerLocation from "./CustomerLocation";
+import CustomerType from "./CustomerType";
import DatePicker from "./DatePicker";
import Department from "./Department";
import Employee from "./Employee";
@@ -65,6 +66,7 @@ export {
Customer,
CustomerContact,
CustomerLocation,
+ CustomerType,
DatePicker,
Department,
Employee,
diff --git a/apps/carbon/app/modules/purchasing/ui/SupplierTypes/SupplierTypeForm.tsx b/apps/carbon/app/modules/purchasing/ui/SupplierTypes/SupplierTypeForm.tsx
index 225a4cde33..4ea8e59cfa 100644
--- a/apps/carbon/app/modules/purchasing/ui/SupplierTypes/SupplierTypeForm.tsx
+++ b/apps/carbon/app/modules/purchasing/ui/SupplierTypes/SupplierTypeForm.tsx
@@ -18,19 +18,19 @@ import { usePermissions } from "~/hooks";
import { supplierTypeValidator } from "~/modules/purchasing";
import { path } from "~/utils/path";
-type DepartmentFormProps = {
+type SupplierTypeFormProps = {
initialValues: z.infer<typeof supplierTypeValidator>;
type?: "modal" | "drawer";
open?: boolean;
onClose: () => void;
};
-const DepartmentForm = ({
+const SupplierTypeForm = ({
initialValues,
open = true,
type = "drawer",
onClose,
-}: DepartmentFormProps) => {
+}: SupplierTypeFormProps) => {
const permissions = usePermissions();
const fetcher = useFetcher();
@@ -93,4 +93,4 @@ const DepartmentForm = ({
);
};
-export default DepartmentForm;
+export default SupplierTypeForm;
diff --git a/apps/carbon/app/modules/sales/ui/Customer/CustomerForm.tsx b/apps/carbon/app/modules/sales/ui/Customer/CustomerForm.tsx
index 723c43942b..bcc65e3979 100644
--- a/apps/carbon/app/modules/sales/ui/Customer/CustomerForm.tsx
+++ b/apps/carbon/app/modules/sales/ui/Customer/CustomerForm.tsx
@@ -15,7 +15,7 @@ import { ValidatedForm } from "@carbon/remix-validated-form";
import { useFetcher } from "@remix-run/react";
import type { z } from "zod";
import {
- Combobox,
+ CustomerType,
Employee,
Hidden,
Input,
@@ -23,7 +23,7 @@ import {
Submit,
} from "~/components/Form";
import { usePermissions, useRouteData } from "~/hooks";
-import type { CustomerStatus, CustomerType } from "~/modules/sales";
+import type { CustomerStatus } from "~/modules/sales";
import { customerValidator } from "~/modules/sales";
import { path } from "~/utils/path";
@@ -42,16 +42,9 @@ const CustomerForm = ({
const fetcher = useFetcher();
const routeData = useRouteData<{
- customerTypes: CustomerType[];
customerStatuses: CustomerStatus[];
}>(path.to.customerRoot);
- const customerTypeOptions =
- routeData?.customerTypes?.map((type) => ({
- value: type.id,
- label: type.name,
- })) ?? [];
-
const customerStatusOptions =
routeData?.customerStatuses?.map((status) => ({
value: status.id,
@@ -100,10 +93,9 @@ const CustomerForm = ({
<Input name="taxId" label="Tax ID" />
</VStack>
<VStack>
- <Combobox
+ <CustomerType
name="customerTypeId"
label="Customer Type"
- options={customerTypeOptions}
placeholder="Select Customer Type"
/>
<Select
diff --git a/apps/carbon/app/modules/sales/ui/CustomerTypes/CustomerTypeForm.tsx b/apps/carbon/app/modules/sales/ui/CustomerTypes/CustomerTypeForm.tsx
index 89694524da..9d796faaaa 100644
--- a/apps/carbon/app/modules/sales/ui/CustomerTypes/CustomerTypeForm.tsx
+++ b/apps/carbon/app/modules/sales/ui/CustomerTypes/CustomerTypeForm.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 { useFetcher, useNavigate } from "@remix-run/react";
+import { useFetcher } from "@remix-run/react";
import type { z } from "zod";
import { Color, Hidden, Input, Submit } from "~/components/Form";
import { usePermissions } from "~/hooks";
@@ -19,62 +20,76 @@ import { path } from "~/utils/path";
type CustomerTypeFormProps = {
initialValues: z.infer<typeof customerTypeValidator>;
+ type?: "modal" | "drawer";
+ open?: boolean;
+ onClose: () => void;
};
-const CustomerTypeForm = ({ initialValues }: CustomerTypeFormProps) => {
- const fetcher = useFetcher();
+const CustomerTypeForm = ({
+ initialValues,
+ open = true,
+ type = "drawer",
+ onClose,
+}: CustomerTypeFormProps) => {
const permissions = usePermissions();
- const navigate = useNavigate();
- const onClose = () => navigate(-1);
+ const fetcher = useFetcher();
const isEditing = initialValues.id !== undefined;
const isDisabled = isEditing
- ? !permissions.can("update", "sales")
- : !permissions.can("create", "sales");
+ ? !permissions.can("update", "purchasing")
+ : !permissions.can("create", "purchasing");
return (
- <Drawer
- open
- onOpenChange={(open) => {
- if (!open) onClose();
- }}
- >
- <DrawerContent>
- <ValidatedForm
- validator={customerTypeValidator}
- method="post"
- action={
- isEditing
- ? path.to.customerType(initialValues.id!)
- : path.to.newCustomerType
- }
- defaultValues={initialValues}
- fetcher={fetcher}
- className="flex flex-col h-full"
- >
- <DrawerHeader>
- <DrawerTitle>
- {isEditing ? "Edit" : "New"} Customer Type
- </DrawerTitle>
- </DrawerHeader>
- <DrawerBody>
- <Hidden name="id" />
- <VStack spacing={4}>
- <Input name="name" label="Customer Type" />
- <Color name="color" label="Color" />
- </VStack>
- </DrawerBody>
- <DrawerFooter>
- <HStack>
- <Submit isDisabled={isDisabled}>Save</Submit>
- <Button size="md" variant="solid" onClick={onClose}>
- Cancel
- </Button>
- </HStack>
- </DrawerFooter>
- </ValidatedForm>
- </DrawerContent>
- </Drawer>
+ <ModalDrawerProvider type={type}>
+ <ModalDrawer
+ open={open}
+ onOpenChange={(open) => {
+ if (!open) onClose?.();
+ }}
+ >
+ <ModalDrawerContent>
+ <ValidatedForm
+ validator={customerTypeValidator}
+ method="post"
+ action={
+ isEditing
+ ? path.to.customerType(initialValues.id!)
+ : path.to.newCustomerType
+ }
+ defaultValues={initialValues}
+ fetcher={fetcher}
+ className="flex flex-col h-full"
+ onSubmit={() => {
+ if (type === "modal") {
+ onClose();
+ }
+ }}
+ >
+ <ModalDrawerHeader>
+ <ModalDrawerTitle>
+ {isEditing ? "Edit" : "New"} Customer Type
+ </ModalDrawerTitle>
+ </ModalDrawerHeader>
+ <ModalDrawerBody>
+ <Hidden name="id" />
+ <Hidden name="type" value={type} />
+ <VStack spacing={4}>
+ <Input name="name" label="Customer Type" />
+ <Color name="color" label="Color" />
+ </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/api+/sales.customer-types.ts b/apps/carbon/app/routes/api+/sales.customer-types.ts
new file mode 100644
index 0000000000..232ba74dc6
--- /dev/null
+++ b/apps/carbon/app/routes/api+/sales.customer-types.ts
@@ -0,0 +1,12 @@
+import type { LoaderFunctionArgs } from "@remix-run/node";
+import { json } from "@remix-run/node";
+import { getCustomerTypesList } from "~/modules/sales";
+import { requirePermissions } from "~/services/auth";
+
+export async function loader({ request }: LoaderFunctionArgs) {
+ const authorized = await requirePermissions(request, {
+ view: "purchasing",
+ });
+
+ return json(await getCustomerTypesList(authorized.client));
+}
diff --git a/apps/carbon/app/routes/x+/sales+/customer-types.$customerTypeId.tsx b/apps/carbon/app/routes/x+/sales+/customer-types.$customerTypeId.tsx
index 81e0880b61..08d9f2c51f 100644
--- a/apps/carbon/app/routes/x+/sales+/customer-types.$customerTypeId.tsx
+++ b/apps/carbon/app/routes/x+/sales+/customer-types.$customerTypeId.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 } from "@remix-run/react";
import {
CustomerTypeForm,
customerTypeValidator,
@@ -78,6 +78,7 @@ export async function action({ request }: ActionFunctionArgs) {
export default function EditCustomerTypesRoute() {
const { customerType } = useLoaderData<typeof loader>();
+ const navigate = useNavigate();
const initialValues = {
id: customerType?.id ?? undefined,
@@ -86,6 +87,10 @@ export default function EditCustomerTypesRoute() {
};
return (
- <CustomerTypeForm key={initialValues.id} initialValues={initialValues} />
+ <CustomerTypeForm
+ key={initialValues.id}
+ initialValues={initialValues}
+ onClose={() => navigate(-1)}
+ />
);
}
diff --git a/apps/carbon/app/routes/x+/sales+/customer-types.new.tsx b/apps/carbon/app/routes/x+/sales+/customer-types.new.tsx
index c08bf5377c..93e98043c7 100644
--- a/apps/carbon/app/routes/x+/sales+/customer-types.new.tsx
+++ b/apps/carbon/app/routes/x+/sales+/customer-types.new.tsx
@@ -1,6 +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 { useNavigate } from "@remix-run/react";
import {
CustomerTypeForm,
customerTypeValidator,
@@ -9,7 +10,7 @@ import {
import { requirePermissions } from "~/services/auth";
import { flash } from "~/services/session.server";
import { assertIsPost } from "~/utils/http";
-import { path } from "~/utils/path";
+import { path, requestReferrer } from "~/utils/path";
import { error, success } from "~/utils/result";
export async function loader({ request }: LoaderFunctionArgs) {
@@ -26,9 +27,10 @@ export async function action({ request }: ActionFunctionArgs) {
create: "sales",
});
- const validation = await validator(customerTypeValidator).validate(
- await request.formData()
- );
+ const formData = await request.formData();
+ const modal = formData.get("type") === "modal";
+
+ const validation = await validator(customerTypeValidator).validate(formData);
if (validation.error) {
return validationError(validation.error);
@@ -41,26 +43,36 @@ export async function action({ request }: ActionFunctionArgs) {
createdBy: userId,
});
if (insertCustomerType.error) {
- return json(
- {},
- await flash(
- request,
- error(insertCustomerType.error, "Failed to insert customer type")
- )
- );
+ return modal
+ ? json(insertCustomerType)
+ : redirect(
+ requestReferrer(request) ?? path.to.customerTypes,
+ await flash(
+ request,
+ error(insertCustomerType.error, "Failed to insert customer type")
+ )
+ );
}
- return redirect(
- path.to.customerTypes,
- await flash(request, success("Customer type created"))
- );
+ return modal
+ ? json(insertCustomerType, { status: 201 })
+ : redirect(
+ path.to.customerTypes,
+ await flash(request, success("Customer type created"))
+ );
}
export default function NewCustomerTypesRoute() {
+ const navigate = useNavigate();
const initialValues = {
name: "",
color: "#000000",
};
- return <CustomerTypeForm initialValues={initialValues} />;
+ return (
+ <CustomerTypeForm
+ initialValues={initialValues}
+ onClose={() => navigate(-1)}
+ />
+ );
}
diff --git a/apps/carbon/app/routes/x+/supplier+/_layout.tsx b/apps/carbon/app/routes/x+/supplier+/_layout.tsx
index 03cabb1988..ae718b2776 100644
--- a/apps/carbon/app/routes/x+/supplier+/_layout.tsx
+++ b/apps/carbon/app/routes/x+/supplier+/_layout.tsx
@@ -27,17 +27,23 @@ export async function loader({ request }: LoaderFunctionArgs) {
view: "purchasing",
});
- const [supplierStatuses, paymentTerms, shippingMethods, shippingTerms] =
- await Promise.all([
- getSupplierTypes(client),
- getSupplierStatuses(client),
- getPaymentTermsList(client),
- getShippingMethodsList(client),
- getShippingTermsList(client),
- ]);
+ const [
+ supplierTypes,
+ supplierStatuses,
+ paymentTerms,
+ shippingMethods,
+ shippingTerms,
+ ] = await Promise.all([
+ getSupplierTypes(client),
+ getSupplierStatuses(client),
+ getPaymentTermsList(client),
+ getShippingMethodsList(client),
+ getShippingTermsList(client),
+ ]);
return json({
supplierStatuses: supplierStatuses.data ?? [],
+ supplierTypes: supplierTypes.data ?? [],
paymentTerms: paymentTerms.data ?? [],
shippingMethods: shippingMethods.data ?? [],
shippingTerms: shippingTerms.data ?? [],
diff --git a/apps/carbon/app/utils/path.ts b/apps/carbon/app/utils/path.ts
index 8cfbdb3ea2..783a22dd84 100644
--- a/apps/carbon/app/utils/path.ts
+++ b/apps/carbon/app/utils/path.ts
@@ -19,6 +19,7 @@ export const path = {
generatePath(`${api}/sales/customer-contacts?customerId=${id}`),
customerLocations: (id: string) =>
generatePath(`${api}/sales/customer-locations?customerId=${id}`),
+ customerTypes: `${api}/sales/customer-types`,
departments: `${api}/resources/departments`,
employeeTypes: `${api}/users/employee-types`,
emptyPermissions: `${api}/users/empty-permissions`,