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: allow differnt fields to be passed in for other views, sometimes google returns a differnt city name, add commented out debug

edsammy8c0a90aa7039
diff --git a/apps/erp/app/components/Form/AddressAutocomplete.tsx b/apps/erp/app/components/Form/AddressAutocomplete.tsx
index 13af5836f6..9b95df0b1c 100644
--- a/apps/erp/app/components/Form/AddressAutocomplete.tsx
+++ b/apps/erp/app/components/Form/AddressAutocomplete.tsx
@@ -13,6 +13,15 @@ import { useGooglePlaces } from "~/hooks/useGooglePlaces";
import { Input } from ".";
import Country from "./Country";
+type AddressFields = {
+ autocomplete: React.ReactNode;
+ addressLine2: React.ReactNode;
+ city: React.ReactNode;
+ stateProvince: React.ReactNode;
+ postalCode: React.ReactNode;
+ country: React.ReactNode;
+};
+
type AddressAutocompleteProps = {
name?: string;
label?: string;
@@ -29,6 +38,8 @@ type AddressAutocompleteProps = {
showStateProvince?: boolean;
showPostalCode?: boolean;
showCountryCode?: boolean;
+ // Custom layout via children function
+ children?: (fields: AddressFields) => React.ReactNode;
};
const AddressAutocomplete = ({
@@ -45,6 +56,7 @@ const AddressAutocomplete = ({
showStateProvince = true,
showPostalCode = true,
showCountryCode = true,
+ children,
}: AddressAutocompleteProps) => {
// Use provided name or default to addressLine1Name
const fieldName = name ?? addressLine1Name;
@@ -53,6 +65,7 @@ const AddressAutocomplete = ({
const { clearError } = useFormContext();
const [open, setOpen] = useState(false);
const [justSelected, setJustSelected] = useState(false);
+ const [userInteracted, setUserInteracted] = useState(false);
const containerRef = useRef<HTMLDivElement>(null);
// Refs to access the input elements directly
@@ -73,7 +86,7 @@ const AddressAutocomplete = ({
// Memoize the callback to prevent recreating the debounced function
const handleInputChange = useCallback(
(input: string) => {
- if (input && !justSelected) {
+ if (input && !justSelected && userInteracted) {
getSuggestions(input);
setOpen(true);
} else {
@@ -81,17 +94,19 @@ const AddressAutocomplete = ({
setOpen(false);
}
},
- [getSuggestions, clearSuggestions, justSelected]
+ [getSuggestions, clearSuggestions, justSelected, userInteracted]
);
// Debounce the API calls to avoid excessive requests
const debouncedGetSuggestions = useDebounce(handleInputChange, 300);
- // Watch for changes to trigger API calls
+ // Watch for changes to trigger API calls (only after user interaction)
useEffect(() => {
- debouncedGetSuggestions(value || "");
+ if (userInteracted) {
+ debouncedGetSuggestions(value || "");
+ }
// eslint-disable-next-line react-hooks/exhaustive-deps
- }, [value]);
+ }, [value, userInteracted]);
// Handle clicks outside the component
useEffect(() => {
@@ -147,23 +162,25 @@ const AddressAutocomplete = ({
}
};
- return (
- <VStack spacing={4}>
+ // Build field objects
+ const fields: AddressFields = {
+ autocomplete: (
<div className="relative w-full" ref={containerRef}>
<Input
name={fieldName}
label={label}
value={value || ""}
onChange={(e) => {
+ setUserInteracted(true); // Mark that user has interacted
setJustSelected(false); // Allow new searches when user types
setValue(e.target.value);
}}
onFocus={() => {
+ setUserInteracted(true); // Mark that user has interacted
if ((value || "").length >= 3 && !justSelected) {
setOpen(true);
}
}}
- autoComplete="off"
/>
{open && suggestions.length > 0 && (
<div className="absolute w-full mt-1" style={{ zIndex: 9999 }}>
@@ -193,26 +210,44 @@ const AddressAutocomplete = ({
</div>
)}
</div>
+ ),
+ addressLine2: showAddressLine2 ? (
+ <Input
+ ref={addressLine2Ref}
+ name={addressLine2Name}
+ label="Address Line 2"
+ />
+ ) : null,
+ city: showCity ? (
+ <Input ref={cityRef} name={cityName} label="City" />
+ ) : null,
+ stateProvince: showStateProvince ? (
+ <Input
+ ref={stateProvinceRef}
+ name={stateProvinceName}
+ label="State / Province"
+ />
+ ) : null,
+ postalCode: showPostalCode ? (
+ <Input ref={postalCodeRef} name={postalCodeName} label="Postal Code" />
+ ) : null,
+ country: showCountryCode ? <Country name={countryCodeName} /> : null,
+ };
- {showAddressLine2 && (
- <Input
- ref={addressLine2Ref}
- name={addressLine2Name}
- label="Address Line 2"
- />
- )}
- {showCity && <Input ref={cityRef} name={cityName} label="City" />}
- {showStateProvince && (
- <Input
- ref={stateProvinceRef}
- name={stateProvinceName}
- label="State / Province"
- />
- )}
- {showPostalCode && (
- <Input ref={postalCodeRef} name={postalCodeName} label="Postal Code" />
- )}
- {showCountryCode && <Country name={countryCodeName} />}
+ // If children function provided, use it for custom layout
+ if (children) {
+ return <>{children(fields)}</>;
+ }
+
+ // Default vertical layout
+ return (
+ <VStack spacing={4}>
+ {fields.autocomplete}
+ {fields.addressLine2}
+ {fields.city}
+ {fields.stateProvince}
+ {fields.postalCode}
+ {fields.country}
</VStack>
);
};
diff --git a/apps/erp/app/modules/purchasing/ui/Supplier/SupplierLocationForm.tsx b/apps/erp/app/modules/purchasing/ui/Supplier/SupplierLocationForm.tsx
index 490efede61..592c707042 100644
--- a/apps/erp/app/modules/purchasing/ui/Supplier/SupplierLocationForm.tsx
+++ b/apps/erp/app/modules/purchasing/ui/Supplier/SupplierLocationForm.tsx
@@ -18,7 +18,7 @@ import type { PostgrestResponse } from "@supabase/supabase-js";
import { useEffect } from "react";
import type { z } from "zod";
import { CustomFormFields, Hidden, Input, Submit } from "~/components/Form";
-import Country from "~/components/Form/Country";
+import AddressAutocomplete from "~/components/Form/AddressAutocomplete";
import { usePermissions } from "~/hooks";
import { supplierLocationValidator } from "~/modules/purchasing";
import { path } from "~/utils/path";
@@ -91,12 +91,7 @@ const SupplierLocationForm = ({
<Hidden name="addressId" />
<VStack spacing={4}>
<Input name="name" label="Name" />
- <Input name="addressLine1" label="Address Line 1" />
- <Input name="addressLine2" label="Address Line 2" />
- <Input name="city" label="City" />
- <Input name="stateProvince" label="State / Province" />
- <Input name="postalCode" label="Postal Code" />
- <Country name="countryCode" />
+ <AddressAutocomplete />
<CustomFormFields table="supplierLocation" />
</VStack>
</ModalDrawerBody>
diff --git a/apps/erp/app/modules/settings/ui/Company/CompanyForm.tsx b/apps/erp/app/modules/settings/ui/Company/CompanyForm.tsx
index 84b1a6cfb5..42166f02c3 100644
--- a/apps/erp/app/modules/settings/ui/Company/CompanyForm.tsx
+++ b/apps/erp/app/modules/settings/ui/Company/CompanyForm.tsx
@@ -2,7 +2,7 @@ import { ValidatedForm } from "@carbon/form";
import { VStack } from "@carbon/react";
import type { z } from "zod";
import { Currency, Hidden, Input, Submit } from "~/components/Form";
-import Country from "~/components/Form/Country";
+import AddressAutocomplete from "~/components/Form/AddressAutocomplete";
import { companyValidator } from "~/modules/settings";
import { path } from "~/utils/path";
@@ -25,12 +25,25 @@ const CompanyForm = ({ company }: CompanyFormProps) => {
<div className="grid grid-cols-1 md:grid-cols-2 gap-4 w-full">
<Input name="name" label="Company Name" />
<Input name="taxId" label="Tax ID" />
- <Input name="addressLine1" label="Address Line 1" />
- <Input name="addressLine2" label="Address Line 2" />
- <Input name="city" label="City" />
- <Input name="stateProvince" label="State / Province" />
- <Input name="postalCode" label="Postal Code" />
- <Country name="countryCode" label="Country" />
+ <AddressAutocomplete>
+ {({
+ autocomplete,
+ addressLine2,
+ city,
+ stateProvince,
+ postalCode,
+ country,
+ }) => (
+ <>
+ {autocomplete}
+ {addressLine2}
+ {city}
+ {stateProvince}
+ {postalCode}
+ {country}
+ </>
+ )}
+ </AddressAutocomplete>
<Currency
name="baseCurrencyCode"
label="Base Currency"
diff --git a/apps/erp/app/routes/onboarding+/company.tsx b/apps/erp/app/routes/onboarding+/company.tsx
index ec6d0c2566..05906e6af8 100644
--- a/apps/erp/app/routes/onboarding+/company.tsx
+++ b/apps/erp/app/routes/onboarding+/company.tsx
@@ -22,7 +22,6 @@ import { getLocalTimeZone } from "@internationalized/date";
import { Link, useLoaderData } from "@remix-run/react";
import { tasks } from "@trigger.dev/sdk/v3";
import { json, redirect, type ActionFunctionArgs } from "@vercel/remix";
-import { useEffect } from "react";
import {
AddressAutocomplete,
Currency,
@@ -201,23 +200,6 @@ export default function OnboardingCompany() {
baseCurrencyCode: company?.baseCurrencyCode ?? "USD",
};
- // Debug: watch form values
- useEffect(() => {
- const interval = setInterval(() => {
- const form = document.querySelector("form");
- if (form) {
- const formData = new FormData(form);
- const entries: Record<string, any> = {};
- formData.forEach((value, key) => {
- entries[key] = value;
- });
- console.log("📋 Current form values:", entries);
- }
- }, 2000); // Log every 2 seconds
-
- return () => clearInterval(interval);
- }, []);
-
return (
<Card className="max-w-lg">
<ValidatedForm