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.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
feat: add modal/drawer component and inline creation for department
Brad Barbinb26714a542c2
diff --git a/apps/carbon/app/components/Form/Department.tsx b/apps/carbon/app/components/Form/Department.tsxindex ff501adbe1..5ebf286676 100644--- a/apps/carbon/app/components/Form/Department.tsx+++ b/apps/carbon/app/components/Form/Department.tsx@@ -1,10 +1,11 @@-import { useMount } from "@carbon/react";+import { useDisclosure, useMount } from "@carbon/react";import { useFetcher } from "@remix-run/react";-import { useMemo } from "react";+import { useMemo, useRef, useState } from "react";import type { getDepartmentsList } from "~/modules/resources";+import { DepartmentForm } from "~/modules/resources";import { path } from "~/utils/path";import type { ComboboxProps } from "./Combobox";-import Combobox from "./Combobox";+import CreatableCombobox from "./CreatableCombobox";type DepartmentSelectProps = Omit<ComboboxProps, "options">;@@ -12,6 +13,10 @@ const Department = (props: DepartmentSelectProps) => {const departmentFetcher =useFetcher<Awaited<ReturnType<typeof getDepartmentsList>>>();+ const newDepartmentModal = useDisclosure();+ const [createdDepartment, setCreatedDepartment] = useState<string>("");+ const triggerRef = useRef<HTMLButtonElement>(null);+useMount(() => {departmentFetcher.load(path.to.api.departments);});@@ -28,11 +33,32 @@ const Department = (props: DepartmentSelectProps) => {);return (- <Combobox- options={options}- {...props}- label={props?.label ?? "Department"}- />+ <>+ <CreatableCombobox+ ref={triggerRef}+ options={options}+ {...props}+ label={props?.label ?? "Department"}+ onCreateOption={(option) => {+ newDepartmentModal.onOpen();+ setCreatedDepartment(option);+ }}+ />+ {newDepartmentModal.isOpen && (+ <DepartmentForm+ type="modal"+ onClose={() => {+ setCreatedDepartment("");+ newDepartmentModal.onClose();+ triggerRef.current?.click();+ }}+ initialValues={{+ name: createdDepartment,+ color: "#000000",+ }}+ />+ )}+ </>);};diff --git a/apps/carbon/app/modules/accounting/accounting.service.ts b/apps/carbon/app/modules/accounting/accounting.service.tsindex add5eb64d6..66d21281cf 100644--- a/apps/carbon/app/modules/accounting/accounting.service.ts+++ b/apps/carbon/app/modules/accounting/accounting.service.ts@@ -680,7 +680,7 @@ export async function upsertAccount(})) {if ("createdBy" in account) {- return client.from("account").insert([account]).select("id").single();+ return client.from("account").insert([account]).select("*").single();}return client.from("account")@@ -761,7 +761,7 @@ export async function upsertCurrency(}if ("createdBy" in currency) {- return client.from("currency").insert([currency]).select("id").single();+ return client.from("currency").insert([currency]).select("*").single();}return client.from("currency")diff --git a/apps/carbon/app/modules/documents/documents.service.ts b/apps/carbon/app/modules/documents/documents.service.tsindex 1e9d88b315..142816a6e5 100644--- a/apps/carbon/app/modules/documents/documents.service.ts+++ b/apps/carbon/app/modules/documents/documents.service.ts@@ -198,7 +198,7 @@ export async function upsertDocument(})) {if ("createdBy" in document) {- return client.from("document").insert(document).select("id").single();+ return client.from("document").insert(document).select("*").single();}return client.from("document")diff --git a/apps/carbon/app/modules/inventory/inventory.service.ts b/apps/carbon/app/modules/inventory/inventory.service.tsindex 8287397678..9033c1e177 100644--- a/apps/carbon/app/modules/inventory/inventory.service.ts+++ b/apps/carbon/app/modules/inventory/inventory.service.ts@@ -139,7 +139,7 @@ export async function upsertReceipt(})) {if ("createdBy" in receipt) {- return client.from("receipt").insert([receipt]).select("id").single();+ return client.from("receipt").insert([receipt]).select("*").single();}return client.from("receipt")diff --git a/apps/carbon/app/modules/parts/parts.service.ts b/apps/carbon/app/modules/parts/parts.service.tsindex 889f668328..49c7d406d3 100644--- a/apps/carbon/app/modules/parts/parts.service.ts+++ b/apps/carbon/app/modules/parts/parts.service.ts@@ -387,7 +387,7 @@ export async function upsertPart(| (z.infer<typeof partValidator> & { updatedBy: string })) {if ("createdBy" in part) {- return client.from("part").insert(part).select("id").single();+ return client.from("part").insert(part).select("*").single();}return client.from("part").update(sanitize(part)).eq("id", part.id);}@@ -483,7 +483,7 @@ export async function upsertPartGroup(})) {if ("createdBy" in partGroup) {- return client.from("partGroup").insert([partGroup]).select("id").single();+ return client.from("partGroup").insert([partGroup]).select("*").single();}return (client@@ -544,7 +544,7 @@ export async function upsertService(})) {if ("createdBy" in service) {- return client.from("service").insert(service).select("id").single();+ return client.from("service").insert(service).select("*").single();}return client.from("service").update(sanitize(service)).eq("id", service.id);}diff --git a/apps/carbon/app/modules/purchasing/purchasing.service.ts b/apps/carbon/app/modules/purchasing/purchasing.service.tsindex 476b895ad4..a2eeceb5dd 100644--- a/apps/carbon/app/modules/purchasing/purchasing.service.ts+++ b/apps/carbon/app/modules/purchasing/purchasing.service.ts@@ -438,7 +438,7 @@ export async function insertSupplier(updatedBy: string;})) {- return client.from("supplier").insert([supplier]).select("id").single();+ return client.from("supplier").insert([supplier]).select("*").single();}export async function insertSupplierContact(diff --git a/apps/carbon/app/modules/resources/resources.service.ts b/apps/carbon/app/modules/resources/resources.service.tsindex 9b26a7ed7c..9ff28e6d90 100644--- a/apps/carbon/app/modules/resources/resources.service.ts+++ b/apps/carbon/app/modules/resources/resources.service.ts@@ -819,7 +819,7 @@ export async function insertAbility(createdBy: string;}) {- return client.from("ability").insert([ability]).select("id").single();+ return client.from("ability").insert([ability]).select("*").single();}export async function insertEmployeeAbilities(@@ -891,7 +891,7 @@ export async function insertEmployeeJob(locationId: string;}) {- return client.from("employeeJob").insert(job).select("id").single();+ return client.from("employeeJob").insert(job).select("*").single();}export async function updateAbility(@@ -1041,7 +1041,7 @@ export async function upsertDepartment(.update(sanitize(department)).eq("id", department.id);}- return client.from("department").insert(department).select("id").single();+ return client.from("department").insert(department).select("*").single();}export async function upsertEmployeeAbility(@@ -1107,7 +1107,7 @@ export async function upsertEquipment(return client.from("equipment").update(sanitize(update)).eq("id", id);}- return client.from("equipment").insert([equipment]).select("id").single();+ return client.from("equipment").insert([equipment]).select("*").single();}export async function upsertEquipmentType(@@ -1153,7 +1153,7 @@ export async function upsertHoliday(.update(sanitize(holiday)).eq("id", holiday.id);}- return client.from("holiday").insert(holiday).select("id").single();+ return client.from("holiday").insert(holiday).select("*").single();}export async function upsertLocation(@@ -1173,7 +1173,7 @@ export async function upsertLocation(.update(sanitize(location)).eq("id", location.id);}- return client.from("location").insert([location]).select("id").single();+ return client.from("location").insert([location]).select("*").single();}export async function upsertPartner(@@ -1219,7 +1219,7 @@ export async function upsertShift(return client.from("shift").update(sanitize(update)).eq("id", id);}- return client.from("shift").insert([update]).select("id").single();+ return client.from("shift").insert([update]).select("*").single();}export async function upsertWorkCell(@@ -1249,7 +1249,7 @@ export async function upsertWorkCell(const { id, ...update } = workCell;return client.from("workCell").update(sanitize(update)).eq("id", id);}- return client.from("workCell").insert([workCell]).select("id").single();+ return client.from("workCell").insert([workCell]).select("*").single();}export async function upsertWorkCellType(diff --git a/apps/carbon/app/modules/resources/ui/Departments/DepartmentForm.tsx b/apps/carbon/app/modules/resources/ui/Departments/DepartmentForm.tsxindex 0b92da9052..23f27103b2 100644--- a/apps/carbon/app/modules/resources/ui/Departments/DepartmentForm.tsx+++ b/apps/carbon/app/modules/resources/ui/Departments/DepartmentForm.tsx@@ -9,9 +9,8 @@ import {HStack,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 { Color, Department, Hidden, Input, Submit } from "~/components/Form";import { usePermissions } from "~/hooks";@@ -20,12 +19,19 @@ import { path } from "~/utils/path";type DepartmentFormProps = {initialValues: z.infer<typeof departmentValidator>;+ type?: "modal" | "drawer";+ open?: boolean;+ onClose: (data?: { id: string; name: string }) => void;};-const DepartmentForm = ({ initialValues }: DepartmentFormProps) => {+const DepartmentForm = ({+ initialValues,+ open = true,+ type = "drawer",+ onClose,+}: DepartmentFormProps) => {const permissions = usePermissions();- const navigate = useNavigate();- const onClose = () => navigate(-1);+ const fetcher = useFetcher();const isEditing = initialValues.id !== undefined;const isDisabled = isEditing@@ -34,9 +40,9 @@ const DepartmentForm = ({ initialValues }: DepartmentFormProps) => {return (<Drawer- open+ open={open}onOpenChange={(open) => {- if (!open) onClose();+ if (!open) onClose?.();}}><DrawerContent>@@ -46,10 +52,18 @@ const DepartmentForm = ({ initialValues }: DepartmentFormProps) => {action={isEditing? path.to.department(initialValues.id!)+ : type === "modal"+ ? path.to.api.newDepartment: path.to.newDepartment}defaultValues={initialValues}+ fetcher={fetcher}className="flex flex-col h-full"+ onSubmit={() => {+ if (type === "modal") {+ onClose();+ }+ }}><DrawerHeader><DrawerTitle>{isEditing ? "Edit" : "New"} Department</DrawerTitle>@@ -65,7 +79,7 @@ const DepartmentForm = ({ initialValues }: DepartmentFormProps) => {<DrawerFooter><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/sales.service.ts b/apps/carbon/app/modules/sales/sales.service.tsindex a7d3f46f82..c2370bc81a 100644--- a/apps/carbon/app/modules/sales/sales.service.ts+++ b/apps/carbon/app/modules/sales/sales.service.ts@@ -436,7 +436,7 @@ export async function insertCustomer(updatedBy: string;})) {- return client.from("customer").insert([customer]).select("id").single();+ return client.from("customer").insert([customer]).select("*").single();}export async function insertCustomerContact(@@ -751,7 +751,7 @@ export async function upsertQuoteLine(.select("id").single();}- return client.from("quoteLine").insert([quotationLine]).select("id").single();+ return client.from("quoteLine").insert([quotationLine]).select("*").single();}export async function upsertQuoteMaterial(diff --git a/apps/carbon/app/modules/shared/shared.service.ts b/apps/carbon/app/modules/shared/shared.service.tsindex fa09a40648..2dc6e177cb 100644--- a/apps/carbon/app/modules/shared/shared.service.ts+++ b/apps/carbon/app/modules/shared/shared.service.ts@@ -28,7 +28,7 @@ export async function insertNote(createdBy: string;}) {- return client.from("note").insert([note]).select("id").single();+ return client.from("note").insert([note]).select("*").single();}export async function updateNote(diff --git a/apps/carbon/app/modules/users/users.server.ts b/apps/carbon/app/modules/users/users.server.tsindex 41e4f5f471..4c63f412df 100644--- a/apps/carbon/app/modules/users/users.server.ts+++ b/apps/carbon/app/modules/users/users.server.ts@@ -396,7 +396,7 @@ export async function insertEmployee(client: SupabaseClient<Database>,employee: EmployeeRow) {- return client.from("employee").insert([employee]).select("id").single();+ return client.from("employee").insert([employee]).select("*").single();}async function insertSupplierAccount(diff --git a/apps/carbon/app/modules/users/users.service.ts b/apps/carbon/app/modules/users/users.service.tsindex 352e205678..d34967f1a3 100644--- a/apps/carbon/app/modules/users/users.service.ts+++ b/apps/carbon/app/modules/users/users.service.ts@@ -229,7 +229,7 @@ export async function insertGroup(client: SupabaseClient<Database>,group: { name: string }) {- return client.from("group").insert(group).select("id").single();+ return client.from("group").insert(group).select("*").single();}export async function upsertEmployeeType(diff --git a/apps/carbon/app/routes/api+/resources.departments.new.tsx b/apps/carbon/app/routes/api+/resources.departments.new.tsxnew file mode 100644index 0000000000..28c4d96273--- /dev/null+++ b/apps/carbon/app/routes/api+/resources.departments.new.tsx@@ -0,0 +1,45 @@+import { validationError, validator } from "@carbon/remix-validated-form";+import type { ActionFunctionArgs } from "@remix-run/node";+import { json } from "@remix-run/node";+import { departmentValidator, upsertDepartment } from "~/modules/resources";+import { requirePermissions } from "~/services/auth";+import { flash } from "~/services/session.server";+import { assertIsPost } from "~/utils/http";+import { error } from "~/utils/result";++export async function action({ request }: ActionFunctionArgs) {+ assertIsPost(request);+ const { client, userId } = await requirePermissions(request, {+ create: "resources",+ });++ const validation = await validator(departmentValidator).validate(+ await request.formData()+ );++ if (validation.error) {+ return validationError(validation.error);+ }++ const { id, ...data } = validation.data;++ const createDepartment = await upsertDepartment(client, {+ ...data,+ createdBy: userId,+ });++ if (createDepartment.error) {+ return json(+ createDepartment,+ await flash(+ request,+ error(createDepartment.error, "Failed to insert department")+ )+ );+ }++ const departmentId = createDepartment.data?.id;+ if (!departmentId) throw new Error("Department ID not found");++ return json(createDepartment, { status: 201 });+}diff --git a/apps/carbon/app/routes/x+/resources+/departments.$departmentId.tsx b/apps/carbon/app/routes/x+/resources+/departments.$departmentId.tsxindex ea3a3dfc7f..910aaa2fd8 100644--- a/apps/carbon/app/routes/x+/resources+/departments.$departmentId.tsx+++ b/apps/carbon/app/routes/x+/resources+/departments.$departmentId.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 {DepartmentForm,departmentValidator,@@ -77,6 +77,7 @@ export async function action({ request }: ActionFunctionArgs) {export default function DepartmentRoute() {const { department } = useLoaderData<typeof loader>();+ const navigate = useNavigate();const initialValues = {id: department.id,@@ -86,6 +87,10 @@ export default function DepartmentRoute() {};return (- <DepartmentForm key={initialValues.id} initialValues={initialValues} />+ <DepartmentForm+ onClose={() => navigate(-1)}+ key={initialValues.id}+ initialValues={initialValues}+ />);}diff --git a/apps/carbon/app/routes/x+/resources+/departments.new.tsx b/apps/carbon/app/routes/x+/resources+/departments.new.tsxindex 21a6529819..e35575242e 100644--- a/apps/carbon/app/routes/x+/resources+/departments.new.tsx+++ b/apps/carbon/app/routes/x+/resources+/departments.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 { useNavigate } from "@remix-run/react";import {DepartmentForm,departmentValidator,@@ -50,10 +51,16 @@ export async function action({ request }: ActionFunctionArgs) {}export default function NewDepartmentRoute() {+ const navigate = useNavigate();const initialValues = {name: "",color: "#000000",};- return <DepartmentForm initialValues={initialValues} />;+ return (+ <DepartmentForm+ onClose={() => navigate(-1)}+ initialValues={initialValues}+ />+ );}diff --git a/apps/carbon/app/utils/path.ts b/apps/carbon/app/utils/path.tsindex 98523fbde4..7b13c38c26 100644--- a/apps/carbon/app/utils/path.ts+++ b/apps/carbon/app/utils/path.ts@@ -27,6 +27,7 @@ export const path = {generatePath(`${api}/users/groups?type=${type}`),locations: `${api}/resources/locations`,newCustomer: `${api}/sales/customers/new`,+ newDepartment: `${api}/resources/departments/new`,newPart: `${api}/parts/new`,newSupplier: `${api}/purchasing/suppliers/new`,rollback: (table: string, id: string) =>diff --git a/packages/react/src/ModalDrawer.tsx b/packages/react/src/ModalDrawer.tsxnew file mode 100644index 0000000000..364779ebc2--- /dev/null+++ b/packages/react/src/ModalDrawer.tsx@@ -0,0 +1,175 @@+/*+ ModalDrawer components are an abstraction over Drawer and Modal components.+*/+import type {+ ComponentPropsWithoutRef,+ ElementRef,+ PropsWithChildren,+} from "react";+import { createContext, forwardRef, useContext } from "react";+import {+ Drawer,+ DrawerContent,+ DrawerDescription,+ DrawerFooter,+ DrawerHeader,+ DrawerTitle,+} from "./Drawer";++import {+ Modal,+ ModalBody,+ ModalContent,+ ModalDescription,+ ModalFooter,+ ModalHeader,+ ModalTitle,+} from "./Modal";++const ModalDrawerTypeContext = createContext<"drawer" | "modal">("drawer");+const ModalDrawerTypeProvider = ModalDrawerTypeContext.Provider;++const ModalDrawerProvider = ({+ type = "drawer",+ ...props+}: PropsWithChildren<{+ type?: "drawer" | "modal";+}>) => <ModalDrawerTypeProvider value={type} {...props} />;++const useModalDrawerType = () => {+ const type = useContext(ModalDrawerTypeContext);+ return type;+};++const ModalDrawer = forwardRef<+ ElementRef<typeof Modal> | ElementRef<typeof Drawer>,+ | (ComponentPropsWithoutRef<typeof Modal> & {+ onClose?: () => void;+ })+ | (ComponentPropsWithoutRef<typeof Drawer> & {+ onClose?: () => void;+ })+>(({ onClose, ...props }, ref) => {+ const type = useModalDrawerType();++ if (type === "drawer") {+ return <Drawer {...props} />;+ }++ return (+ <Modal+ {...props}+ onOpenChange={(open) => {+ if (!open) {+ onClose?.();+ }+ }}+ open+ />+ );+});+ModalDrawer.displayName = "ModalDrawer";++const ModalDrawerBody = forwardRef<+ ElementRef<typeof ModalBody> | ElementRef<typeof DrawerContent>,+ | ComponentPropsWithoutRef<typeof ModalBody>+ | ComponentPropsWithoutRef<typeof DrawerContent>+>((props, ref) => {+ const type = useModalDrawerType();++ if (type === "drawer") {+ return <DrawerContent {...props} ref={ref} />;+ }++ return <ModalBody {...props} />;+});+ModalDrawerBody.displayName = "ModalDrawerBody";++const ModalDrawerContent = forwardRef<+ ElementRef<typeof ModalContent> | ElementRef<"div">,+ | ComponentPropsWithoutRef<typeof ModalContent>+ | ComponentPropsWithoutRef<"div">+>((props, ref) => {+ const type = useModalDrawerType();++ if (type === "drawer") {+ return <div {...props} ref={ref} />;+ }++ return <ModalContent {...props} ref={ref} />;+});+ModalDrawerContent.displayName = "ModalDrawerContent";++const ModalDrawerDescription = forwardRef<+ ElementRef<typeof ModalDescription> | ElementRef<typeof DrawerDescription>,+ | ComponentPropsWithoutRef<typeof ModalDescription>+ | ComponentPropsWithoutRef<typeof DrawerDescription>+>((props, ref) => {+ const type = useModalDrawerType();++ if (type === "drawer") {+ return <DrawerDescription {...props} ref={ref} />;+ }++ return <ModalDescription {...props} ref={ref} />;+});+ModalDrawerDescription.displayName = "ModalDrawerDescription";++const ModalDrawerFooter = forwardRef<+ ElementRef<typeof ModalFooter> | ElementRef<typeof DrawerFooter>,+ | ComponentPropsWithoutRef<typeof ModalFooter>+ | ComponentPropsWithoutRef<typeof DrawerFooter>+>((props, ref) => {+ const type = useModalDrawerType();++ if (type === "drawer") {+ return <DrawerFooter {...props} />;+ }++ return <ModalFooter {...props} />;+});+ModalDrawerFooter.displayName = "ModalDrawerFooter";++const ModalDrawerHeader = forwardRef<+ ElementRef<typeof ModalHeader> | ElementRef<typeof DrawerHeader>,+ | ComponentPropsWithoutRef<typeof ModalHeader>+ | ComponentPropsWithoutRef<typeof DrawerHeader>+>((props, ref) => {+ const type = useModalDrawerType();++ if (type === "drawer") {+ return <DrawerHeader {...props} />;+ }++ return <ModalHeader {...props} />;+});+ModalDrawerHeader.displayName = "ModalDrawerHeader";++const ModalDrawerTitle = forwardRef<+ ElementRef<typeof ModalTitle> | ElementRef<typeof DrawerTitle>,+ | ComponentPropsWithoutRef<typeof ModalTitle>+ | ComponentPropsWithoutRef<typeof DrawerTitle>+>((props, ref) => {+ const type = useModalDrawerType();++ if (type === "drawer") {+ return <DrawerTitle {...props} ref={ref} />;+ }++ return <ModalTitle {...props} ref={ref} />;+});+ModalDrawerTitle.displayName = "ModalDrawerTitle";++export {+ ModalDrawer,+ ModalDrawerBody,+ ModalDrawerContent,+ ModalDrawerDescription,+ ModalDrawerFooter,+ ModalDrawerHeader,+ ModalDrawerProvider,+ ModalDrawerTitle,+ ModalDrawerTypeContext,+ ModalDrawerTypeProvider,+ useModalDrawerType,+};diff --git a/packages/react/src/index.tsx b/packages/react/src/index.tsxindex 2e92414da4..59127f121f 100644--- a/packages/react/src/index.tsx+++ b/packages/react/src/index.tsx@@ -158,6 +158,19 @@ import {ModalCardTypeProvider,useModalCardType,} from "./ModalCard";+import {+ ModalDrawer,+ ModalDrawerBody,+ ModalDrawerContent,+ ModalDrawerDescription,+ ModalDrawerFooter,+ ModalDrawerHeader,+ ModalDrawerProvider,+ ModalDrawerTitle,+ ModalDrawerTypeContext,+ ModalDrawerTypeProvider,+ useModalDrawerType,+} from "./ModalDrawer";import type { NumberFieldProps } from "./Number";import {NumberDecrementStepper,@@ -348,6 +361,16 @@ export {ModalClose,ModalContent,ModalDescription,+ ModalDrawer,+ ModalDrawerBody,+ ModalDrawerContent,+ ModalDrawerDescription,+ ModalDrawerFooter,+ ModalDrawerHeader,+ ModalDrawerProvider,+ ModalDrawerTitle,+ ModalDrawerTypeContext,+ ModalDrawerTypeProvider,ModalFooter,ModalHeader,ModalOverlay,@@ -413,6 +436,7 @@ export {useInterval,useKeyboardShortcuts,useModalCardType,+ useModalDrawerType,useMount,useOutsideClick,};