settings.ts (hoppscotch-2.0.0) | : | settings.ts (hoppscotch-2.1.0) | ||
---|---|---|---|---|
import firebase from "firebase/app" | import { | |||
import "firebase/firestore" | collection, | |||
doc, | ||||
getFirestore, | ||||
onSnapshot, | ||||
setDoc, | ||||
} from "@firebase/firestore" | ||||
import { currentUser$ } from "./auth" | import { currentUser$ } from "./auth" | |||
import { applySetting, settingsStore, SettingsType } from "~/newstore/settings" | import { applySetting, settingsStore, SettingsType } from "~/newstore/settings" | |||
/** | /** | |||
* Used locally to prevent infinite loop when settings sync update | * Used locally to prevent infinite loop when settings sync update | |||
* is applied to the store which then fires the store sync listener. | * is applied to the store which then fires the store sync listener. | |||
* When you want to update settings and not want to fire the update listener, | * When you want to update settings and not want to fire the update listener, | |||
* set this to true and then set it back to false once it is done | * set this to true and then set it back to false once it is done | |||
*/ | */ | |||
let loadedSettings = false | let loadedSettings = false | |||
skipping to change at line 31 | skipping to change at line 36 | |||
const st = { | const st = { | |||
updatedOn: new Date(), | updatedOn: new Date(), | |||
author: currentUser$.value.uid, | author: currentUser$.value.uid, | |||
author_name: currentUser$.value.displayName, | author_name: currentUser$.value.displayName, | |||
author_image: currentUser$.value.photoURL, | author_image: currentUser$.value.photoURL, | |||
name: setting, | name: setting, | |||
value, | value, | |||
} | } | |||
try { | try { | |||
await firebase | await setDoc( | |||
.firestore() | doc(getFirestore(), "users", currentUser$.value.uid, "settings", setting), | |||
.collection("users") | st | |||
.doc(currentUser$.value.uid) | ) | |||
.collection("settings") | ||||
.doc(setting) | ||||
.set(st) | ||||
} catch (e) { | } catch (e) { | |||
console.error("error updating", st, e) | console.error("error updating", st, e) | |||
throw e | throw e | |||
} | } | |||
} | } | |||
export function initSettings() { | export function initSettings() { | |||
settingsStore.dispatches$.subscribe((dispatch) => { | settingsStore.dispatches$.subscribe((dispatch) => { | |||
if (currentUser$.value && loadedSettings) { | if (currentUser$.value && loadedSettings) { | |||
if (dispatch.dispatcher === "bulkApplySettings") { | if (dispatch.dispatcher === "bulkApplySettings") { | |||
skipping to change at line 69 | skipping to change at line 71 | |||
let snapshotStop: (() => void) | null = null | let snapshotStop: (() => void) | null = null | |||
// Subscribe and unsubscribe event listeners | // Subscribe and unsubscribe event listeners | |||
currentUser$.subscribe((user) => { | currentUser$.subscribe((user) => { | |||
if (!user && snapshotStop) { | if (!user && snapshotStop) { | |||
// User logged out | // User logged out | |||
snapshotStop() | snapshotStop() | |||
snapshotStop = null | snapshotStop = null | |||
} else if (user) { | } else if (user) { | |||
snapshotStop = firebase | snapshotStop = onSnapshot( | |||
.firestore() | collection(getFirestore(), "users", user.uid, "settings"), | |||
.collection("users") | (settingsRef) => { | |||
.doc(user.uid) | ||||
.collection("settings") | ||||
.onSnapshot((settingsRef) => { | ||||
const settings: any[] = [] | const settings: any[] = [] | |||
settingsRef.forEach((doc) => { | settingsRef.forEach((doc) => { | |||
const setting = doc.data() | const setting = doc.data() | |||
setting.id = doc.id | setting.id = doc.id | |||
settings.push(setting) | settings.push(setting) | |||
}) | }) | |||
loadedSettings = false | loadedSettings = false | |||
settings.forEach((e) => { | settings.forEach((e) => { | |||
if (e && e.name && e.value != null) { | if (e && e.name && e.value != null) { | |||
applySetting(e.name, e.value) | applySetting(e.name, e.value) | |||
} | } | |||
}) | }) | |||
loadedSettings = true | loadedSettings = true | |||
}) | } | |||
) | ||||
} | } | |||
}) | }) | |||
} | } | |||
End of changes. 4 change blocks. | ||||
16 lines changed or deleted | 16 lines changed or added |