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 contact

Brad Barbinc7b943770765
diff --git a/apps/carbon/app/components/Form/CustomerContact.tsx b/apps/carbon/app/components/Form/CustomerContact.tsx
index 8b4227c69f..e68adefe7c 100644
--- a/apps/carbon/app/components/Form/CustomerContact.tsx
+++ b/apps/carbon/app/components/Form/CustomerContact.tsx
@@ -1,13 +1,15 @@
import { useFetcher } from "@remix-run/react";
-import { useEffect, useMemo } from "react";
+import { useEffect, useMemo, useRef, useState } from "react";
import type {
CustomerContact as CustomerContactType,
getCustomerContacts,
} from "~/modules/sales";
import { path } from "~/utils/path";
+import { useDisclosure } from "@carbon/react";
+import { CustomerContactForm } from "~/modules/sales";
import type { ComboboxProps } from "./Combobox";
-import Combobox from "./Combobox";
+import CreatableCombobox from "./CreatableCombobox";
type CustomerContactSelectProps = Omit<
ComboboxProps,
@@ -21,6 +23,12 @@ const CustomerContact = (props: CustomerContactSelectProps) => {
const customerContactsFetcher =
useFetcher<Awaited<ReturnType<typeof getCustomerContacts>>>();
+ const newContactModal = useDisclosure();
+ const [created, setCreated] = useState<string>("");
+ const triggerRef = useRef<HTMLButtonElement>(null);
+
+ const [firstName, ...lastName] = created.split(" ");
+
useEffect(() => {
if (props?.customer) {
customerContactsFetcher.load(
@@ -50,12 +58,35 @@ const CustomerContact = (props: CustomerContactSelectProps) => {
};
return (
- <Combobox
- options={options}
- {...props}
- onChange={onChange}
- label={props?.label ?? "Customer Contact"}
- />
+ <>
+ <CreatableCombobox
+ ref={triggerRef}
+ options={options}
+ {...props}
+ label={props?.label ?? "Customer Contact"}
+ onChange={onChange}
+ onCreateOption={(option) => {
+ newContactModal.onOpen();
+ setCreated(option);
+ }}
+ />
+ {newContactModal.isOpen && (
+ <CustomerContactForm
+ customerId={props.customer!}
+ type="modal"
+ onClose={() => {
+ setCreated("");
+ newContactModal.onClose();
+ triggerRef.current?.click();
+ }}
+ initialValues={{
+ email: "",
+ firstName: firstName,
+ lastName: lastName.join(" "),
+ }}
+ />
+ )}
+ </>
);
};
diff --git a/apps/carbon/app/modules/resources/ui/Departments/DepartmentForm.tsx b/apps/carbon/app/modules/resources/ui/Departments/DepartmentForm.tsx
index ffab8ac137..cb1d1a697a 100644
--- a/apps/carbon/app/modules/resources/ui/Departments/DepartmentForm.tsx
+++ b/apps/carbon/app/modules/resources/ui/Departments/DepartmentForm.tsx
@@ -85,7 +85,7 @@ const DepartmentForm = ({
<ModalDrawerFooter>
<HStack>
<Submit isDisabled={isDisabled}>Save</Submit>
- <Button size="md" variant="solid" onClick={() => onClose()}>
+ <Button size="md" variant="solid" onClick={() => onClose?.()}>
Cancel
</Button>
</HStack>
diff --git a/apps/carbon/app/modules/sales/ui/Customer/CustomerContactForm.tsx b/apps/carbon/app/modules/sales/ui/Customer/CustomerContactForm.tsx
index 5a98801153..8d30840b39 100644
--- a/apps/carbon/app/modules/sales/ui/Customer/CustomerContactForm.tsx
+++ b/apps/carbon/app/modules/sales/ui/Customer/CustomerContactForm.tsx
@@ -10,7 +10,7 @@ import {
VStack,
} 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 {
DatePicker,
@@ -25,15 +25,21 @@ import { customerContactValidator } from "~/modules/sales";
import { path } from "~/utils/path";
type CustomerContactFormProps = {
+ customerId: string;
initialValues: z.infer<typeof customerContactValidator>;
+ type?: "modal" | "drawer";
+ open?: boolean;
+ onClose: () => void;
};
-const CustomerContactForm = ({ initialValues }: CustomerContactFormProps) => {
+const CustomerContactForm = ({
+ customerId,
+ initialValues,
+ open = true,
+ type = "drawer",
+ onClose,
+}: CustomerContactFormProps) => {
const fetcher = useFetcher();
- const navigate = useNavigate();
- const { customerId } = useParams();
-
- if (!customerId) throw new Error("customerId not found");
const permissions = usePermissions();
const isEditing = !!initialValues?.id;
@@ -41,13 +47,11 @@ const CustomerContactForm = ({ initialValues }: CustomerContactFormProps) => {
? !permissions.can("update", "sales")
: !permissions.can("create", "sales");
- const onClose = () => navigate(path.to.customerContacts(customerId));
-
return (
<Drawer
- open
+ open={open}
onOpenChange={(open) => {
- if (!open) onClose();
+ if (!open) onClose?.();
}}
>
<DrawerContent>
@@ -61,14 +65,19 @@ const CustomerContactForm = ({ initialValues }: CustomerContactFormProps) => {
}
defaultValues={initialValues}
fetcher={fetcher}
- onSubmit={onClose}
className="flex flex-col h-full"
+ onSubmit={() => {
+ if (type === "modal") {
+ onClose?.();
+ }
+ }}
>
<DrawerHeader>
<DrawerTitle>{isEditing ? "Edit" : "New"} Contact</DrawerTitle>
</DrawerHeader>
<DrawerBody>
<Hidden name="id" />
+ <Hidden name="type" value={type} />
<Hidden name="contactId" />
<VStack spacing={4}>
<Input name="firstName" label="First Name" />
diff --git a/apps/carbon/app/routes/x+/customer+/$customerId.contacts.$customerContactId.tsx b/apps/carbon/app/routes/x+/customer+/$customerId.contacts.$customerContactId.tsx
index 1ca18e0c1e..0a7e704d7e 100644
--- a/apps/carbon/app/routes/x+/customer+/$customerId.contacts.$customerContactId.tsx
+++ b/apps/carbon/app/routes/x+/customer+/$customerId.contacts.$customerContactId.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 {
CustomerContactForm,
customerContactValidator,
@@ -88,6 +88,10 @@ export async function action({ request, params }: ActionFunctionArgs) {
export default function EditCustomerContactRoute() {
const { contact } = useLoaderData<typeof loader>();
+ const navigate = useNavigate();
+
+ const { customerId } = useParams();
+ if (!customerId) throw new Error("customerId not found");
const initialValues = {
id: contact?.id ?? undefined,
@@ -109,6 +113,11 @@ export default function EditCustomerContactRoute() {
};
return (
- <CustomerContactForm key={initialValues.id} initialValues={initialValues} />
+ <CustomerContactForm
+ key={initialValues.id}
+ customerId={customerId}
+ initialValues={initialValues}
+ onClose={() => navigate(path.to.customerContacts(customerId))}
+ />
);
}
diff --git a/apps/carbon/app/routes/x+/customer+/$customerId.contacts.new.tsx b/apps/carbon/app/routes/x+/customer+/$customerId.contacts.new.tsx
index 049fd2de05..9220de04d1 100644
--- a/apps/carbon/app/routes/x+/customer+/$customerId.contacts.new.tsx
+++ b/apps/carbon/app/routes/x+/customer+/$customerId.contacts.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 { getSupabaseServiceRole } from "~/lib/supabase";
import {
CustomerContactForm,
@@ -25,10 +26,15 @@ export async function action({ request, params }: ActionFunctionArgs) {
const { customerId } = params;
if (!customerId) throw notFound("customerId not found");
+ const formData = await request.formData();
+ const modal = formData.get("type") === "modal";
+
const validation = await validator(customerContactValidator).validate(
- await request.formData()
+ formData
);
+ console.log({ modal, data: validation.data });
+
if (validation.error) {
return validationError(validation.error);
}
@@ -40,27 +46,44 @@ export async function action({ request, params }: ActionFunctionArgs) {
contact,
});
if (createCustomerContact.error) {
- return redirect(
- path.to.customerContacts(customerId),
- await flash(
- request,
- error(createCustomerContact.error, "Failed to create customer contact")
- )
- );
+ return modal
+ ? json(createCustomerContact)
+ : redirect(
+ path.to.customerContacts(customerId),
+ await flash(
+ request,
+ error(
+ createCustomerContact.error,
+ "Failed to create customer contact"
+ )
+ )
+ );
}
- return redirect(
- path.to.customerContacts(customerId),
- await flash(request, success("Customer contact created"))
- );
+ return modal
+ ? json(createCustomerContact, { status: 201 })
+ : redirect(
+ path.to.customerContacts(customerId),
+ await flash(request, success("Customer contact created"))
+ );
}
export default function CustomerContactsNewRoute() {
+ const navigate = useNavigate();
+ const { customerId } = useParams();
+ if (!customerId) throw new Error("customerId not found");
+
const initialValues = {
firstName: "",
lastName: "",
email: "",
};
- return <CustomerContactForm initialValues={initialValues} />;
+ return (
+ <CustomerContactForm
+ customerId={customerId}
+ initialValues={initialValues}
+ onClose={() => navigate(path.to.customerContacts(customerId))}
+ />
+ );
}
diff --git a/apps/carbon/app/routes/x+/quote+/$id.details.tsx b/apps/carbon/app/routes/x+/quote+/$id.details.tsx
index c62fa7f546..8cd376766a 100644
--- a/apps/carbon/app/routes/x+/quote+/$id.details.tsx
+++ b/apps/carbon/app/routes/x+/quote+/$id.details.tsx
@@ -29,6 +29,8 @@ export async function action({ request, params }: ActionFunctionArgs) {
await request.formData()
);
+ console.log({ validation });
+
if (validation.error) {
return validationError(validation.error);
}
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 5df72457aa..82068bc6a6 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -7461,7 +7461,7 @@ export type Database = {
foreignKeyName: "quote_customerContactId_fkey"
columns: ["customerContactId"]
isOneToOne: false
- referencedRelation: "contact"
+ referencedRelation: "customerContact"
referencedColumns: ["id"]
},
{
@@ -7482,23 +7482,9 @@ export type Database = {
foreignKeyName: "quote_customerLocationId_fkey"
columns: ["customerLocationId"]
isOneToOne: false
- referencedRelation: "location"
+ referencedRelation: "customerLocation"
referencedColumns: ["id"]
},
- {
- foreignKeyName: "quote_customerLocationId_fkey"
- columns: ["customerLocationId"]
- isOneToOne: false
- referencedRelation: "partQuantities"
- referencedColumns: ["locationId"]
- },
- {
- foreignKeyName: "quote_customerLocationId_fkey"
- columns: ["customerLocationId"]
- isOneToOne: false
- referencedRelation: "purchaseOrders"
- referencedColumns: ["locationId"]
- },
{
foreignKeyName: "quote_locationId_fkey"
columns: ["locationId"]
@@ -12657,7 +12643,7 @@ export type Database = {
foreignKeyName: "quote_customerContactId_fkey"
columns: ["customerContactId"]
isOneToOne: false
- referencedRelation: "contact"
+ referencedRelation: "customerContact"
referencedColumns: ["id"]
},
{
@@ -12678,23 +12664,9 @@ export type Database = {
foreignKeyName: "quote_customerLocationId_fkey"
columns: ["customerLocationId"]
isOneToOne: false
- referencedRelation: "location"
+ referencedRelation: "customerLocation"
referencedColumns: ["id"]
},
- {
- foreignKeyName: "quote_customerLocationId_fkey"
- columns: ["customerLocationId"]
- isOneToOne: false
- referencedRelation: "partQuantities"
- referencedColumns: ["locationId"]
- },
- {
- foreignKeyName: "quote_customerLocationId_fkey"
- columns: ["customerLocationId"]
- isOneToOne: false
- referencedRelation: "purchaseOrders"
- referencedColumns: ["locationId"]
- },
{
foreignKeyName: "quote_locationId_fkey"
columns: ["locationId"]
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 5df72457aa..82068bc6a6 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -7461,7 +7461,7 @@ export type Database = {
foreignKeyName: "quote_customerContactId_fkey"
columns: ["customerContactId"]
isOneToOne: false
- referencedRelation: "contact"
+ referencedRelation: "customerContact"
referencedColumns: ["id"]
},
{
@@ -7482,23 +7482,9 @@ export type Database = {
foreignKeyName: "quote_customerLocationId_fkey"
columns: ["customerLocationId"]
isOneToOne: false
- referencedRelation: "location"
+ referencedRelation: "customerLocation"
referencedColumns: ["id"]
},
- {
- foreignKeyName: "quote_customerLocationId_fkey"
- columns: ["customerLocationId"]
- isOneToOne: false
- referencedRelation: "partQuantities"
- referencedColumns: ["locationId"]
- },
- {
- foreignKeyName: "quote_customerLocationId_fkey"
- columns: ["customerLocationId"]
- isOneToOne: false
- referencedRelation: "purchaseOrders"
- referencedColumns: ["locationId"]
- },
{
foreignKeyName: "quote_locationId_fkey"
columns: ["locationId"]
@@ -12657,7 +12643,7 @@ export type Database = {
foreignKeyName: "quote_customerContactId_fkey"
columns: ["customerContactId"]
isOneToOne: false
- referencedRelation: "contact"
+ referencedRelation: "customerContact"
referencedColumns: ["id"]
},
{
@@ -12678,23 +12664,9 @@ export type Database = {
foreignKeyName: "quote_customerLocationId_fkey"
columns: ["customerLocationId"]
isOneToOne: false
- referencedRelation: "location"
+ referencedRelation: "customerLocation"
referencedColumns: ["id"]
},
- {
- foreignKeyName: "quote_customerLocationId_fkey"
- columns: ["customerLocationId"]
- isOneToOne: false
- referencedRelation: "partQuantities"
- referencedColumns: ["locationId"]
- },
- {
- foreignKeyName: "quote_customerLocationId_fkey"
- columns: ["customerLocationId"]
- isOneToOne: false
- referencedRelation: "purchaseOrders"
- referencedColumns: ["locationId"]
- },
{
foreignKeyName: "quote_locationId_fkey"
columns: ["locationId"]
diff --git a/packages/database/supabase/migrations/20240206224405_quotes.sql b/packages/database/supabase/migrations/20240206224405_quotes.sql
index 609d9b8148..1d08ae8d86 100644
--- a/packages/database/supabase/migrations/20240206224405_quotes.sql
+++ b/packages/database/supabase/migrations/20240206224405_quotes.sql
@@ -35,8 +35,8 @@ CREATE TABLE "quote" (
CONSTRAINT "quote_quoteId_key" UNIQUE ("quoteId"),
CONSTRAINT "quote_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "user" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "quote_customerId_fkey" FOREIGN KEY ("customerId") REFERENCES "customer" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
- CONSTRAINT "quote_customerLocationId_fkey" FOREIGN KEY ("customerLocationId") REFERENCES "location" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
- CONSTRAINT "quote_customerContactId_fkey" FOREIGN KEY ("customerContactId") REFERENCES "contact" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
+ CONSTRAINT "quote_customerLocationId_fkey" FOREIGN KEY ("customerLocationId") REFERENCES "customerLocation" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
+ CONSTRAINT "quote_customerContactId_fkey" FOREIGN KEY ("customerContactId") REFERENCES "customerContact" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "quote_locationId_fkey" FOREIGN KEY ("locationId") REFERENCES "location" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "quote_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "quote_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
diff --git a/packages/utils/src/color.ts b/packages/utils/src/color.ts
index dade9c0d65..ff53d64d0e 100644
--- a/packages/utils/src/color.ts
+++ b/packages/utils/src/color.ts
@@ -6,7 +6,7 @@ const colors = [
{ background: "#264653", color: "#ffffff" },
{ background: "#2a9d8f", color: "#ffffff" },
{ background: "#4ff38c", color: "#000000" },
- { background: "#f4a261", color: "#ffffff" },
+ { background: "#f4a261", color: "#38210f" },
{ background: "#e76f51", color: "#ffffff" },
{ background: "#8ecae6", color: "#ffffff" },
{ background: "#219ebc", color: "#ffffff" },