diff --git a/src/lib/components/AddPlaces.svelte b/src/lib/components/AddPlaces.svelte index e108220..e04553d 100644 --- a/src/lib/components/AddPlaces.svelte +++ b/src/lib/components/AddPlaces.svelte @@ -63,15 +63,19 @@ if (place && place.name) { // If the place has photos, get the URL for the first photo if (place.photos && place.photos.length > 0) { - const photoOptions = { - maxWidth: 400, - maxHeight: 300 - }; - // Get the photo URL - console.log(place.photos[0]); - const photoUrl = place.photos[0].getUrl(photoOptions); - place.photoUrl = photoUrl; - console.log(place.photoUrl); + try { + const photoOptions = { + maxWidth: 400, + maxHeight: 300 + }; + place.photoUrl = place.photos[0].getUrl(photoOptions); + } catch (error) { + console.error('Error getting photo URL:', error); + place.photoUrl = '/placeholder.jpeg'; + } + } + else { + place.photoUrl = '/placeholder.jpeg'; } selectedPlace = place; diff --git a/src/lib/components/ItineraryDate.svelte b/src/lib/components/ItineraryDate.svelte index 14934c4..622e425 100644 --- a/src/lib/components/ItineraryDate.svelte +++ b/src/lib/components/ItineraryDate.svelte @@ -16,7 +16,7 @@ const newPlace = { name: place.name || 'Unknown Place', desc: place.formatted_address || '', - image: (place as any).photoUrl || 'placeholder.jpeg', + image: (place as any).photoUrl || '/placeholder.jpeg', time: 'Add Time' }; diff --git a/src/lib/components/NewTripPopup.svelte b/src/lib/components/NewTripPopup.svelte index 45be26b..71e838b 100644 --- a/src/lib/components/NewTripPopup.svelte +++ b/src/lib/components/NewTripPopup.svelte @@ -3,16 +3,18 @@ import { goto } from '$app/navigation'; import { onMount } from 'svelte'; import { Loader } from '@googlemaps/js-api-loader'; + import { ref, child, get, set, onValue, push } from 'firebase/database'; + import { db } from '../../firebase'; import Button from './Button.svelte'; export let showPopup = false; - export let fromPage = 'home'; let destination = ""; + let selectedPlace: any; let lastSelectedPlaceName = ""; let startDate = ""; let endDate = ""; - let friends: string[] = []; + let tripmates: string[] = []; let currentEmail = ""; let destinationError = false; let startDateError = false; @@ -74,13 +76,14 @@ autocomplete = new google.maps.places.Autocomplete(input, { types: ['(regions)'] }); - autocomplete.setFields(['name', 'formatted_address']); + autocomplete.setFields(['name', 'formatted_address', 'photos', 'place_id', 'geometry']); autocomplete.addListener('place_changed', () => { if (!autocomplete) return; const place = autocomplete.getPlace(); if (place.name) { destination = place.name; + selectedPlace = place; lastSelectedPlaceName = input.value.trim(); destinationError = false; } @@ -100,7 +103,7 @@ destinationError = false; destination = ""; } - }, 200); + }, 400); }); } @@ -114,15 +117,15 @@ event.preventDefault(); const email = currentEmail.trim(); - if (email && isValidEmail(email) && !friends.includes(email)) { - friends = [...friends, email]; + if (email && isValidEmail(email) && !tripmates.includes(email)) { + tripmates = [...tripmates, email]; currentEmail = ""; } } } function removeEmail(emailToRemove: string) { - friends = friends.filter(email => email !== emailToRemove); + tripmates = tripmates.filter(email => email !== emailToRemove); } function handleCancel() { @@ -130,7 +133,7 @@ destination = ""; startDate = ""; endDate = ""; - friends = []; + tripmates = []; currentEmail = ""; destinationError = false; startDateError = false; @@ -142,7 +145,7 @@ } } - function handleStart() { + async function handleStart() { destinationError = !destination; startDateError = !startDate; endDateError = !endDate; @@ -162,12 +165,41 @@ } if (destinationError || startDateError || endDateError) { - // alert('Please fill in all required fields: Destination, Start Date, End Date'); return; } else { - goto(`/itinerary?from=${fromPage}`); - handleCancel(); + const tid = crypto.randomUUID(); + + // Extract required place details + const placeDetails = { + name: selectedPlace.name, + formatted_address: selectedPlace.formatted_address, + photo: selectedPlace.photos?.[0]?.getUrl(), + location: { + lat: selectedPlace.geometry.location.lat(), + lng: selectedPlace.geometry.location.lng() + } + }; + + const tripData = { + tid, + destination: placeDetails, + startDate, + endDate, + tripmates, + created_at: new Date().toISOString() + }; + + try { + // Create a new reference for this specific trip using its ID + const tripRef = ref(db, `trips/${tid}`); + await set(tripRef, tripData); + console.log(`Trip saved to db with ID: ${tid}`); + goto(`/itinerary/${tid}`); + handleCancel(); + } catch (error) { + console.error('Error saving trip:', error); + } } } @@ -182,7 +214,7 @@