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: [] });
|
||||
|
||||
Reference in New Issue
Block a user