mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-22 19:31:12 -05:00
Fix: Implement location data storage fix
This commit is contained in:
@@ -23,7 +23,6 @@ interface LocationResult {
|
||||
}
|
||||
|
||||
interface SelectedLocation {
|
||||
id?: string;
|
||||
name: string;
|
||||
city?: string;
|
||||
state_province?: string;
|
||||
@@ -32,6 +31,7 @@ interface SelectedLocation {
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
timezone?: string;
|
||||
display_name: string; // Full OSM display name for reference
|
||||
}
|
||||
|
||||
interface LocationSearchProps {
|
||||
@@ -61,11 +61,10 @@ export function LocationSearch({ onLocationSelect, initialLocationId, className
|
||||
.from('locations')
|
||||
.select('*')
|
||||
.eq('id', locationId)
|
||||
.single();
|
||||
.maybeSingle();
|
||||
|
||||
if (data && !error) {
|
||||
setSelectedLocation({
|
||||
id: data.id,
|
||||
name: data.name,
|
||||
city: data.city || undefined,
|
||||
state_province: data.state_province || undefined,
|
||||
@@ -74,6 +73,7 @@ export function LocationSearch({ onLocationSelect, initialLocationId, className
|
||||
latitude: parseFloat(data.latitude?.toString() || '0'),
|
||||
longitude: parseFloat(data.longitude?.toString() || '0'),
|
||||
timezone: data.timezone || undefined,
|
||||
display_name: data.name, // Use name as display for existing locations
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -94,12 +94,30 @@ export function LocationSearch({ onLocationSelect, initialLocationId, className
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
// Check if response is OK and content-type is JSON
|
||||
if (!response.ok) {
|
||||
console.error('OpenStreetMap API error:', response.status);
|
||||
setResults([]);
|
||||
setShowResults(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const contentType = response.headers.get('content-type');
|
||||
if (!contentType || !contentType.includes('application/json')) {
|
||||
console.error('Invalid response format from OpenStreetMap');
|
||||
setResults([]);
|
||||
setShowResults(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
setResults(data);
|
||||
setShowResults(true);
|
||||
} catch (error) {
|
||||
console.error('Error searching locations:', error);
|
||||
setResults([]);
|
||||
setShowResults(false);
|
||||
} finally {
|
||||
setIsSearching(false);
|
||||
}
|
||||
@@ -123,61 +141,18 @@ export function LocationSearch({ onLocationSelect, initialLocationId, className
|
||||
? `${city}, ${result.address.state || ''} ${result.address.country}`.trim()
|
||||
: result.display_name;
|
||||
|
||||
// Check if location exists in database
|
||||
const { data: existingLocation } = await supabase
|
||||
.from('locations')
|
||||
.select('*')
|
||||
.eq('latitude', latitude)
|
||||
.eq('longitude', longitude)
|
||||
.maybeSingle();
|
||||
|
||||
let locationData: SelectedLocation;
|
||||
|
||||
if (existingLocation) {
|
||||
locationData = {
|
||||
id: existingLocation.id,
|
||||
name: existingLocation.name,
|
||||
city: existingLocation.city || undefined,
|
||||
state_province: existingLocation.state_province || undefined,
|
||||
country: existingLocation.country,
|
||||
postal_code: existingLocation.postal_code || undefined,
|
||||
latitude: parseFloat(existingLocation.latitude?.toString() || '0'),
|
||||
longitude: parseFloat(existingLocation.longitude?.toString() || '0'),
|
||||
timezone: existingLocation.timezone || undefined,
|
||||
};
|
||||
} else {
|
||||
// Create new location
|
||||
const { data: newLocation, error } = await supabase
|
||||
.from('locations')
|
||||
.insert({
|
||||
name: locationName,
|
||||
city: city || null,
|
||||
state_province: result.address.state || null,
|
||||
country: result.address.country || '',
|
||||
postal_code: result.address.postcode || null,
|
||||
latitude,
|
||||
longitude,
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (error || !newLocation) {
|
||||
console.error('Error creating location:', error);
|
||||
return;
|
||||
}
|
||||
|
||||
locationData = {
|
||||
id: newLocation.id,
|
||||
name: newLocation.name,
|
||||
city: newLocation.city || undefined,
|
||||
state_province: newLocation.state_province || undefined,
|
||||
country: newLocation.country,
|
||||
postal_code: newLocation.postal_code || undefined,
|
||||
latitude: parseFloat(newLocation.latitude?.toString() || '0'),
|
||||
longitude: parseFloat(newLocation.longitude?.toString() || '0'),
|
||||
timezone: newLocation.timezone || undefined,
|
||||
};
|
||||
}
|
||||
// Build location data object (no database operations)
|
||||
const locationData: SelectedLocation = {
|
||||
name: locationName,
|
||||
city: city || undefined,
|
||||
state_province: result.address.state || undefined,
|
||||
country: result.address.country || '',
|
||||
postal_code: result.address.postcode || undefined,
|
||||
latitude,
|
||||
longitude,
|
||||
timezone: undefined, // Will be set by server during approval if needed
|
||||
display_name: result.display_name,
|
||||
};
|
||||
|
||||
setSelectedLocation(locationData);
|
||||
setSearchQuery('');
|
||||
|
||||
Reference in New Issue
Block a user