minor db index work

This commit is contained in:
2025-12-03 21:45:35 -08:00
parent 8d29ac6b90
commit 0f8fd255d5
5 changed files with 40 additions and 6 deletions

View File

@@ -7,6 +7,7 @@ import { LoadingSpinner } from '../../../components/LoadingSpinner';
interface AddressFormProps {
address: Partial<Address>;
onAddressChange: (field: keyof Address, value: string) => void;
onGeocode: () => void;
isGeocoding: boolean;
}
@@ -18,11 +19,15 @@ export const AddressForm: React.FC<AddressFormProps> = ({ address, onAddressChan
return (
<div className="space-y-4">
<div className="flex items-center space-x-3">
<div className="flex justify-between items-center">
<h3 className="text-lg font-medium text-gray-900 dark:text-white">Home Address</h3>
{isGeocoding && (
<div className="w-4 h-4 text-brand-primary"><LoadingSpinner /></div>
)}
<button type="button" onClick={onGeocode} disabled={isGeocoding} className="text-sm bg-gray-200 dark:bg-gray-700 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-800 dark:text-white font-bold py-1 px-3 rounded-lg flex items-center disabled:opacity-50">
{isGeocoding ? <LoadingSpinner /> : <MapPinIcon className="w-4 h-4 mr-2" />}
Re-Geocode
</button>
</div>
<div>
<label htmlFor="address_line_1" className="block text-sm font-medium text-gray-700 dark:text-gray-300">Address Line 1</label>

View File

@@ -139,6 +139,32 @@ export const ProfileManager: React.FC<ProfileManagerProps> = ({ isOpen, onClose,
setAddress(prev => ({ ...prev, [field]: value }));
};
const handleManualGeocode = async () => {
const addressString = [
address.address_line_1,
address.city,
address.province_state,
address.postal_code,
address.country,
].filter(Boolean).join(', ');
if (!addressString) {
toast.error('Please fill in the address fields before geocoding.');
return;
}
setIsGeocoding(true);
try {
const response = await apiClient.geocodeAddress(addressString);
const { lat, lng } = await response.json();
setAddress(prev => ({ ...prev, latitude: lat, longitude: lng }));
toast.success('Address re-geocoded successfully!');
} catch (error) {
toast.error('Failed to re-geocode address.');
} finally {
setIsGeocoding(false);
}
};
// --- Automatic Geocoding Logic ---
const debouncedAddress = useDebounce(address, 1500); // Debounce address state by 1.5 seconds
@@ -442,7 +468,7 @@ export const ProfileManager: React.FC<ProfileManagerProps> = ({ isOpen, onClose,
<input id="avatarUrl" type="url" value={avatarUrl} onChange={e => setAvatarUrl(e.target.value)} className="mt-1 block w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm" />
</div>
<div className="border-t border-gray-200 dark:border-gray-700 pt-4">
<AddressForm address={address} onAddressChange={handleAddressChange} isGeocoding={isGeocoding} />
<AddressForm address={address} onAddressChange={handleAddressChange} onGeocode={handleManualGeocode} isGeocoding={isGeocoding} />
</div>
{address.latitude && address.longitude && (
<div className="pt-4">

View File

@@ -185,8 +185,11 @@ export async function createFlyerAndItems(
};
const addressRes = await client.query<{ address_id: number }>(
`INSERT INTO public.addresses (address_line_1, city, province_state, postal_code, country, latitude, longitude, location)
VALUES ($1, $2, $3, $4, $5, $6, $7, ${coords ? `ST_SetSRID(ST_MakePoint(${coords.lng}, ${coords.lat}), 4326)` : null})
ON CONFLICT (address_line_1) DO UPDATE SET address_line_1 = EXCLUDED.address_line_1 RETURNING address_id`,
VALUES ($1, $2, $3, $4, $5, $6, $7, ${coords ? `ST_SetSRID(ST_MakePoint(${coords.lng}, ${coords.lat}), 4326)` : 'NULL'})
ON CONFLICT (address_line_1) DO UPDATE
SET latitude = EXCLUDED.latitude, longitude = EXCLUDED.longitude, location = EXCLUDED.location, updated_at = NOW()
WHERE public.addresses.latitude IS NULL AND EXCLUDED.latitude IS NOT NULL
RETURNING address_id`,
[addressData.address_line_1, addressData.city, addressData.province_state, addressData.postal_code, addressData.country, addressData.latitude, addressData.longitude]
);
const addressId = addressRes.rows[0].address_id;