updated comunication with firebase

This commit is contained in:
2026-06-16 16:18:28 +09:00
parent 36f0c25721
commit 0a823948df
9 changed files with 22 additions and 66 deletions

View File

@@ -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
View 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 {};

View File

@@ -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);
}

View File

@@ -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();

View File

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

View File

@@ -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(() => {

View File

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

View File

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

View File

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