collections.ts (hoppscotch-2.0.0) | : | collections.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 { | |||
restCollections$, | restCollections$, | |||
graphqlCollections$, | graphqlCollections$, | |||
setRESTCollections, | setRESTCollections, | |||
setGraphqlCollections, | setGraphqlCollections, | |||
translateToNewRESTCollection, | translateToNewRESTCollection, | |||
translateToNewGQLCollection, | translateToNewGQLCollection, | |||
} from "~/newstore/collections" | } from "~/newstore/collections" | |||
import { settingsStore } from "~/newstore/settings" | import { settingsStore } from "~/newstore/settings" | |||
skipping to change at line 52 | skipping to change at line 57 | |||
const cl = { | const cl = { | |||
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, | |||
collection, | collection, | |||
} | } | |||
try { | try { | |||
await firebase | await setDoc( | |||
.firestore() | doc(getFirestore(), "users", currentUser$.value.uid, flag, "sync"), | |||
.collection("users") | cl | |||
.doc(currentUser$.value.uid) | ) | |||
.collection(flag) | ||||
.doc("sync") | ||||
.set(cl) | ||||
} catch (e) { | } catch (e) { | |||
console.error("error updating", cl, e) | console.error("error updating", cl, e) | |||
throw e | throw e | |||
} | } | |||
} | } | |||
export function initCollections() { | export function initCollections() { | |||
restCollections$.subscribe((collections) => { | restCollections$.subscribe((collections) => { | |||
if ( | if ( | |||
loadedRESTCollections && | loadedRESTCollections && | |||
skipping to change at line 101 | skipping to change at line 103 | |||
if (restSnapshotStop) { | if (restSnapshotStop) { | |||
restSnapshotStop() | restSnapshotStop() | |||
restSnapshotStop = null | restSnapshotStop = null | |||
} | } | |||
if (graphqlSnapshotStop) { | if (graphqlSnapshotStop) { | |||
graphqlSnapshotStop() | graphqlSnapshotStop() | |||
graphqlSnapshotStop = null | graphqlSnapshotStop = null | |||
} | } | |||
} else { | } else { | |||
restSnapshotStop = firebase | restSnapshotStop = onSnapshot( | |||
.firestore() | collection(getFirestore(), "users", user.uid, "collections"), | |||
.collection("users") | (collectionsRef) => { | |||
.doc(user.uid) | ||||
.collection("collections") | ||||
.onSnapshot((collectionsRef) => { | ||||
const collections: any[] = [] | const collections: any[] = [] | |||
collectionsRef.forEach((doc) => { | collectionsRef.forEach((doc) => { | |||
const collection = doc.data() | const collection = doc.data() | |||
collection.id = doc.id | collection.id = doc.id | |||
collections.push(collection) | collections.push(collection) | |||
}) | }) | |||
// Prevent infinite ping-pong of updates | // Prevent infinite ping-pong of updates | |||
loadedRESTCollections = false | loadedRESTCollections = false | |||
// TODO: Wth is with collections[0] | // TODO: Wth is with collections[0] | |||
if (collections.length > 0) { | if (collections.length > 0) { | |||
setRESTCollections( | setRESTCollections( | |||
(collections[0].collection ?? []).map( | (collections[0].collection ?? []).map( | |||
translateToNewRESTCollection | translateToNewRESTCollection | |||
) | ) | |||
) | ) | |||
} | } | |||
loadedRESTCollections = true | loadedRESTCollections = true | |||
}) | } | |||
) | ||||
graphqlSnapshotStop = firebase | graphqlSnapshotStop = onSnapshot( | |||
.firestore() | collection(getFirestore(), "users", user.uid, "collectionsGraphql"), | |||
.collection("users") | (collectionsRef) => { | |||
.doc(user.uid) | ||||
.collection("collectionsGraphql") | ||||
.onSnapshot((collectionsRef) => { | ||||
const collections: any[] = [] | const collections: any[] = [] | |||
collectionsRef.forEach((doc) => { | collectionsRef.forEach((doc) => { | |||
const collection = doc.data() | const collection = doc.data() | |||
collection.id = doc.id | collection.id = doc.id | |||
collections.push(collection) | collections.push(collection) | |||
}) | }) | |||
// Prevent infinite ping-pong of updates | // Prevent infinite ping-pong of updates | |||
loadedGraphqlCollections = false | loadedGraphqlCollections = false | |||
// TODO: Wth is with collections[0] | // TODO: Wth is with collections[0] | |||
if (collections.length > 0) { | if (collections.length > 0) { | |||
setGraphqlCollections( | setGraphqlCollections( | |||
(collections[0].collection ?? []).map(translateToNewGQLCollection) | (collections[0].collection ?? []).map(translateToNewGQLCollection) | |||
) | ) | |||
} | } | |||
loadedGraphqlCollections = true | loadedGraphqlCollections = true | |||
}) | } | |||
) | ||||
} | } | |||
}) | }) | |||
} | } | |||
End of changes. 6 change blocks. | ||||
23 lines changed or deleted | 21 lines changed or added |