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: [] });