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.

refactor: new table/filter components for customers table

Brad Barbin80f32612f11a
diff --git a/apps/carbon/app/modules/sales/sales.service.ts b/apps/carbon/app/modules/sales/sales.service.ts
index bdeea583e9..534a9f825f 100644
--- a/apps/carbon/app/modules/sales/sales.service.ts
+++ b/apps/carbon/app/modules/sales/sales.service.ts
@@ -181,25 +181,15 @@ export async function getCustomerShipping(
export async function getCustomers(
client: SupabaseClient<Database>,
args: GenericQueryFilters & {
- name: string | null;
- type: string | null;
- status: string | null;
+ search: string | null;
}
) {
let query = client.from("customers").select("*", {
count: "exact",
});
- if (args.name) {
- query = query.ilike("name", `%${args.name}%`);
- }
-
- if (args.type) {
- query = query.eq("customerTypeId", args.type);
- }
-
- if (args.status) {
- query = query.eq("customerStatusId", args.status);
+ if (args.search) {
+ query = query.ilike("name", `%${args.search}%`);
}
query = setGenericQueryFilters(query, args, [
diff --git a/apps/carbon/app/modules/sales/ui/CustomerStatuses/CustomerStatusesTable.tsx b/apps/carbon/app/modules/sales/ui/CustomerStatuses/CustomerStatusesTable.tsx
index e5ce231753..003a944675 100644
--- a/apps/carbon/app/modules/sales/ui/CustomerStatuses/CustomerStatusesTable.tsx
+++ b/apps/carbon/app/modules/sales/ui/CustomerStatuses/CustomerStatusesTable.tsx
@@ -42,7 +42,7 @@ const CustomerStatusesTable = memo(
<>
<MenuItem
onClick={() => {
- navigate(`${path.to.customers}?status=${row.id}`);
+ navigate(`${path.to.customers}?filter=status:eq:${row.name}`);
}}
>
<MenuIcon icon={<BsPeopleFill />} />
diff --git a/apps/carbon/app/modules/sales/ui/CustomerTypes/CustomerTypesTable.tsx b/apps/carbon/app/modules/sales/ui/CustomerTypes/CustomerTypesTable.tsx
index c3e3781303..ed36837562 100644
--- a/apps/carbon/app/modules/sales/ui/CustomerTypes/CustomerTypesTable.tsx
+++ b/apps/carbon/app/modules/sales/ui/CustomerTypes/CustomerTypesTable.tsx
@@ -41,7 +41,7 @@ const CustomerTypesTable = memo(({ data, count }: CustomerTypesTableProps) => {
<>
<MenuItem
onClick={() => {
- navigate(`${path.to.customers}?type=${row.id}`);
+ navigate(`${path.to.customers}?filter=type:eq:${row.name}`);
}}
>
<MenuIcon icon={<BsPeopleFill />} />
diff --git a/apps/carbon/app/modules/sales/ui/Customers/CustomersTable.tsx b/apps/carbon/app/modules/sales/ui/Customers/CustomersTable.tsx
index 384b77f5e5..507fda8e94 100644
--- a/apps/carbon/app/modules/sales/ui/Customers/CustomersTable.tsx
+++ b/apps/carbon/app/modules/sales/ui/Customers/CustomersTable.tsx
@@ -3,83 +3,109 @@ import { useNavigate } from "@remix-run/react";
import type { ColumnDef } from "@tanstack/react-table";
import { memo, useMemo } from "react";
import { BsFillPenFill } from "react-icons/bs";
-import { Table } from "~/components";
-import type { Customer } from "~/modules/sales";
+import { TableNew } from "~/components";
+import { usePermissions } from "~/hooks";
+import type { Customer, CustomerStatus, CustomerType } from "~/modules/sales";
import { path } from "~/utils/path";
type CustomersTableProps = {
data: Customer[];
count: number;
+ customerTypes: Partial<CustomerType>[];
+ customerStatuses: CustomerStatus[];
};
-const CustomersTable = memo(({ data, count }: CustomersTableProps) => {
- const navigate = useNavigate();
+const CustomersTable = memo(
+ ({ data, count, customerStatuses, customerTypes }: CustomersTableProps) => {
+ const navigate = useNavigate();
+ const permissions = usePermissions();
- const columns = useMemo<ColumnDef<Customer>[]>(() => {
- return [
- {
- accessorKey: "name",
- header: "Name",
- cell: ({ row }) => (
- <Hyperlink
- onClick={() => navigate(path.to.customer(row.original.id!))}
- >
- {row.original.name}
- </Hyperlink>
- ),
- },
- {
- accessorKey: "type",
- header: "Customer Type",
- cell: (item) => <Enumerable value={item.getValue<string>()} />,
- },
- {
- accessorKey: "status",
- header: "Customer Status",
- cell: (item) => <Enumerable value={item.getValue<string>()} />,
- },
- // {
- // id: "orders",
- // header: "Orders",
- // cell: ({ row }) => (
- //
- // <Button
- // variant="secondary"
- // onClick={() =>
- // navigate(`${path.to.salesOrders}?customerId=${row.original.id}`)
- // }
- // >
- // {row.original.orderCount ?? 0} Orders
- // </Button>
- // ),
- // },
- ];
- }, [navigate]);
+ const columns = useMemo<ColumnDef<Customer>[]>(() => {
+ return [
+ {
+ accessorKey: "name",
+ header: "Name",
+ cell: ({ row }) => (
+ <Hyperlink
+ onClick={() => navigate(path.to.customer(row.original.id!))}
+ >
+ {row.original.name}
+ </Hyperlink>
+ ),
+ },
+ {
+ accessorKey: "type",
+ header: "Customer Type",
+ cell: (item) => <Enumerable value={item.getValue<string>()} />,
+ meta: {
+ filter: {
+ type: "static",
+ options: customerTypes?.map((type) => ({
+ value: type.name ?? "",
+ label: <Enumerable value={type.name ?? ""} />,
+ })),
+ },
+ },
+ },
+ {
+ accessorKey: "status",
+ header: "Customer Status",
+ cell: (item) => <Enumerable value={item.getValue<string>()} />,
+ meta: {
+ filter: {
+ type: "static",
+ options: customerStatuses?.map((status) => ({
+ value: status.name,
+ label: <Enumerable value={status.name ?? ""} />,
+ })),
+ },
+ },
+ },
+ // {
+ // id: "orders",
+ // header: "Orders",
+ // cell: ({ row }) => (
+ //
+ // <Button
+ // variant="secondary"
+ // onClick={() =>
+ // navigate(`${path.to.salesOrders}?customerId=${row.original.id}`)
+ // }
+ // >
+ // {row.original.orderCount ?? 0} Orders
+ // </Button>
+ // ),
+ // },
+ ];
+ }, [customerStatuses, customerTypes, navigate]);
- const renderContextMenu = useMemo(
- // eslint-disable-next-line react/display-name
- () => (row: Customer) =>
- (
- <MenuItem onClick={() => navigate(path.to.customer(row.id!))}>
- <MenuIcon icon={<BsFillPenFill />} />
- Edit Customer
- </MenuItem>
- ),
- [navigate]
- );
+ const renderContextMenu = useMemo(
+ // eslint-disable-next-line react/display-name
+ () => (row: Customer) =>
+ (
+ <MenuItem onClick={() => navigate(path.to.customer(row.id!))}>
+ <MenuIcon icon={<BsFillPenFill />} />
+ Edit Customer
+ </MenuItem>
+ ),
+ [navigate]
+ );
- return (
- <>
- <Table<Customer>
- count={count}
- columns={columns}
- data={data}
- withPagination
- renderContextMenu={renderContextMenu}
- />
- </>
- );
-});
+ return (
+ <>
+ <TableNew<Customer>
+ count={count}
+ columns={columns}
+ data={data}
+ newPath={path.to.newCustomer}
+ newPermission={permissions.can("create", "sales")}
+ withPagination
+ renderContextMenu={renderContextMenu}
+ />
+ </>
+ );
+ }
+);
CustomersTable.displayName = "CustomerTable";
diff --git a/apps/carbon/app/modules/sales/ui/Customers/CustomersTableFilters.tsx b/apps/carbon/app/modules/sales/ui/Customers/CustomersTableFilters.tsx
deleted file mode 100644
index d75bf392f4..0000000000
--- a/apps/carbon/app/modules/sales/ui/Customers/CustomersTableFilters.tsx
+++ /dev/null
@@ -1,75 +0,0 @@
-import { HStack } from "@carbon/react";
-import { Combobox, New, Select } from "~/components";
-import { TableFilters } from "~/components/Layout";
-import { DebouncedInput } from "~/components/Search";
-import { usePermissions, useUrlParams } from "~/hooks";
-import type { CustomerStatus, CustomerType } from "~/modules/sales";
-import { path } from "~/utils/path";
-
-type CustomersTableFiltersProps = {
- customerTypes: Partial<CustomerType>[];
- customerStatuses: CustomerStatus[];
-};
-
-const CustomersTableFilters = ({
- customerTypes,
- customerStatuses,
-}: CustomersTableFiltersProps) => {
- const [params, setParams] = useUrlParams();
- const permissions = usePermissions();
- const customerTypeOptions =
- customerTypes?.map<{ value: string; label: string }>((type) => ({
- value: type.id!,
- label: type.name!,
- })) ?? [];
-
- const customerStatusOptions =
- customerStatuses?.map((status) => ({
- value: status.id,
- label: status.name,
- })) ?? [];
-
- return (
- <TableFilters>
- <HStack>
- <DebouncedInput param="name" size="sm" placeholder="Search" />
- {customerTypeOptions.length > 0 && (
- <Combobox
- size="sm"
- value={params.get("type") ?? ""}
- isClearable
- options={customerTypeOptions}
- onChange={(selected) => {
- setParams({ type: selected });
- }}
- aria-label="Customer Type"
- placeholder="Customer Type"
- />
- )}
- {customerStatusOptions && (
- <Select
- size="sm"
- isClearable
- value={params.get("status") ?? ""}
- options={customerStatusOptions}
- onChange={(selected) => {
- setParams({ status: selected });
- }}
- aria-label="Status"
- placeholder="Customer Status"
- />
- )}
- </HStack>
- <HStack>
- {permissions.can("create", "sales") && (
- <New
- label="Customer"
- to={`${path.to.newCustomer}?${params.toString()}`}
- />
- )}
- </HStack>
- </TableFilters>
- );
-};
-
-export default CustomersTableFilters;
diff --git a/apps/carbon/app/modules/sales/ui/Customers/index.ts b/apps/carbon/app/modules/sales/ui/Customers/index.ts
index bfc2c47c3e..66d6af25ed 100644
--- a/apps/carbon/app/modules/sales/ui/Customers/index.ts
+++ b/apps/carbon/app/modules/sales/ui/Customers/index.ts
@@ -1,4 +1,3 @@
import CustomersTable from "./CustomersTable";
-import CustomersTableFilters from "./CustomersTableFilters";
-export { CustomersTable, CustomersTableFilters };
+export { CustomersTable };
diff --git a/apps/carbon/app/routes/x+/sales+/customers.tsx b/apps/carbon/app/routes/x+/sales+/customers.tsx
index 07d2e9f934..6fe887f0f6 100644
--- a/apps/carbon/app/routes/x+/sales+/customers.tsx
+++ b/apps/carbon/app/routes/x+/sales+/customers.tsx
@@ -4,7 +4,6 @@ import { json, redirect } from "@remix-run/node";
import { Outlet, useLoaderData } from "@remix-run/react";
import {
CustomersTable,
- CustomersTableFilters,
getCustomerStatuses,
getCustomerTypes,
getCustomers,
@@ -28,15 +27,19 @@ export async function loader({ request }: LoaderFunctionArgs) {
const url = new URL(request.url);
const searchParams = new URLSearchParams(url.search);
- const name = searchParams.get("name");
- const type = searchParams.get("type");
- const status = searchParams.get("status");
+ const search = searchParams.get("search");
const { limit, offset, sorts, filters } =
getGenericQueryFilters(searchParams);
const [customers, customerTypes, customerStatuses] = await Promise.all([
- getCustomers(client, { name, type, status, limit, offset, sorts, filters }),
+ getCustomers(client, {
+ search,
+ limit,
+ offset,
+ sorts,
+ filters,
+ }),
getCustomerTypes(client),
getCustomerStatuses(client),
]);
@@ -62,11 +65,12 @@ export default function SalesCustomersRoute() {
return (
<VStack spacing={0} className="h-full">
- <CustomersTableFilters
+ <CustomersTable
+ data={customers}
+ count={count}
customerTypes={customerTypes}
customerStatuses={customerStatuses}
/>
- <CustomersTable data={customers} count={count} />
<Outlet />
</VStack>
);