environments.ts (hoppscotch-2.0.0) | : | environments.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 { | import { | |||
Environment, | Environment, | |||
environments$, | environments$, | |||
globalEnv$, | globalEnv$, | |||
replaceEnvironments, | replaceEnvironments, | |||
setGlobalEnvVariables, | setGlobalEnvVariables, | |||
} from "~/newstore/environments" | } from "~/newstore/environments" | |||
import { settingsStore } from "~/newstore/settings" | import { settingsStore } from "~/newstore/settings" | |||
skipping to change at line 42 | skipping to change at line 47 | |||
const ev = { | const ev = { | |||
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, | |||
environment, | environment, | |||
} | } | |||
try { | try { | |||
await firebase | await setDoc( | |||
.firestore() | doc( | |||
.collection("users") | getFirestore(), | |||
.doc(currentUser$.value.uid) | "users", | |||
.collection("environments") | currentUser$.value.uid, | |||
.doc("sync") | "environments", | |||
.set(ev) | "sync" | |||
), | ||||
ev | ||||
) | ||||
} catch (e) { | } catch (e) { | |||
console.error("error updating", ev, e) | console.error("error updating", ev, e) | |||
throw e | throw e | |||
} | } | |||
} | } | |||
async function writeGlobalEnvironment(variables: Environment["variables"]) { | async function writeGlobalEnvironment(variables: Environment["variables"]) { | |||
if (currentUser$.value == null) | if (currentUser$.value == null) | |||
throw new Error("Cannot write global environment when signed out") | throw new Error("Cannot write global environment when signed out") | |||
const ev = { | const ev = { | |||
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, | |||
variables, | variables, | |||
} | } | |||
try { | try { | |||
await firebase | await setDoc( | |||
.firestore() | doc(getFirestore(), "users", currentUser$.value.uid, "globalEnv", "sync"), | |||
.collection("users") | ev | |||
.doc(currentUser$.value.uid) | ) | |||
.collection("globalEnv") | ||||
.doc("sync") | ||||
.set(ev) | ||||
} catch (e) { | } catch (e) { | |||
console.error("error updating", ev, e) | console.error("error updating", ev, e) | |||
throw e | throw e | |||
} | } | |||
} | } | |||
export function initEnvironments() { | export function initEnvironments() { | |||
environments$.subscribe((envs) => { | environments$.subscribe((envs) => { | |||
if ( | if ( | |||
currentUser$.value && | currentUser$.value && | |||
skipping to change at line 118 | skipping to change at line 123 | |||
if (envSnapshotStop) { | if (envSnapshotStop) { | |||
envSnapshotStop() | envSnapshotStop() | |||
envSnapshotStop = null | envSnapshotStop = null | |||
} | } | |||
if (globalsSnapshotStop) { | if (globalsSnapshotStop) { | |||
globalsSnapshotStop() | globalsSnapshotStop() | |||
globalsSnapshotStop = null | globalsSnapshotStop = null | |||
} | } | |||
} else if (user) { | } else if (user) { | |||
envSnapshotStop = firebase | envSnapshotStop = onSnapshot( | |||
.firestore() | collection(getFirestore(), "users", user.uid, "environments"), | |||
.collection("users") | (environmentsRef) => { | |||
.doc(user.uid) | ||||
.collection("environments") | ||||
.onSnapshot((environmentsRef) => { | ||||
const environments: any[] = [] | const environments: any[] = [] | |||
environmentsRef.forEach((doc) => { | environmentsRef.forEach((doc) => { | |||
const environment = doc.data() | const environment = doc.data() | |||
environment.id = doc.id | environment.id = doc.id | |||
environments.push(environment) | environments.push(environment) | |||
}) | }) | |||
loadedEnvironments = false | loadedEnvironments = false | |||
if (environments.length > 0) { | if (environments.length > 0) { | |||
replaceEnvironments(environments[0].environment) | replaceEnvironments(environments[0].environment) | |||
} | } | |||
loadedEnvironments = true | loadedEnvironments = true | |||
}) | } | |||
globalsSnapshotStop = firebase | ) | |||
.firestore() | globalsSnapshotStop = onSnapshot( | |||
.collection("users") | collection(getFirestore(), "users", user.uid, "globalEnv"), | |||
.doc(user.uid) | (globalsRef) => { | |||
.collection("globalEnv") | ||||
.onSnapshot((globalsRef) => { | ||||
if (globalsRef.docs.length === 0) { | if (globalsRef.docs.length === 0) { | |||
loadedGlobals = true | loadedGlobals = true | |||
return | return | |||
} | } | |||
const doc = globalsRef.docs[0].data() | const doc = globalsRef.docs[0].data() | |||
loadedGlobals = false | loadedGlobals = false | |||
setGlobalEnvVariables(doc.variables) | setGlobalEnvVariables(doc.variables) | |||
loadedGlobals = true | loadedGlobals = true | |||
}) | } | |||
) | ||||
} | } | |||
}) | }) | |||
} | } | |||
End of changes. 6 change blocks. | ||||
30 lines changed or deleted | 31 lines changed or added |