fixing bugs with item notification radius
This commit is contained in:
93
frontend/src/hooks/useAddressAutocomplete.ts
Normal file
93
frontend/src/hooks/useAddressAutocomplete.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
import { useCallback } from 'react';
|
||||
import { PlaceDetails } from '../services/placesService';
|
||||
|
||||
// US States list
|
||||
export const usStates = [
|
||||
"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado",
|
||||
"Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho",
|
||||
"Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana",
|
||||
"Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota",
|
||||
"Mississippi", "Missouri", "Montana", "Nebraska", "Nevada",
|
||||
"New Hampshire", "New Jersey", "New Mexico", "New York",
|
||||
"North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon",
|
||||
"Pennsylvania", "Rhode Island", "South Carolina", "South Dakota",
|
||||
"Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington",
|
||||
"West Virginia", "Wisconsin", "Wyoming",
|
||||
];
|
||||
|
||||
// State code to full name mapping
|
||||
const stateCodeToName: { [key: string]: string } = {
|
||||
AL: "Alabama", AK: "Alaska", AZ: "Arizona", AR: "Arkansas",
|
||||
CA: "California", CO: "Colorado", CT: "Connecticut", DE: "Delaware",
|
||||
FL: "Florida", GA: "Georgia", HI: "Hawaii", ID: "Idaho",
|
||||
IL: "Illinois", IN: "Indiana", IA: "Iowa", KS: "Kansas",
|
||||
KY: "Kentucky", LA: "Louisiana", ME: "Maine", MD: "Maryland",
|
||||
MA: "Massachusetts", MI: "Michigan", MN: "Minnesota", MS: "Mississippi",
|
||||
MO: "Missouri", MT: "Montana", NE: "Nebraska", NV: "Nevada",
|
||||
NH: "New Hampshire", NJ: "New Jersey", NM: "New Mexico", NY: "New York",
|
||||
NC: "North Carolina", ND: "North Dakota", OH: "Ohio", OK: "Oklahoma",
|
||||
OR: "Oregon", PA: "Pennsylvania", RI: "Rhode Island", SC: "South Carolina",
|
||||
SD: "South Dakota", TN: "Tennessee", TX: "Texas", UT: "Utah",
|
||||
VT: "Vermont", VA: "Virginia", WA: "Washington", WV: "West Virginia",
|
||||
WI: "Wisconsin", WY: "Wyoming", DC: "District of Columbia",
|
||||
PR: "Puerto Rico", VI: "Virgin Islands", AS: "American Samoa",
|
||||
GU: "Guam", MP: "Northern Mariana Islands",
|
||||
};
|
||||
|
||||
export interface ParsedAddress {
|
||||
address1: string;
|
||||
city: string;
|
||||
state: string;
|
||||
zipCode: string;
|
||||
country: string;
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom hook for handling Google Places autocomplete
|
||||
* Parses place details and extracts address components
|
||||
*/
|
||||
export const useAddressAutocomplete = () => {
|
||||
const parsePlace = useCallback((place: PlaceDetails): ParsedAddress | null => {
|
||||
try {
|
||||
const addressComponents = place.addressComponents;
|
||||
|
||||
// Build address1 from street number and route
|
||||
const streetNumber = addressComponents.streetNumber || "";
|
||||
const route = addressComponents.route || "";
|
||||
const address1 = `${streetNumber} ${route}`.trim() || place.formattedAddress;
|
||||
|
||||
// Parse state - convert code to full name
|
||||
const stateCode = addressComponents.administrativeAreaLevel1 || "";
|
||||
const stateName =
|
||||
stateCodeToName[stateCode] ||
|
||||
addressComponents.administrativeAreaLevel1Long ||
|
||||
stateCode;
|
||||
|
||||
// Only use the state if it's valid
|
||||
const state = usStates.includes(stateName) ? stateName : "";
|
||||
|
||||
if (!state) {
|
||||
console.warn(
|
||||
`State not found in dropdown options: ${stateName} (code: ${stateCode})`
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
address1,
|
||||
city: addressComponents.locality || "",
|
||||
state,
|
||||
zipCode: addressComponents.postalCode || "",
|
||||
country: addressComponents.country || "US",
|
||||
latitude: place.geometry.latitude,
|
||||
longitude: place.geometry.longitude,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error parsing place details:", error);
|
||||
return null;
|
||||
}
|
||||
}, []);
|
||||
|
||||
return { parsePlace };
|
||||
};
|
||||
Reference in New Issue
Block a user