history.ts (hoppscotch-2.0.0) | : | history.ts (hoppscotch-2.1.0) | ||
---|---|---|---|---|
import firebase from "firebase/app" | import { | |||
import "firebase/firestore" | addDoc, | |||
collection, | ||||
deleteDoc, | ||||
doc, | ||||
getDocs, | ||||
getFirestore, | ||||
limit, | ||||
onSnapshot, | ||||
orderBy, | ||||
query, | ||||
updateDoc, | ||||
} from "firebase/firestore" | ||||
import { currentUser$ } from "./auth" | import { currentUser$ } from "./auth" | |||
import { settingsStore } from "~/newstore/settings" | import { settingsStore } from "~/newstore/settings" | |||
import { | import { | |||
GQLHistoryEntry, | GQLHistoryEntry, | |||
graphqlHistoryStore, | graphqlHistoryStore, | |||
HISTORY_LIMIT, | HISTORY_LIMIT, | |||
RESTHistoryEntry, | RESTHistoryEntry, | |||
restHistoryStore, | restHistoryStore, | |||
setGraphqlHistoryEntries, | setGraphqlHistoryEntries, | |||
setRESTHistoryEntries, | setRESTHistoryEntries, | |||
skipping to change at line 49 | skipping to change at line 60 | |||
async function writeHistory(entry: any, col: HistoryFBCollections) { | async function writeHistory(entry: any, col: HistoryFBCollections) { | |||
if (currentUser$.value == null) | if (currentUser$.value == null) | |||
throw new Error("User not logged in to sync history") | throw new Error("User not logged in to sync history") | |||
const hs = { | const hs = { | |||
...entry, | ...entry, | |||
updatedOn: new Date(), | updatedOn: new Date(), | |||
} | } | |||
try { | try { | |||
await firebase | await addDoc( | |||
.firestore() | collection(getFirestore(), "users", currentUser$.value.uid, col), | |||
.collection("users") | hs | |||
.doc(currentUser$.value.uid) | ) | |||
.collection(col) | ||||
.add(hs) | ||||
} catch (e) { | } catch (e) { | |||
console.error("error writing to history", hs, e) | console.error("error writing to history", hs, e) | |||
throw e | throw e | |||
} | } | |||
} | } | |||
async function deleteHistory(entry: any, col: HistoryFBCollections) { | async function deleteHistory(entry: any, col: HistoryFBCollections) { | |||
if (currentUser$.value == null) | if (currentUser$.value == null) | |||
throw new Error("User not logged in to delete history") | throw new Error("User not logged in to delete history") | |||
try { | try { | |||
await firebase | await deleteDoc( | |||
.firestore() | doc(getFirestore(), "users", currentUser$.value.uid, col, entry.id) | |||
.collection("users") | ) | |||
.doc(currentUser$.value.uid) | ||||
.collection(col) | ||||
.doc(entry.id) | ||||
.delete() | ||||
} catch (e) { | } catch (e) { | |||
console.error("error deleting history", entry, e) | console.error("error deleting history", entry, e) | |||
throw e | throw e | |||
} | } | |||
} | } | |||
async function clearHistory(col: HistoryFBCollections) { | async function clearHistory(col: HistoryFBCollections) { | |||
if (currentUser$.value == null) | if (currentUser$.value == null) | |||
throw new Error("User not logged in to clear history") | throw new Error("User not logged in to clear history") | |||
const { docs } = await firebase | const { docs } = await getDocs( | |||
.firestore() | collection(getFirestore(), "users", currentUser$.value.uid, col) | |||
.collection("users") | ) | |||
.doc(currentUser$.value.uid) | ||||
.collection(col) | ||||
.get() | ||||
await Promise.all(docs.map((e) => deleteHistory(e, col))) | await Promise.all(docs.map((e) => deleteHistory(e, col))) | |||
} | } | |||
async function toggleStar(entry: any, col: HistoryFBCollections) { | async function toggleStar(entry: any, col: HistoryFBCollections) { | |||
if (currentUser$.value == null) | if (currentUser$.value == null) | |||
throw new Error("User not logged in to toggle star") | throw new Error("User not logged in to toggle star") | |||
try { | try { | |||
await firebase | await updateDoc( | |||
.firestore() | doc(getFirestore(), "users", currentUser$.value.uid, col, entry.id), | |||
.collection("users") | { star: !entry.star } | |||
.doc(currentUser$.value.uid) | ) | |||
.collection(col) | ||||
.doc(entry.id) | ||||
.update({ star: !entry.star }) | ||||
} catch (e) { | } catch (e) { | |||
console.error("error toggling star", entry, e) | console.error("error toggling star", entry, e) | |||
throw e | throw e | |||
} | } | |||
} | } | |||
export function initHistory() { | export function initHistory() { | |||
restHistoryStore.dispatches$.subscribe((dispatch) => { | restHistoryStore.dispatches$.subscribe((dispatch) => { | |||
if ( | if ( | |||
loadedRESTHistory && | loadedRESTHistory && | |||
skipping to change at line 164 | skipping to change at line 163 | |||
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() | query( | |||
.collection("users") | collection(getFirestore(), "users", user.uid, "history"), | |||
.doc(user.uid) | orderBy("updatedOn", "desc"), | |||
.collection("history") | limit(HISTORY_LIMIT) | |||
.orderBy("updatedOn", "desc") | ), | |||
.limit(HISTORY_LIMIT) | (historyRef) => { | |||
.onSnapshot((historyRef) => { | ||||
const history: RESTHistoryEntry[] = [] | const history: RESTHistoryEntry[] = [] | |||
historyRef.forEach((doc) => { | historyRef.forEach((doc) => { | |||
const entry = doc.data() | const entry = doc.data() | |||
entry.id = doc.id | entry.id = doc.id | |||
history.push(translateToNewRESTHistory(entry)) | history.push(translateToNewRESTHistory(entry)) | |||
}) | }) | |||
loadedRESTHistory = false | loadedRESTHistory = false | |||
setRESTHistoryEntries(history) | setRESTHistoryEntries(history) | |||
loadedRESTHistory = true | loadedRESTHistory = true | |||
}) | } | |||
) | ||||
graphqlSnapshotStop = firebase | graphqlSnapshotStop = onSnapshot( | |||
.firestore() | query( | |||
.collection("users") | collection(getFirestore(), "users", user.uid, "graphqlHistory"), | |||
.doc(user.uid) | orderBy("updatedOn", "desc"), | |||
.collection("graphqlHistory") | limit(HISTORY_LIMIT) | |||
.orderBy("updatedOn", "desc") | ), | |||
.limit(HISTORY_LIMIT) | (historyRef) => { | |||
.onSnapshot((historyRef) => { | ||||
const history: GQLHistoryEntry[] = [] | const history: GQLHistoryEntry[] = [] | |||
historyRef.forEach((doc) => { | historyRef.forEach((doc) => { | |||
const entry = doc.data() | const entry = doc.data() | |||
entry.id = doc.id | entry.id = doc.id | |||
history.push(translateToNewGQLHistory(entry)) | history.push(translateToNewGQLHistory(entry)) | |||
}) | }) | |||
loadedGraphqlHistory = false | loadedGraphqlHistory = false | |||
setGraphqlHistoryEntries(history) | setGraphqlHistoryEntries(history) | |||
loadedGraphqlHistory = true | loadedGraphqlHistory = true | |||
}) | } | |||
) | ||||
} | } | |||
}) | }) | |||
} | } | |||
End of changes. 9 change blocks. | ||||
46 lines changed or deleted | 45 lines changed or added |