updated comunication with firebase
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { db } from '../firebase.js';
|
||||
import { doc, onSnapshot, setDoc, updateDoc, arrayUnion, arrayRemove } from 'firebase/firestore';
|
||||
import { doc, onSnapshot, updateDoc, arrayUnion, arrayRemove } from 'firebase/firestore';
|
||||
|
||||
let selected = $state(new Set());
|
||||
let totalCountries = $state(0);
|
||||
@@ -20,25 +20,12 @@ export function initSelectionListener(uid) {
|
||||
});
|
||||
}
|
||||
|
||||
const visitedRef = doc(db, 'visited', 'countries');
|
||||
|
||||
onSnapshot(visitedRef, (snap) => {
|
||||
if (snap.exists()) {
|
||||
selected = new Set(snap.data().ids ?? []);
|
||||
}
|
||||
});
|
||||
|
||||
function persist() {
|
||||
setDoc(visitedRef, { ids: [...selected] });
|
||||
}
|
||||
|
||||
export function toggle(id) {
|
||||
const was = selected.has(id);
|
||||
const next = new Set(selected);
|
||||
if (was) next.delete(id);
|
||||
else next.add(id);
|
||||
selected = next;
|
||||
persist();
|
||||
if (_uid) {
|
||||
const userRef = doc(db, 'users', _uid);
|
||||
if (was) updateDoc(userRef, { visitedCountries: arrayRemove(id) });
|
||||
@@ -48,7 +35,6 @@ export function toggle(id) {
|
||||
|
||||
export function clearAll() {
|
||||
selected = new Set();
|
||||
persist();
|
||||
if (_uid) {
|
||||
const userRef = doc(db, 'users', _uid);
|
||||
updateDoc(userRef, { visitedCountries: [] });
|
||||
|
||||
15
src/lib/shared/types.js
Normal file
15
src/lib/shared/types.js
Normal file
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @typedef {{
|
||||
* id: string,
|
||||
* title: string,
|
||||
* date: string,
|
||||
* location: { country: string, cities: string[] },
|
||||
* photos: string[],
|
||||
* transport: 'flight' | 'train' | 'bus' | 'car' | 'ship' | 'walk',
|
||||
* tripType: 'solo' | 'friends' | 'family',
|
||||
* days: number,
|
||||
* memo: string
|
||||
* }} JournalEntry
|
||||
*/
|
||||
|
||||
export {};
|
||||
@@ -1,45 +0,0 @@
|
||||
import { writable } from 'svelte/store';
|
||||
import { db } from '../firebase.js';
|
||||
import {
|
||||
collection, onSnapshot, addDoc, updateDoc, deleteDoc, doc, serverTimestamp
|
||||
} from 'firebase/firestore';
|
||||
|
||||
/**
|
||||
* @typedef {{
|
||||
* id: string,
|
||||
* title: string,
|
||||
* date: string,
|
||||
* location: { country: string, cities: string[] },
|
||||
* photos: string[],
|
||||
* transport: 'flight' | 'train' | 'bus' | 'car' | 'ship' | 'walk',
|
||||
* tripType: 'solo' | 'friends' | 'family',
|
||||
* days: number,
|
||||
* memo: string
|
||||
* }} JournalEntry
|
||||
*/
|
||||
|
||||
export const journals = writable(/** @type {JournalEntry[]} */([]));
|
||||
export const journalsLoading = writable(true);
|
||||
|
||||
const entriesRef = collection(db, 'entries');
|
||||
|
||||
onSnapshot(entriesRef, (snap) => {
|
||||
journals.set(snap.docs.map(d => ({ id: d.id, ...d.data() })));
|
||||
journalsLoading.set(false);
|
||||
});
|
||||
|
||||
/** @param {Omit<JournalEntry, 'id'>} entry */
|
||||
export async function addJournal(entry) {
|
||||
await addDoc(entriesRef, { ...entry, createdAt: serverTimestamp() });
|
||||
}
|
||||
|
||||
/** @param {string} id */
|
||||
export async function removeJournal(id) {
|
||||
await deleteDoc(doc(db, 'entries', id));
|
||||
}
|
||||
|
||||
/** @param {JournalEntry} updated */
|
||||
export async function updateJournal(updated) {
|
||||
const { id, ...data } = updated;
|
||||
await updateDoc(doc(db, 'entries', id), data);
|
||||
}
|
||||
@@ -9,7 +9,7 @@
|
||||
/**
|
||||
* entry = null → "new entry" mode
|
||||
* entry = {...} → "edit" mode
|
||||
* @type {{ entry?: import('../stores/journalStore.js').JournalEntry | null, initialCountry?: string, onBack: () => void }}
|
||||
* @type {{ entry?: import('../shared/types.js').JournalEntry | null, initialCountry?: string, onBack: () => void }}
|
||||
*/
|
||||
let { entry = null, initialCountry = '', onBack } = $props();
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import { flagEmoji } from '../shared/countries.js';
|
||||
import DeleteConfirm from './DeleteConfirm.svelte';
|
||||
|
||||
/** @type {{ entry: import('../stores/journalStore.js').JournalEntry, onBack: () => void, onEdit: () => void }} */
|
||||
/** @type {{ entry: import('../shared/types.js').JournalEntry, onBack: () => void, onEdit: () => void }} */
|
||||
let { entry, onBack, onEdit } = $props();
|
||||
|
||||
let showDeleteConfirm = $state(false);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script>
|
||||
/** @type {{ entries: import('../stores/journalStore.js').JournalEntry[] }} */
|
||||
/** @type {{ entries: import('../shared/types.js').JournalEntry[] }} */
|
||||
let { entries } = $props();
|
||||
|
||||
let stats = $derived.by(() => {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<script>
|
||||
import { toPng } from 'html-to-image';
|
||||
|
||||
/** @type {{ entries: import('../stores/journalStore.js').JournalEntry[], onClose: () => void }} */
|
||||
/** @type {{ entries: import('../shared/types.js').JournalEntry[], onClose: () => void }} */
|
||||
let { entries, onClose } = $props();
|
||||
|
||||
let cardEl = $state(null);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script>
|
||||
/** @type {{ entries: import('../stores/journalStore.js').JournalEntry[], onClick: () => void }} */
|
||||
/** @type {{ entries: import('../shared/types.js').JournalEntry[], onClick: () => void }} */
|
||||
let { entries, onClick } = $props();
|
||||
|
||||
const continentMap = {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<script>
|
||||
import { flagEmoji } from '../shared/countries.js';
|
||||
|
||||
/** @type {{ entry: import('../stores/journalStore.js').JournalEntry, onClick: () => void }} */
|
||||
/** @type {{ entry: import('../shared/types.js').JournalEntry, onClick: () => void }} */
|
||||
let { entry, onClick } = $props();
|
||||
|
||||
function formatDate(/** @type {string} */ iso) {
|
||||
|
||||
Reference in New Issue
Block a user