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: add dropdown edit/delete to customer parts

Rob Carrington4d9da480ec68
diff --git a/apps/carbon/app/modules/items/items.service.ts b/apps/carbon/app/modules/items/items.service.ts
index 99b9a972c0..d65659e98e 100644
--- a/apps/carbon/app/modules/items/items.service.ts
+++ b/apps/carbon/app/modules/items/items.service.ts
@@ -30,6 +30,18 @@ import type {
unitOfMeasureValidator,
} from "./items.models";
+export async function deleteItemCustomerPart(
+ client: SupabaseClient<Database>,
+ id: string,
+ companyId: string
+) {
+ return client
+ .from("customerPartToItem")
+ .delete()
+ .eq("id", id)
+ .eq("companyId", companyId);
+}
+
export async function deleteItem(client: SupabaseClient<Database>, id: string) {
return client.from("item").update({ active: false }).eq("id", id);
}
@@ -239,6 +251,19 @@ export async function getItemCost(
.single();
}
+export async function getItemCustomerPart(
+ client: SupabaseClient<Database>,
+ id: string,
+ companyId: string
+) {
+ return client
+ .from("customerPartToItem")
+ .select("*, customer(id, name)")
+ .eq("id", id)
+ .eq("companyId", companyId)
+ .single();
+}
+
export async function getItemCustomerParts(
client: SupabaseClient<Database>,
itemId: string,
@@ -1444,7 +1469,7 @@ export async function upsertItemCustomerPart(
) {
if ("id" in customerPart) {
return client
- .from("buyMethod")
+ .from("customerPartToItem")
.update(sanitize(customerPart))
.eq("id", customerPart.id)
.select("id")
diff --git a/apps/carbon/app/modules/items/ui/Item/CustomerPartForm.tsx b/apps/carbon/app/modules/items/ui/Item/CustomerPartForm.tsx
index 58a7511e80..c6f8e6b298 100644
--- a/apps/carbon/app/modules/items/ui/Item/CustomerPartForm.tsx
+++ b/apps/carbon/app/modules/items/ui/Item/CustomerPartForm.tsx
@@ -46,7 +46,7 @@ const CustomerPartForm = ({ initialValues }: CustomerPartFormProps) => {
defaultValues={initialValues}
validator={customerPartValidator}
method="post"
- action={path.to.newCustomerPart(itemId)}
+ action={isEditing ? undefined : path.to.newCustomerPart(itemId)}
className="flex flex-col h-full"
>
<DrawerHeader>
diff --git a/apps/carbon/app/modules/items/ui/Item/CustomerParts/CustomerParts.tsx b/apps/carbon/app/modules/items/ui/Item/CustomerParts/CustomerParts.tsx
index 03578d800c..f8f73216ed 100644
--- a/apps/carbon/app/modules/items/ui/Item/CustomerParts/CustomerParts.tsx
+++ b/apps/carbon/app/modules/items/ui/Item/CustomerParts/CustomerParts.tsx
@@ -4,24 +4,34 @@ import {
CardContent,
CardHeader,
CardTitle,
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuIcon,
+ DropdownMenuItem,
+ DropdownMenuTrigger,
HStack,
+ IconButton,
} from "@carbon/react";
import { Outlet, useNavigate } from "@remix-run/react";
import type { ColumnDef } from "@tanstack/react-table";
import { useMemo } from "react";
+import { LuPencil, LuTrash } from "react-icons/lu";
+import { MdMoreHoriz } from "react-icons/md";
import { CustomerAvatar, New } from "~/components";
import { EditableText } from "~/components/Editable";
import Grid from "~/components/Grid";
import type { CustomerPart } from "~/modules/items";
+import { path } from "~/utils/path";
import useCustomerParts from "./useCustomerParts";
type CustomerPartsProps = {
customerParts: CustomerPart[];
+ itemId: string;
};
-const CustomerParts = ({ customerParts }: CustomerPartsProps) => {
+const CustomerParts = ({ customerParts, itemId }: CustomerPartsProps) => {
const navigate = useNavigate();
- const { canEdit, onCellEdit } = useCustomerParts();
+ const { canEdit, onCellEdit, canDelete } = useCustomerParts();
const columns = useMemo<ColumnDef<CustomerPart>[]>(() => {
const defaultColumns: ColumnDef<CustomerPart>[] = [
@@ -29,8 +39,44 @@ const CustomerParts = ({ customerParts }: CustomerPartsProps) => {
accessorKey: "customer.id",
header: "Customer",
cell: ({ row }) => (
- <HStack className="justify-between">
+ <HStack className="justify-between min-w-[100px]">
<CustomerAvatar customerId={row.original.customerId} />
+ <div className="relative w-6 h-5">
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <IconButton
+ aria-label="Edit purchase order line type"
+ icon={<MdMoreHoriz />}
+ size="md"
+ className="absolute right-[-1px] top-[-6px]"
+ variant="ghost"
+ onClick={(e) => e.stopPropagation()}
+ />
+ </DropdownMenuTrigger>
+ <DropdownMenuContent>
+ <DropdownMenuItem
+ onClick={() =>
+ navigate(path.to.customerPart(itemId, row.original.id!))
+ }
+ disabled={!canEdit}
+ >
+ <DropdownMenuIcon icon={<LuPencil />} />
+ Edit Customer Part
+ </DropdownMenuItem>
+ <DropdownMenuItem
+ onClick={() =>
+ navigate(
+ path.to.deleteCustomerPart(itemId, row.original.id!)
+ )
+ }
+ disabled={!canDelete}
+ >
+ <DropdownMenuIcon icon={<LuTrash />} />
+ Delete Customer Part
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ </div>
</HStack>
),
},
@@ -46,7 +92,7 @@ const CustomerParts = ({ customerParts }: CustomerPartsProps) => {
},
];
return [...defaultColumns];
- }, []);
+ }, [canDelete, canEdit, itemId, navigate]);
const editableComponents = useMemo(
() => ({
@@ -63,7 +109,9 @@ const CustomerParts = ({ customerParts }: CustomerPartsProps) => {
<CardHeader>
<CardTitle>Customer Parts</CardTitle>
</CardHeader>
- <CardAction>{canEdit && <New to="new" />}</CardAction>
+ <CardAction>
+ {canEdit && <New to={path.to.newCustomerPart(itemId)} />}
+ </CardAction>
</HStack>
<CardContent>
<Grid<CustomerPart>
@@ -71,7 +119,11 @@ const CustomerParts = ({ customerParts }: CustomerPartsProps) => {
columns={columns}
canEdit={canEdit}
editableComponents={editableComponents}
- onNewRow={canEdit ? () => navigate("new") : undefined}
+ onNewRow={
+ canEdit
+ ? () => navigate(path.to.newCustomerPart(itemId))
+ : undefined
+ }
/>
</CardContent>
</Card>
diff --git a/apps/carbon/app/routes/x+/part+/$itemId.sales.customer-parts.$customerPartToItemId.tsx b/apps/carbon/app/routes/x+/part+/$itemId.sales.customer-parts.$customerPartToItemId.tsx
new file mode 100644
index 0000000000..2a418aeeeb
--- /dev/null
+++ b/apps/carbon/app/routes/x+/part+/$itemId.sales.customer-parts.$customerPartToItemId.tsx
@@ -0,0 +1,89 @@
+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 {
+ customerPartValidator,
+ getItemCustomerPart,
+ upsertItemCustomerPart,
+} from "~/modules/items";
+import CustomerPartForm from "~/modules/items/ui/Item/CustomerPartForm";
+import { requirePermissions } from "~/services/auth/auth.server";
+import { flash } from "~/services/session.server";
+import { assertIsPost } from "~/utils/http";
+import { path } from "~/utils/path";
+import { error } from "~/utils/result";
+
+export async function loader({ request, params }: LoaderFunctionArgs) {
+ const { client, companyId } = await requirePermissions(request, {
+ view: "parts",
+ role: "employee",
+ });
+
+ const { customerPartToItemId } = params;
+ if (!customerPartToItemId)
+ throw new Error("Could not find customerPartToItemId");
+
+ const customerPart = await getItemCustomerPart(
+ client,
+ customerPartToItemId,
+ companyId
+ );
+
+ return json({
+ customerPart: customerPart?.data ?? null,
+ });
+}
+
+export async function action({ request, params }: ActionFunctionArgs) {
+ assertIsPost(request);
+ const { client } = await requirePermissions(request, {
+ create: "parts",
+ role: "employee",
+ });
+
+ const { itemId, customerPartToItemId } = params;
+ if (!itemId) throw new Error("Could not find itemId");
+ if (!customerPartToItemId)
+ throw new Error("Could not find customerPartToItemId");
+
+ const formData = await request.formData();
+ const validation = await validator(customerPartValidator).validate(formData);
+
+ if (validation.error) {
+ return validationError(validation.error);
+ }
+
+ const { id, ...data } = validation.data;
+
+ const updatedCustomerPart = await upsertItemCustomerPart(client, {
+ id: customerPartToItemId,
+ ...data,
+ });
+
+ if (updatedCustomerPart.error) {
+ throw redirect(
+ path.to.partSales(itemId),
+ await flash(
+ request,
+ error(updatedCustomerPart.error, "Failed to update customer part")
+ )
+ );
+ }
+
+ throw redirect(path.to.partSales(itemId));
+}
+
+export default function EditCustomerPartRoute() {
+ const { customerPart } = useLoaderData<typeof loader>();
+
+ const initialValues = {
+ id: customerPart?.id ?? "",
+ itemId: customerPart?.itemId ?? "",
+ customerId: customerPart?.customerId ?? "",
+ customerPartId: customerPart?.customerPartId ?? "",
+ customerPartRevision: customerPart?.customerPartRevision ?? "",
+ };
+
+ return <CustomerPartForm initialValues={initialValues} />;
+}
diff --git a/apps/carbon/app/routes/x+/part+/$itemId.sales.customer-parts.delete.$customerPartToItemId.tsx b/apps/carbon/app/routes/x+/part+/$itemId.sales.customer-parts.delete.$customerPartToItemId.tsx
new file mode 100644
index 0000000000..ca74cfa8d8
--- /dev/null
+++ b/apps/carbon/app/routes/x+/part+/$itemId.sales.customer-parts.delete.$customerPartToItemId.tsx
@@ -0,0 +1,91 @@
+import type { ActionFunctionArgs, LoaderFunctionArgs } from "@remix-run/node";
+import { json, redirect } from "@remix-run/node";
+import { useLoaderData, useNavigate, useParams } from "@remix-run/react";
+import { ConfirmDelete } from "~/components/Modals";
+import { deleteItemCustomerPart, getItemCustomerPart } from "~/modules/items";
+import { requirePermissions } from "~/services/auth/auth.server";
+import { flash } from "~/services/session.server";
+import { notFound } from "~/utils/http";
+import { path } from "~/utils/path";
+import { error, success } from "~/utils/result";
+
+export async function loader({ request, params }: LoaderFunctionArgs) {
+ const { client, companyId } = await requirePermissions(request, {
+ delete: "parts",
+ });
+ const { itemId, customerPartToItemId } = params;
+ if (!itemId) throw notFound("itemId not found");
+ if (!customerPartToItemId) throw notFound("customerPartToItemId not found");
+
+ const customerPart = await getItemCustomerPart(
+ client,
+ customerPartToItemId,
+ companyId
+ );
+ if (customerPart.error) {
+ throw redirect(
+ path.to.partSales(itemId),
+ await flash(
+ request,
+ error(customerPart.error, "Failed to get customer part")
+ )
+ );
+ }
+
+ return json({ customerPart: customerPart.data });
+}
+
+export async function action({ request, params }: ActionFunctionArgs) {
+ const { client, companyId } = await requirePermissions(request, {
+ delete: "parts",
+ });
+
+ const { itemId, customerPartToItemId } = params;
+ if (!itemId) throw notFound("Could not find itemId");
+ if (!customerPartToItemId)
+ throw notFound("Could not find customerPartToItemId");
+
+ const { error: deleteTypeError } = await deleteItemCustomerPart(
+ client,
+ customerPartToItemId,
+ companyId
+ );
+ if (deleteTypeError) {
+ throw redirect(
+ path.to.partSales(itemId),
+ await flash(
+ request,
+ error(deleteTypeError, "Failed to delete customer part")
+ )
+ );
+ }
+
+ throw redirect(
+ path.to.partSales(itemId),
+ await flash(request, success("Successfully deleted customer part"))
+ );
+}
+
+export default function DeletePurchaseOrderLineRoute() {
+ const { itemId, customerPartToItemId } = useParams();
+ const { customerPart } = useLoaderData<typeof loader>();
+ const navigate = useNavigate();
+
+ if (!customerPart) return null;
+ if (!itemId) throw notFound("Could not find itemId");
+ if (!customerPartToItemId)
+ throw notFound("Could not find customerPartToItemId");
+
+ const onCancel = () => navigate(path.to.partSales(itemId));
+
+ return (
+ <ConfirmDelete
+ action={path.to.deleteCustomerPart(itemId, customerPartToItemId)}
+ name="Customer Part"
+ text={`Are you sure you want to delete the customer part for ${
+ customerPart?.customer?.name ?? ""
+ }? This cannot be undone.`}
+ onCancel={onCancel}
+ />
+ );
+}
diff --git a/apps/carbon/app/routes/x+/part+/$itemId.sales.new.tsx b/apps/carbon/app/routes/x+/part+/$itemId.sales.customer-parts.new.tsx
similarity index 100%
rename from apps/carbon/app/routes/x+/part+/$itemId.sales.new.tsx
rename to apps/carbon/app/routes/x+/part+/$itemId.sales.customer-parts.new.tsx
diff --git a/apps/carbon/app/routes/x+/part+/$itemId.sales.tsx b/apps/carbon/app/routes/x+/part+/$itemId.sales.tsx
index 1c63d5364c..ba3ad5a72b 100644
--- a/apps/carbon/app/routes/x+/part+/$itemId.sales.tsx
+++ b/apps/carbon/app/routes/x+/part+/$itemId.sales.tsx
@@ -21,6 +21,7 @@ import { error, success } from "~/utils/result";
export async function loader({ request, params }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "parts",
+ role: "employee",
});
const { itemId } = params;
@@ -44,6 +45,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
return json({
partUnitSalePrice: partUnitSalePrice.data,
customerParts: customerParts.data,
+ itemId,
});
}
@@ -88,12 +90,14 @@ export async function action({ request, params }: ActionFunctionArgs) {
}
export default function PartSalesRoute() {
- const { partUnitSalePrice, customerParts } = useLoaderData<typeof loader>();
+ const { partUnitSalePrice, customerParts, itemId } =
+ useLoaderData<typeof loader>();
const initialValues = {
...partUnitSalePrice,
salesUnitOfMeasureCode: partUnitSalePrice?.salesUnitOfMeasureCode ?? "",
...getCustomFields(partUnitSalePrice.customFields),
+ itemId: itemId,
};
return (
@@ -102,7 +106,9 @@ export default function PartSalesRoute() {
key={initialValues.itemId}
initialValues={initialValues}
/>
- {customerParts ? <CustomerParts customerParts={customerParts} /> : null}
+ {customerParts ? (
+ <CustomerParts customerParts={customerParts} itemId={itemId} />
+ ) : null}
</VStack>
);
}
diff --git a/apps/carbon/app/utils/path.ts b/apps/carbon/app/utils/path.ts
index cddba5ff8c..d752fc0edf 100644
--- a/apps/carbon/app/utils/path.ts
+++ b/apps/carbon/app/utils/path.ts
@@ -152,6 +152,10 @@ export const path = {
generatePath(`${x}/customer/${customerId}/locations/${id}`),
customerLocations: (id: string) =>
generatePath(`${x}/customer/${id}/locations`),
+ customerPart: (id: string, customerPartToItemId: string) =>
+ generatePath(
+ `${x}/part/${id}/sales/customer-parts/${customerPartToItemId}`
+ ),
customerPayment: (id: string) =>
generatePath(`${x}/customer/${id}/payments`),
customerShipping: (id: string) =>
@@ -188,6 +192,10 @@ export const path = {
generatePath(`${x}/customer/${customerId}/contacts/delete/${id}`),
deleteCustomerLocation: (customerId: string, id: string) =>
generatePath(`${x}/customer/${customerId}/locations/delete/${id}`),
+ deleteCustomerPart: (id: string, customerPartToItemId: string) =>
+ generatePath(
+ `${x}/part/${id}/sales/customer-parts/delete/${customerPartToItemId}`
+ ),
deleteCustomerStatus: (id: string) =>
generatePath(`${x}/sales/customer-statuses/delete/${id}`),
deleteCustomerType: (id: string) =>
@@ -404,7 +412,8 @@ export const path = {
newCustomerType: `${x}/sales/customer-types/new`,
newCustomField: (tableId: string) =>
generatePath(`${x}/settings/custom-fields/${tableId}/new`),
- newCustomerPart: (id: string) => generatePath(`${x}/part/${id}/sales/new`),
+ newCustomerPart: (id: string) =>
+ generatePath(`${x}/part/${id}/sales/customer-parts/new`),
newDepartment: `${x}/people/departments/new`,
newDocument: `${x}/documents/new`,
newEmployee: `${x}/users/employees/new`,