Google maps integration
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import React, { useState, useEffect } from "react";
|
||||
import React, { useState, useEffect, useRef } from "react";
|
||||
import { useParams, useNavigate } from "react-router-dom";
|
||||
import { Item, Rental, Address } from "../types";
|
||||
import { useAuth } from "../contexts/AuthContext";
|
||||
@@ -58,6 +58,9 @@ const EditItem: React.FC = () => {
|
||||
const [userAddresses, setUserAddresses] = useState<Address[]>([]);
|
||||
const [selectedAddressId, setSelectedAddressId] = useState<string>("");
|
||||
const [addressesLoading, setAddressesLoading] = useState(true);
|
||||
|
||||
// Reference to LocationForm geocoding function
|
||||
const geocodeLocationRef = useRef<(() => Promise<boolean>) | null>(null);
|
||||
const [formData, setFormData] = useState<ItemFormData>({
|
||||
name: "",
|
||||
description: "",
|
||||
@@ -204,10 +207,27 @@ const EditItem: React.FC = () => {
|
||||
}
|
||||
};
|
||||
|
||||
const handleCoordinatesChange = (latitude: number, longitude: number) => {
|
||||
setFormData((prev) => ({
|
||||
...prev,
|
||||
latitude,
|
||||
longitude,
|
||||
}));
|
||||
};
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setError(null);
|
||||
|
||||
// Try to geocode the address before submitting
|
||||
if (geocodeLocationRef.current) {
|
||||
try {
|
||||
await geocodeLocationRef.current();
|
||||
} catch (error) {
|
||||
console.warn('Geocoding failed, updating item without coordinates:', error);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// Use existing image previews (which includes both old and new images)
|
||||
const imageUrls = imagePreviews;
|
||||
@@ -412,6 +432,10 @@ const EditItem: React.FC = () => {
|
||||
onChange={handleChange}
|
||||
onAddressSelect={handleAddressSelect}
|
||||
formatAddressDisplay={formatAddressDisplay}
|
||||
onCoordinatesChange={handleCoordinatesChange}
|
||||
onGeocodeRef={(geocodeFunction) => {
|
||||
geocodeLocationRef.current = geocodeFunction;
|
||||
}}
|
||||
/>
|
||||
|
||||
<DeliveryOptions
|
||||
|
||||
Reference in New Issue
Block a user