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.

fix: add flexible address formatting to handle missing address fields gracefully

Rob Carrington44a391adff90
diff --git a/apps/carbon/app/components/Form/CustomerLocation.tsx b/apps/carbon/app/components/Form/CustomerLocation.tsx
index 543aed6f52..963ea66c66 100644
--- a/apps/carbon/app/components/Form/CustomerLocation.tsx
+++ b/apps/carbon/app/components/Form/CustomerLocation.tsx
@@ -8,6 +8,7 @@ import {
import { path } from "~/utils/path";
import { useDisclosure } from "@carbon/react";
+import { formatAddress } from "@carbon/utils";
import type { ComboboxProps } from "./Combobox";
import CreatableCombobox from "./CreatableCombobox";
@@ -40,7 +41,12 @@ const CustomerLocation = (props: CustomerLocationSelectProps) => {
() =>
customerLocationsFetcher.data?.data?.map((c) => ({
value: c.id,
- label: `${c.address?.addressLine1} ${c.address?.city}, ${c.address?.state} (${c.name})`,
+ label: `${formatAddress(
+ c.address?.addressLine1,
+ c.address?.addressLine2,
+ c.address?.city,
+ c.address?.state
+ )} (${c.name})`,
})) ?? [],
[customerLocationsFetcher.data]
diff --git a/apps/carbon/app/components/Form/SupplierLocation.tsx b/apps/carbon/app/components/Form/SupplierLocation.tsx
index 0c3e25ef89..0d9054d001 100644
--- a/apps/carbon/app/components/Form/SupplierLocation.tsx
+++ b/apps/carbon/app/components/Form/SupplierLocation.tsx
@@ -8,6 +8,7 @@ import {
import { path } from "~/utils/path";
import { useDisclosure } from "@carbon/react";
+import { formatAddress } from "@carbon/utils";
import type { ComboboxProps } from "./Combobox";
import CreatableCombobox from "./CreatableCombobox";
@@ -39,7 +40,12 @@ const SupplierLocation = (props: SupplierLocationSelectProps) => {
() =>
supplierLocationsFetcher.data?.data?.map((c) => ({
value: c.id,
- label: `${c.address?.addressLine1} ${c.address?.city}, ${c.address?.state} (${c.name})`,
+ label: `${formatAddress(
+ c.address?.addressLine1,
+ c.address?.addressLine2,
+ c.address?.city,
+ c.address?.state
+ )} (${c.name})`,
})) ?? [],
[supplierLocationsFetcher.data]
diff --git a/apps/carbon/app/components/Location.tsx b/apps/carbon/app/components/Location.tsx
index ca28fd7252..418a660a0d 100644
--- a/apps/carbon/app/components/Location.tsx
+++ b/apps/carbon/app/components/Location.tsx
@@ -4,6 +4,7 @@ import {
DropdownMenuItem,
VStack,
} from "@carbon/react";
+import { formatAddressLines, formatCityStateZipCountry } from "@carbon/utils";
import { LuMapPin } from "react-icons/lu";
import type { Action } from "~/types";
@@ -11,9 +12,10 @@ type LocationProps = {
location: {
name: string;
address: {
+ addressLine1: string | null;
+ addressLine2: string | null;
city: string | null;
state: string | null;
- addressLine1: string | null;
postalCode: string | null;
} | null;
};
@@ -26,20 +28,25 @@ const Location = ({ location, actions }: LocationProps) => {
}
const locationName = location.name;
- const addressLine1 = location.address.addressLine1;
- const cityStateZip = `${location.address.city ?? ""}, ${
- location.address.state ?? ""
- } ${location.address.postalCode ?? ""}`;
+ const addressLines = formatAddressLines(
+ location.address.addressLine1,
+ location.address.addressLine2
+ );
+ const cityStateZipCountry = formatCityStateZipCountry(
+ location.address.city,
+ location.address.state,
+ location.address.postalCode
+ );
return (
<div className="grid w-full gap-4 grid-cols-[auto_1fr_auto]">
<LuMapPin className="w-8 h-8" />
<VStack spacing={0}>
<p className="font-bold line-clamp-1">{locationName}</p>
<p className="text-sm text-muted-foreground line-clamp-1">
- {addressLine1}
+ {addressLines}
</p>
<p className="text-sm text-muted-foreground line-clamp-1">
- {cityStateZip}
+ {cityStateZipCountry}
</p>
</VStack>
{actions.length > 0 && (
diff --git a/packages/utils/src/address.ts b/packages/utils/src/address.ts
new file mode 100644
index 0000000000..59fa69ab2b
--- /dev/null
+++ b/packages/utils/src/address.ts
@@ -0,0 +1,67 @@
+export function formatAddressLines(
+ addressLine1?: string | null,
+ addressLine2?: string | null
+): string {
+ // Concatenate addressLine1 and addressLine2 if both are provided
+ if (addressLine1) {
+ const address = addressLine2
+ ? `${addressLine1} ${addressLine2}`
+ : addressLine1;
+ return address;
+ } else if (addressLine2) {
+ return addressLine2;
+ }
+}
+
+export function formatCityStateZipCountry(
+ city?: string | null,
+ state?: string | null,
+ zip?: string | null,
+ country?: string | null
+): string {
+ // Create an array to hold the different parts of the address
+ const parts: string[] = [];
+
+ // Add city in the correct format
+ if (city) parts.push(city);
+
+ // Combine state and zip without a comma if both are provided
+ if (state) {
+ const stateZip = zip ? `${state} ${zip}` : state;
+ parts.push(stateZip);
+ } else if (zip) {
+ parts.push(zip);
+ }
+
+ // Add country in the correct format
+ if (country) parts.push(country);
+
+ // Join all parts with a comma separator
+ return parts.join(", ");
+}
+
+export function formatAddress(
+ addressLine1?: string | null,
+ addressLine2?: string | null,
+ city?: string | null,
+ state?: string | null,
+ zip?: string | null,
+ country?: string | null
+): string {
+ // Create an array to hold the different parts of the address
+ const parts: string[] = [];
+
+ const formattedAddressLines = formatAddressLines(addressLine1, addressLine2);
+ if (formattedAddressLines) parts.push(formattedAddressLines);
+
+ const formattedCityStateZipCountry = formatCityStateZipCountry(
+ city,
+ state,
+ zip,
+ country
+ );
+ if (formattedCityStateZipCountry) parts.push(formattedCityStateZipCountry);
+
+ // Join all parts with a comma separator
+ return parts.join(", ");
+}
diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts
index d88cba6fc7..440ec1edaa 100644
--- a/packages/utils/src/index.ts
+++ b/packages/utils/src/index.ts
@@ -1,4 +1,5 @@
export * from "./accounting";
+export * from "./address";
export * from "./arrays";
export * from "./color";
export * from "./date";