fix memory path in db
This commit is contained in:
parent
6325ec86fe
commit
32c0b6135e
|
@ -8,9 +8,8 @@
|
||||||
import { db } from '../../firebase';
|
import { db } from '../../firebase';
|
||||||
|
|
||||||
export let showPopup = false;
|
export let showPopup = false;
|
||||||
export let onAddMemory = () => {};
|
// export let onAddMemory = () => {};
|
||||||
export let onCancel = () => {};
|
export let onCancel = () => {};
|
||||||
export let tid: string;
|
|
||||||
|
|
||||||
let startDate = '';
|
let startDate = '';
|
||||||
let endDate = '';
|
let endDate = '';
|
||||||
|
@ -28,22 +27,24 @@
|
||||||
|
|
||||||
let tripOptions: { value: string; label: string }[] = [];
|
let tripOptions: { value: string; label: string }[] = [];
|
||||||
|
|
||||||
////////// THIS PART - load destination, startDate, and endDate from previous trip ////////
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
|
// reference to the trips node
|
||||||
const tripsRef = ref(db, 'trips');
|
const tripsRef = ref(db, 'trips');
|
||||||
onValue(tripsRef, snapshot => {
|
|
||||||
|
// listen for changes in the trips data
|
||||||
|
onValue(tripsRef, (snapshot) => {
|
||||||
const options: { value: string; label: string }[] = [];
|
const options: { value: string; label: string }[] = [];
|
||||||
snapshot.forEach(child => {
|
snapshot.forEach(child => {
|
||||||
const val = child.val();
|
const val = child.val();
|
||||||
const tripId = child.key;
|
const tripId = child.key;
|
||||||
const { name } = val.destination;
|
const { name } = val.destination;
|
||||||
const start = new Date(val.startDate);
|
const start = new Date(val.startDate);
|
||||||
const end = new Date(val.endDate);
|
const end = new Date(val.endDate);
|
||||||
const format = (d: Date) => `${String(d.getMonth() + 1).padStart(2, '0')}.${String(d.getDate()).padStart(2, '0')}`;
|
const format = (d: Date) => `${String(d.getMonth() + 1).padStart(2, '0')}/${String(d.getDate()).padStart(2, '0')}/${String(d.getFullYear())}`;
|
||||||
options.push({
|
options.push({
|
||||||
value: tripId,
|
value: tripId,
|
||||||
label: `${name} (${format(start)} - ${format(end)})`
|
label: `${name} (${format(start)} - ${format(end)})`
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
tripOptions = options;
|
tripOptions = options;
|
||||||
});
|
});
|
||||||
|
@ -64,8 +65,9 @@
|
||||||
const tripRef = ref(db, `trips/${selectedTripId}`);
|
const tripRef = ref(db, `trips/${selectedTripId}`);
|
||||||
onValue(tripRef, (snapshot) => {
|
onValue(tripRef, (snapshot) => {
|
||||||
const val = snapshot.val();
|
const val = snapshot.val();
|
||||||
startDate = val.startDate;
|
startDate = new Date(val.startDate).toLocaleDateString('en-GB', { day: '2-digit', month: '2-digit', year: 'numeric' });
|
||||||
endDate = val.endDate;
|
endDate = new Date(val.endDate).toLocaleDateString('en-GB', { day: '2-digit', month: '2-digit', year: 'numeric' });
|
||||||
|
console.log(`startDate: ${startDate}, endDate: ${endDate}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -159,6 +161,7 @@
|
||||||
|
|
||||||
const finalLocation = isCustomLocation() ? customLocation : selectedLocation;
|
const finalLocation = isCustomLocation() ? customLocation : selectedLocation;
|
||||||
|
|
||||||
|
console.log(`startDate = ${startDate}, endDate = ${endDate}, location = ${location}`)
|
||||||
const newMemory = {
|
const newMemory = {
|
||||||
location: finalLocation,
|
location: finalLocation,
|
||||||
startDate,
|
startDate,
|
||||||
|
@ -166,20 +169,21 @@
|
||||||
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 memoryRef = ref(db, `trips/${tid}/memories`);
|
console.log(`tid = ${selectedTripId}`);
|
||||||
|
const memoryRef = ref(db, `trips/${selectedTripId}/memories`);
|
||||||
const newMemory = {
|
const newMemory = {
|
||||||
location: finalLocation,
|
location: finalLocation,
|
||||||
startDate,
|
startDate: startDate,
|
||||||
endDate,
|
endDate: endDate,
|
||||||
|
// TODO: change this to use non-local URL
|
||||||
images: images.map(file => URL.createObjectURL(file)),
|
images: images.map(file => URL.createObjectURL(file)),
|
||||||
createdAt: new Date().toISOString()
|
createdAt: new Date().toISOString()
|
||||||
};
|
};
|
||||||
const addedRef = await push(memoryRef, newMemory);
|
const addedRef = await push(memoryRef, newMemory);
|
||||||
|
|
||||||
reset();
|
reset();
|
||||||
goto(`/viewimage?id=${addedRef.key}`);
|
goto(`/viewimage/${selectedTripId}/${addedRef.key}`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error saving memory:', error);
|
console.error('Error saving memory:', error);
|
||||||
}
|
}
|
||||||
|
@ -206,7 +210,6 @@
|
||||||
|
|
||||||
<div class="input-form">
|
<div class="input-form">
|
||||||
<label for="location">Load from past trips</label>
|
<label for="location">Load from past trips</label>
|
||||||
<!-- THIS PART - binding informaions to dropdown options-->
|
|
||||||
<select bind:value={selectedTripId}>
|
<select bind:value={selectedTripId}>
|
||||||
<option value="" disabled>Select location</option>
|
<option value="" disabled>Select location</option>
|
||||||
{#each tripOptions as opt}
|
{#each tripOptions as opt}
|
||||||
|
|
29
src/lib/constants/Interfaces.ts
Normal file
29
src/lib/constants/Interfaces.ts
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
// the trip data structure saved in the database
|
||||||
|
export interface Trip {
|
||||||
|
tid: string;
|
||||||
|
destination: {
|
||||||
|
name: string;
|
||||||
|
photo: string;
|
||||||
|
formatted_address: string;
|
||||||
|
location: {
|
||||||
|
lat: number;
|
||||||
|
lng: number;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
startDate: string;
|
||||||
|
endDate: string;
|
||||||
|
tripmates: string[];
|
||||||
|
created_at: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// the place data structure saved in the database
|
||||||
|
export interface Place {
|
||||||
|
name: string;
|
||||||
|
desc?: string;
|
||||||
|
image?: string;
|
||||||
|
time?: string;
|
||||||
|
geometry?: {
|
||||||
|
lat: number;
|
||||||
|
lng: number;
|
||||||
|
};
|
||||||
|
}
|
|
@ -22,6 +22,7 @@
|
||||||
import RecommendationPopup from '$lib/components/RecommendationPopup.svelte';
|
import RecommendationPopup from '$lib/components/RecommendationPopup.svelte';
|
||||||
import LoadingOverlay from '$lib/components/LoadingOverlay.svelte';
|
import LoadingOverlay from '$lib/components/LoadingOverlay.svelte';
|
||||||
import TurnIntoItineraryPopup from '$lib/components/TurnIntoItineraryPopup.svelte';
|
import TurnIntoItineraryPopup from '$lib/components/TurnIntoItineraryPopup.svelte';
|
||||||
|
import type { Place } from '$lib/constants/Interfaces';
|
||||||
|
|
||||||
let tripData: any = null;
|
let tripData: any = null;
|
||||||
let tripDates: string[] = [];
|
let tripDates: string[] = [];
|
||||||
|
@ -30,18 +31,6 @@
|
||||||
let showTurnIntoItineraryPopup = false;
|
let showTurnIntoItineraryPopup = false;
|
||||||
let isDistributingPlaces = false;
|
let isDistributingPlaces = false;
|
||||||
|
|
||||||
// the place data structure saved in the database
|
|
||||||
interface Place {
|
|
||||||
name: string;
|
|
||||||
desc?: string;
|
|
||||||
image?: string;
|
|
||||||
time?: string;
|
|
||||||
geometry?: {
|
|
||||||
lat: number;
|
|
||||||
lng: number;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
interface DatePlaces {
|
interface DatePlaces {
|
||||||
placesPlanned: Place[];
|
placesPlanned: Place[];
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,23 +7,7 @@
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import { ref, onValue } from 'firebase/database';
|
import { ref, onValue } from 'firebase/database';
|
||||||
import { db } from '../../firebase';
|
import { db } from '../../firebase';
|
||||||
|
import type { Trip } from '$lib/constants/Interfaces';
|
||||||
interface Trip {
|
|
||||||
tid: string;
|
|
||||||
destination: {
|
|
||||||
name: string;
|
|
||||||
photo: string;
|
|
||||||
formatted_address: string;
|
|
||||||
location: {
|
|
||||||
lat: number;
|
|
||||||
lng: number;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
startDate: string;
|
|
||||||
endDate: string;
|
|
||||||
tripmates: string[];
|
|
||||||
created_at: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
let activeTab = "Ongoing Trips";
|
let activeTab = "Ongoing Trips";
|
||||||
let showNewTripPopup = false;
|
let showNewTripPopup = false;
|
||||||
|
|
|
@ -2,26 +2,34 @@
|
||||||
import '../../app.css';
|
import '../../app.css';
|
||||||
import Nav from '$lib/components/Nav.svelte';
|
import Nav from '$lib/components/Nav.svelte';
|
||||||
import Button from '$lib/components/Button.svelte';
|
import Button from '$lib/components/Button.svelte';
|
||||||
import { page } from '$app/stores';
|
import { page } from '$app/state';
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import { getDatabase, ref, get } from 'firebase/database';
|
import { ref, get } from 'firebase/database';
|
||||||
import { db } from '../../firebase';
|
import { db } from '../../firebase';
|
||||||
|
|
||||||
let memoryId = '';
|
let memoryId = '';
|
||||||
|
/**
|
||||||
|
* @type {{ location: any; startDate: any; endDate: any; images: any; } | null}
|
||||||
|
*/
|
||||||
let memory = null;
|
let memory = null;
|
||||||
|
let tripId = '';
|
||||||
|
|
||||||
$: {
|
$: {
|
||||||
const q = $page.url.searchParams;
|
tripId = page.params.tripId;
|
||||||
memoryId = q.get('id') ?? '';
|
memoryId = page.params.memoryId;
|
||||||
}
|
}
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
if (memoryId) {
|
if (tripId && memoryId) {
|
||||||
const snapshot = await get(ref(db, `memories/${memoryId}`));
|
try {
|
||||||
if (snapshot.exists()) {
|
const snapshot = await get(ref(db, `trips/${tripId}/memories/${memoryId}`));
|
||||||
memory = snapshot.val();
|
if (snapshot.exists()) {
|
||||||
} else {
|
memory = snapshot.val();
|
||||||
console.error('No memory found');
|
} else {
|
||||||
|
console.error('No memory found');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching memory:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -50,6 +58,7 @@
|
||||||
|
|
||||||
<div class="image-list">
|
<div class="image-list">
|
||||||
{#each memory.images as img}
|
{#each memory.images as img}
|
||||||
|
<!-- svelte-ignore a11y_img_redundant_alt -->
|
||||||
<img src={typeof img === 'string' ? img : URL.createObjectURL(img)} alt="Memory image" />
|
<img src={typeof img === 'string' ? img : URL.createObjectURL(img)} alt="Memory image" />
|
||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
|
|
125
src/routes/viewimage/[tripId]/[memoryId]/+page.svelte
Normal file
125
src/routes/viewimage/[tripId]/[memoryId]/+page.svelte
Normal file
|
@ -0,0 +1,125 @@
|
||||||
|
<script>
|
||||||
|
import '../../../../app.css';
|
||||||
|
import Nav from '$lib/components/Nav.svelte';
|
||||||
|
import Button from '$lib/components/Button.svelte';
|
||||||
|
import { page } from '$app/state';
|
||||||
|
import { onMount } from 'svelte';
|
||||||
|
import { ref, get } from 'firebase/database';
|
||||||
|
import { db } from '../../../../firebase';
|
||||||
|
|
||||||
|
let memoryId = '';
|
||||||
|
/**
|
||||||
|
* @type {{ location: any; startDate: any; endDate: any; images: any; } | null}
|
||||||
|
*/
|
||||||
|
let memory = null;
|
||||||
|
let tripId = '';
|
||||||
|
|
||||||
|
// Subscribe to page store to get URL parameters
|
||||||
|
$: {
|
||||||
|
tripId = page.params.tripId;
|
||||||
|
memoryId = page.params.memoryId;
|
||||||
|
}
|
||||||
|
|
||||||
|
onMount(async () => {
|
||||||
|
if (tripId && memoryId) {
|
||||||
|
try {
|
||||||
|
const snapshot = await get(ref(db, `trips/${tripId}/memories/${memoryId}`));
|
||||||
|
if (snapshot.exists()) {
|
||||||
|
memory = snapshot.val();
|
||||||
|
} else {
|
||||||
|
console.error('No memory found');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching memory:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let gradientColors = ['#e74c3c', '#f1c40f', '#2ecc71', '#3498db', '#9b59b6'];
|
||||||
|
$: gradientStyle = `
|
||||||
|
conic-gradient(
|
||||||
|
${gradientColors.map((c, i) => `${c} ${i * 72}deg ${(i + 1) * 72}deg`).join(',')}
|
||||||
|
)
|
||||||
|
`;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<main>
|
||||||
|
<Nav activeTab="MyMemory" darkMode={true}/>
|
||||||
|
|
||||||
|
<div class="content">
|
||||||
|
{#if memory}
|
||||||
|
<div class="header">
|
||||||
|
<h1>{memory.location}</h1>
|
||||||
|
<p>{memory.startDate} - {memory.endDate}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="wheel-container">
|
||||||
|
<div class="gradient-wheel" style="background-image: {gradientStyle};"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="image-list">
|
||||||
|
{#each memory.images as img}
|
||||||
|
<!-- svelte-ignore a11y_img_redundant_alt -->
|
||||||
|
<img src={typeof img === 'string' ? img : URL.createObjectURL(img)} alt="Memory image" />
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
|
{:else}
|
||||||
|
<p class="empty">Loading memory...</p>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
main {
|
||||||
|
height: 100vh;
|
||||||
|
background-color: var(--black);
|
||||||
|
font-family: 'Inter', sans-serif;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
.content {
|
||||||
|
flex: 1;
|
||||||
|
padding: 2rem 1rem;
|
||||||
|
overflow-y: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.header {
|
||||||
|
text-align: center;
|
||||||
|
color: var(--white);
|
||||||
|
margin-bottom: 2rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gradient-wheel {
|
||||||
|
width: 300px;
|
||||||
|
height: 300px;
|
||||||
|
margin: 2rem auto;
|
||||||
|
border-radius: 50%;
|
||||||
|
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
|
||||||
|
}
|
||||||
|
|
||||||
|
.wheel-container {
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.image-list {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
gap: 1rem;
|
||||||
|
margin-top: 2rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.image-list img {
|
||||||
|
width: 80%;
|
||||||
|
max-width: 500px;
|
||||||
|
border-radius: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.empty {
|
||||||
|
color: var(--gray-400);
|
||||||
|
text-align: center;
|
||||||
|
margin-top: 4rem;
|
||||||
|
}
|
||||||
|
</style>
|
Loading…
Reference in New Issue
Block a user