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 @@

Start a New Plan

- +
{#if destinationError}

Please enter your destination

@@ -229,7 +261,7 @@
- {#each friends as email} + {#each tripmates as email}
diff --git a/src/lib/components/PlaceCard.svelte b/src/lib/components/PlaceCard.svelte index b6baf44..450762f 100644 --- a/src/lib/components/PlaceCard.svelte +++ b/src/lib/components/PlaceCard.svelte @@ -10,7 +10,7 @@ const defaultPlace: Omit = { name: 'PlaceName', - image: 'placeholder.jpeg', + image: '/placeholder.jpeg', time: 'Add Time' }; diff --git a/src/lib/components/ProfilePicture.svelte b/src/lib/components/ProfilePicture.svelte index 6d3b2ac..92df273 100644 --- a/src/lib/components/ProfilePicture.svelte +++ b/src/lib/components/ProfilePicture.svelte @@ -1,32 +1,55 @@ - -
- {#if image} - - {:else} - - {/if} +
+ {#each Array(friends) as _, i} +
+ {#if images[i]} + Profile + {:else} +
+ +
+ {/if} +
+ {/each}
\ No newline at end of file diff --git a/src/lib/components/TripCard.svelte b/src/lib/components/TripCard.svelte index 0aa6f74..55d8918 100644 --- a/src/lib/components/TripCard.svelte +++ b/src/lib/components/TripCard.svelte @@ -6,7 +6,7 @@
-
+
{#if !image}
diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 919fecf..c72671f 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -29,7 +29,7 @@ goto('/trips')} /> - + \ No newline at end of file diff --git a/src/routes/trips/+page.svelte b/src/routes/trips/+page.svelte index 530d91d..4411cfd 100644 --- a/src/routes/trips/+page.svelte +++ b/src/routes/trips/+page.svelte @@ -4,29 +4,65 @@ import Button from '$lib/components/Button.svelte'; import NewTripPopup from '$lib/components/NewTripPopup.svelte'; import Nav from '$lib/components/Nav.svelte'; + import { onMount } from 'svelte'; + import { ref, onValue } from 'firebase/database'; + import { db } from '../../firebase'; interface Trip { - destination: string; + tid: string; + destination: { + name: string; + photo: string; + formatted_address: string; + location: { + lat: number; + lng: number; + } + }; startDate: string; endDate: string; - imageUrl: string; + tripmates: string[]; + created_at: string; } let activeTab = "Ongoing Trips"; let showNewTripPopup = false; let contentContainer: HTMLElement; - // Sample data, replace with actual data later - const sample_trip = { - destination: "Taiwan", - startDate: "04.27.2025", - endDate: "04.30.2025", - imageUrl: "" - } - let ongoingTrips = Array(3).fill(sample_trip); - - // let pastTrips: Trip[] = []; - let pastTrips = Array(14).fill(sample_trip); + let ongoingTrips: Trip[] = []; + let pastTrips: Trip[] = []; + + onMount(() => { + // Reference to the trips node + const tripsRef = ref(db, 'trips'); + + // Listen for changes in the trips data + onValue(tripsRef, (snapshot) => { + const trips: Trip[] = []; + snapshot.forEach((childSnapshot) => { + trips.push({ + tid: childSnapshot.key, + ...childSnapshot.val() + }); + }); + + console.log(trips); + // Get today's date at midnight for comparison + const today = new Date(); + today.setHours(0, 0, 0, 0); + + // Filter trips based on end date + ongoingTrips = trips.filter(trip => { + const endDate = new Date(trip.endDate); + return endDate >= today; + }).sort((a, b) => new Date(a.startDate).getTime() - new Date(b.startDate).getTime()); + + pastTrips = trips.filter(trip => { + const endDate = new Date(trip.endDate); + return endDate < today; + }).sort((a, b) => new Date(b.endDate).getTime() - new Date(a.endDate).getTime()); // Sort past trips by most recent first + }); + }); function handleNewTrip() { showNewTripPopup = true; @@ -71,7 +107,12 @@ {:else}
{#each ongoingTrips as trip} - + {/each}
{/if} @@ -83,7 +124,12 @@ {:else}
{#each pastTrips as trip} - + {/each}
{/if} @@ -95,7 +141,7 @@
- +