problems with binding memory with trip

This commit is contained in:
Chaebean Yang 2025-06-07 14:17:04 +09:00
parent b73519c5db
commit 6325ec86fe

View File

@ -4,25 +4,18 @@
import { goto } from '$app/navigation'; import { goto } from '$app/navigation';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { Loader } from '@googlemaps/js-api-loader'; import { Loader } from '@googlemaps/js-api-loader';
import { collection, addDoc, getFirestore, Timestamp } from 'firebase/firestore'; import { ref, push, onValue } from 'firebase/database';
import { ref, push } from 'firebase/database';
import { db } from '../../firebase'; import { db } from '../../firebase';
export let showPopup = false; export let showPopup = false;
$: console.log('Popup received prop:', showPopup);
export let onAddMemory = () => {}; export let onAddMemory = () => {};
export let onCancel = () => {}; export let onCancel = () => {};
export let tid: string;
function handleCancelClick() {
onCancel();
reset();
}
let startDate = ''; let startDate = '';
let endDate = ''; let endDate = '';
let isGoogleLoaded = false; let isGoogleLoaded = false;
let dragActive = false; let dragActive = false;
let selectedLocation = '';
let customLocation = ''; let customLocation = '';
let customLocationInput: HTMLInputElement; let customLocationInput: HTMLInputElement;
let images: File[] = []; let images: File[] = [];
@ -30,6 +23,32 @@
let showImageError = false; let showImageError = false;
let hasAttemptedSubmit = false; let hasAttemptedSubmit = false;
let isFormValid = true; let isFormValid = true;
let selectedTripId = ''; //for dropdown
let selectedLocation = '';
let tripOptions: { value: string; label: string }[] = [];
////////// THIS PART - load destination, startDate, and endDate from previous trip ////////
onMount(() => {
const tripsRef = ref(db, 'trips');
onValue(tripsRef, snapshot => {
const options: { value: string; label: string }[] = [];
snapshot.forEach(child => {
const val = child.val();
const tripId = child.key;
const { name } = val.destination;
const start = new Date(val.startDate);
const end = new Date(val.endDate);
const format = (d: Date) => `${String(d.getMonth() + 1).padStart(2, '0')}.${String(d.getDate()).padStart(2, '0')}`;
options.push({
value: tripId,
label: `${name} (${format(start)} - ${format(end)})`
});
});
tripOptions = options;
});
});
$: if (hasAttemptedSubmit) { $: if (hasAttemptedSubmit) {
isFormValid = ( isFormValid = (
@ -37,6 +56,23 @@
images.length > 0 images.length > 0
); );
} }
$: if (selectedTripId && selectedTripId !== 'custom') {
const trip = tripOptions.find(t => t.value === selectedTripId);
if (trip) {
selectedLocation = trip.label.split(' (')[0]; // label에서 name 추출
const tripRef = ref(db, `trips/${selectedTripId}`);
onValue(tripRef, (snapshot) => {
const val = snapshot.val();
startDate = val.startDate;
endDate = val.endDate;
});
}
}
function isCustomLocation() {
return selectedTripId === 'custom';
}
const GOOGLE_PLACES_API_KEY = import.meta.env.VITE_GOOGLE_PLACES_API_KEY; const GOOGLE_PLACES_API_KEY = import.meta.env.VITE_GOOGLE_PLACES_API_KEY;
@ -80,6 +116,11 @@
} }
} }
} }
function handleCancelClick() {
onCancel();
reset();
}
function handleDrop(event: DragEvent) { function handleDrop(event: DragEvent) {
event.preventDefault(); event.preventDefault();
@ -104,10 +145,6 @@
} }
} }
function isCustomLocation() {
return selectedLocation === 'custom';
}
function removeImage(imageToRemove: File) { function removeImage(imageToRemove: File) {
images = images.filter(img => img !== imageToRemove); images = images.filter(img => img !== imageToRemove);
} }
@ -129,32 +166,39 @@
images: images.map(file => URL.createObjectURL(file)), images: images.map(file => URL.createObjectURL(file)),
createdAt: new Date().toISOString() createdAt: new Date().toISOString()
}; };
////////// THIS PART - add memory node on trips /////////
try { try {
const newMemoryRef = ref(db, 'memories'); const memoryRef = ref(db, `trips/${tid}/memories`);
const addedRef = await push(newMemoryRef, newMemory); const newMemory = {
location: finalLocation,
startDate,
endDate,
images: images.map(file => URL.createObjectURL(file)),
createdAt: new Date().toISOString()
};
const addedRef = await push(memoryRef, newMemory);
reset(); reset();
goto(`/viewimage?id=${addedRef.key}`); goto(`/viewimage?id=${addedRef.key}`);
} catch (error) { } catch (error) {
console.error('Error saving memory:', error); console.error('Error saving memory:', error);
} }
} }
function reset() { function reset() {
showPopup = false; showPopup = false;
selectedLocation = ''; selectedLocation = '';
customLocation = ''; customLocation = '';
images = []; images = [];
startDate = ''; startDate = '';
endDate = ''; endDate = '';
showLocationError = false; showLocationError = false;
showImageError = false; showImageError = false;
} }
const locations = ['Paris', 'Tokyo', 'New York']; const locations = ['Paris', 'Tokyo', 'New York'];
</script> </script>
{#if showPopup} {#if showPopup}
<div class="overlay"> <div class="overlay">
<div class="popup {showLocationError || showImageError ? 'error' : ''}"> <div class="popup {showLocationError || showImageError ? 'error' : ''}">
@ -162,13 +206,14 @@
<div class="input-form"> <div class="input-form">
<label for="location">Load from past trips</label> <label for="location">Load from past trips</label>
<select id="location" bind:value={selectedLocation}> <!-- THIS PART - binding informaions to dropdown options-->
<select bind:value={selectedTripId}>
<option value="" disabled>Select location</option> <option value="" disabled>Select location</option>
{#each locations as loc} {#each tripOptions as opt}
<option value={loc}>{loc}</option> <option value={opt.value}>{opt.label}</option>
{/each} {/each}
<option value="custom" class="custom-option">+ Enter custom trip</option> <option value="custom" class="custom-option">+ Enter custom trip</option>
</select> </select>
</div> </div>
{#if isCustomLocation()} {#if isCustomLocation()}