Migrate code to new history store
This commit is contained in:
@@ -91,10 +91,18 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { fb } from "~/helpers/fb"
|
import {
|
||||||
|
restHistory$,
|
||||||
const updateOnLocalStorage = (propertyName, property) =>
|
graphqlHistory$,
|
||||||
window.localStorage.setItem(propertyName, JSON.stringify(property))
|
clearRESTHistory,
|
||||||
|
clearGraphqlHistory,
|
||||||
|
toggleGraphqlHistoryEntryStar,
|
||||||
|
toggleRESTHistoryEntryStar,
|
||||||
|
addGraphqlHistoryEntry,
|
||||||
|
addRESTHistoryEntry,
|
||||||
|
deleteGraphqlHistoryEntry,
|
||||||
|
deleteRESTHistoryEntry
|
||||||
|
} from "~/newstore/history"
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
props: {
|
props: {
|
||||||
@@ -102,34 +110,21 @@ export default {
|
|||||||
},
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
history:
|
|
||||||
fb.currentUser !== null
|
|
||||||
? this.page === "rest"
|
|
||||||
? fb.currentHistory
|
|
||||||
: fb.currentGraphqlHistory
|
|
||||||
: JSON.parse(
|
|
||||||
window.localStorage.getItem(
|
|
||||||
this.page === "rest" ? "history" : "graphqlHistory"
|
|
||||||
)
|
|
||||||
) || [],
|
|
||||||
filterText: "",
|
filterText: "",
|
||||||
showFilter: false,
|
showFilter: false,
|
||||||
isClearingHistory: false,
|
isClearingHistory: false,
|
||||||
showMore: false,
|
showMore: false,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
subscriptions() {
|
||||||
|
return {
|
||||||
|
history: this.page === "rest" ? restHistory$ : graphqlHistory$
|
||||||
|
}
|
||||||
|
},
|
||||||
computed: {
|
computed: {
|
||||||
filteredHistory() {
|
filteredHistory() {
|
||||||
const filteringHistory =
|
const filteringHistory = this.history
|
||||||
fb.currentUser !== null
|
|
||||||
? this.page === "rest"
|
|
||||||
? fb.currentHistory
|
|
||||||
: fb.currentGraphqlHistory
|
|
||||||
: JSON.parse(
|
|
||||||
window.localStorage.getItem(
|
|
||||||
this.page === "rest" ? "history" : "graphqlHistory"
|
|
||||||
)
|
|
||||||
) || []
|
|
||||||
return filteringHistory.filter((entry) => {
|
return filteringHistory.filter((entry) => {
|
||||||
const filterText = this.filterText.toLowerCase()
|
const filterText = this.filterText.toLowerCase()
|
||||||
return Object.keys(entry).some((key) => {
|
return Object.keys(entry).some((key) => {
|
||||||
@@ -141,19 +136,10 @@ export default {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
async clearHistory() {
|
clearHistory() {
|
||||||
if (fb.currentUser !== null) {
|
if (this.page === "rest") clearRESTHistory()
|
||||||
this.page === "rest"
|
else clearGraphqlHistory()
|
||||||
? await fb.clearHistory()
|
|
||||||
: await fb.clearGraphqlHistory()
|
|
||||||
}
|
|
||||||
this.history = []
|
|
||||||
this.filterText = ""
|
|
||||||
this.disableHistoryClearing()
|
|
||||||
updateOnLocalStorage(
|
|
||||||
this.page === "rest" ? "history" : "graphqlHistory",
|
|
||||||
this.history
|
|
||||||
)
|
|
||||||
this.$toast.error(this.$t("history_deleted"), {
|
this.$toast.error(this.$t("history_deleted"), {
|
||||||
icon: "delete",
|
icon: "delete",
|
||||||
})
|
})
|
||||||
@@ -161,36 +147,17 @@ export default {
|
|||||||
useHistory(entry) {
|
useHistory(entry) {
|
||||||
this.$emit("useHistory", entry)
|
this.$emit("useHistory", entry)
|
||||||
},
|
},
|
||||||
async deleteHistory(entry) {
|
deleteHistory(entry) {
|
||||||
if (this.history.length === 0) {
|
if (this.page === "rest") deleteRESTHistoryEntry(entry)
|
||||||
this.filterText = ""
|
else deleteGraphqlHistoryEntry(entry)
|
||||||
}
|
|
||||||
if (fb.currentUser !== null) {
|
|
||||||
await (this.page === "rest"
|
|
||||||
? fb.deleteHistory(entry)
|
|
||||||
: fb.deleteGraphqlHistory(entry))
|
|
||||||
this.history = fb.currentHistory
|
|
||||||
updateOnLocalStorage(
|
|
||||||
this.page === "rest" ? "history" : "graphqlHistory",
|
|
||||||
this.history
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
this.history.splice(this.history.indexOf(entry), 1)
|
|
||||||
updateOnLocalStorage(
|
|
||||||
this.page === "rest" ? "history" : "graphqlHistory",
|
|
||||||
this.history
|
|
||||||
)
|
|
||||||
}
|
|
||||||
this.$toast.error(this.$t("deleted"), {
|
this.$toast.error(this.$t("deleted"), {
|
||||||
icon: "delete",
|
icon: "delete",
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
addEntry(entry) {
|
addEntry(entry) {
|
||||||
this.history.push(entry)
|
if (this.page === "rest") addRESTHistoryEntry(entry)
|
||||||
updateOnLocalStorage(
|
else addGraphqlHistoryEntry(entry)
|
||||||
this.page === "rest" ? "history" : "graphqlHistory",
|
|
||||||
this.history
|
|
||||||
)
|
|
||||||
},
|
},
|
||||||
enableHistoryClearing() {
|
enableHistoryClearing() {
|
||||||
if (!this.history || !this.history.length) return
|
if (!this.history || !this.history.length) return
|
||||||
@@ -202,17 +169,9 @@ export default {
|
|||||||
toggleCollapse() {
|
toggleCollapse() {
|
||||||
this.showMore = !this.showMore
|
this.showMore = !this.showMore
|
||||||
},
|
},
|
||||||
async toggleStar(entry) {
|
toggleStar(entry) {
|
||||||
if (fb.currentUser !== null) {
|
if (this.page === "rest") toggleRESTHistoryEntryStar(entry)
|
||||||
this.page === "rest"
|
else toggleGraphqlHistoryEntryStar(entry)
|
||||||
? await fb.toggleStar(entry, !entry.star)
|
|
||||||
: await fb.toggleGraphqlHistoryStar(entry, !entry.star)
|
|
||||||
}
|
|
||||||
entry.star = !entry.star
|
|
||||||
updateOnLocalStorage(
|
|
||||||
this.page === "rest" ? "history" : "graphqlHistory",
|
|
||||||
this.history
|
|
||||||
)
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,12 @@ import "firebase/firestore"
|
|||||||
import "firebase/auth"
|
import "firebase/auth"
|
||||||
import { ReplaySubject } from "rxjs"
|
import { ReplaySubject } from "rxjs"
|
||||||
import { applySettingFB, settingsStore } from "~/newstore/settings"
|
import { applySettingFB, settingsStore } from "~/newstore/settings"
|
||||||
|
import {
|
||||||
|
restHistoryStore,
|
||||||
|
setRESTHistoryEntries,
|
||||||
|
graphqlHistoryStore,
|
||||||
|
setGraphqlHistoryEntries,
|
||||||
|
} from "~/newstore/history"
|
||||||
|
|
||||||
// Initialize Firebase, copied from cloud console
|
// Initialize Firebase, copied from cloud console
|
||||||
const firebaseConfig = {
|
const firebaseConfig = {
|
||||||
@@ -35,8 +41,6 @@ export class FirebaseInstance {
|
|||||||
this.idToken = null
|
this.idToken = null
|
||||||
this.currentFeeds = []
|
this.currentFeeds = []
|
||||||
this.currentSettings = []
|
this.currentSettings = []
|
||||||
this.currentHistory = []
|
|
||||||
this.currentGraphqlHistory = []
|
|
||||||
this.currentCollections = []
|
this.currentCollections = []
|
||||||
this.currentGraphqlCollections = []
|
this.currentGraphqlCollections = []
|
||||||
this.currentEnvironments = []
|
this.currentEnvironments = []
|
||||||
@@ -45,6 +49,44 @@ export class FirebaseInstance {
|
|||||||
this.idToken$ = new ReplaySubject(1)
|
this.idToken$ = new ReplaySubject(1)
|
||||||
|
|
||||||
let loadedSettings = false
|
let loadedSettings = false
|
||||||
|
let loadedRESTHistory = false
|
||||||
|
let loadedGraphqlHistory = false
|
||||||
|
|
||||||
|
restHistoryStore.dispatches$.subscribe((dispatch) => {
|
||||||
|
if (
|
||||||
|
loadedRESTHistory &&
|
||||||
|
this.currentUser &&
|
||||||
|
settingsStore.value.syncHistory
|
||||||
|
) {
|
||||||
|
if (dispatch.dispatcher === "addEntry") {
|
||||||
|
this.writeHistory(dispatch.payload.entry)
|
||||||
|
} else if (dispatch.dispatcher === "deleteEntry") {
|
||||||
|
this.deleteHistory(dispatch.payload.entry)
|
||||||
|
} else if (dispatch.dispatcher === "clearHistory") {
|
||||||
|
this.clearHistory()
|
||||||
|
} else if (dispatch.dispatcher === "toggleStar") {
|
||||||
|
this.toggleStar(dispatch.payload.entry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
graphqlHistoryStore.dispatches$.subscribe((dispatch) => {
|
||||||
|
if (
|
||||||
|
loadedGraphqlHistory &&
|
||||||
|
this.currentUser &&
|
||||||
|
settingsStore.value.syncHistory
|
||||||
|
) {
|
||||||
|
if (dispatch.dispatcher === "addEntry") {
|
||||||
|
this.writeGraphqlHistory(dispatch.payload.entry)
|
||||||
|
} else if (dispatch.dispatcher === "deleteEntry") {
|
||||||
|
this.deleteGraphqlHistory(dispatch.payload.entry)
|
||||||
|
} else if (dispatch.dispatcher === "clearHistory") {
|
||||||
|
this.clearGraphqlHistory()
|
||||||
|
} else if (dispatch.dispatcher === "toggleStar") {
|
||||||
|
this.toggleGraphqlHistoryStar(dispatch.payload.entry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
settingsStore.dispatches$.subscribe((dispatch) => {
|
settingsStore.dispatches$.subscribe((dispatch) => {
|
||||||
if (
|
if (
|
||||||
@@ -139,12 +181,16 @@ export class FirebaseInstance {
|
|||||||
.limit(HISTORY_LIMIT)
|
.limit(HISTORY_LIMIT)
|
||||||
.onSnapshot((historyRef) => {
|
.onSnapshot((historyRef) => {
|
||||||
const history = []
|
const history = []
|
||||||
|
|
||||||
historyRef.forEach((doc) => {
|
historyRef.forEach((doc) => {
|
||||||
const entry = doc.data()
|
const entry = doc.data()
|
||||||
entry.id = doc.id
|
entry.id = doc.id
|
||||||
history.push(entry)
|
history.push(entry)
|
||||||
})
|
})
|
||||||
this.currentHistory = history
|
|
||||||
|
setRESTHistoryEntries(history)
|
||||||
|
|
||||||
|
loadedRESTHistory = true
|
||||||
})
|
})
|
||||||
|
|
||||||
this.usersCollection
|
this.usersCollection
|
||||||
@@ -154,12 +200,16 @@ export class FirebaseInstance {
|
|||||||
.limit(GQL_HISTORY_LIMIT)
|
.limit(GQL_HISTORY_LIMIT)
|
||||||
.onSnapshot((historyRef) => {
|
.onSnapshot((historyRef) => {
|
||||||
const history = []
|
const history = []
|
||||||
|
|
||||||
historyRef.forEach((doc) => {
|
historyRef.forEach((doc) => {
|
||||||
const entry = doc.data()
|
const entry = doc.data()
|
||||||
entry.id = doc.id
|
entry.id = doc.id
|
||||||
history.push(entry)
|
history.push(entry)
|
||||||
})
|
})
|
||||||
this.currentGraphqlHistory = history
|
|
||||||
|
setGraphqlHistoryEntries(history)
|
||||||
|
|
||||||
|
loadedGraphqlHistory = true
|
||||||
})
|
})
|
||||||
|
|
||||||
this.usersCollection
|
this.usersCollection
|
||||||
@@ -365,13 +415,13 @@ export class FirebaseInstance {
|
|||||||
await Promise.all(docs.map((e) => this.deleteGraphqlHistory(e)))
|
await Promise.all(docs.map((e) => this.deleteGraphqlHistory(e)))
|
||||||
}
|
}
|
||||||
|
|
||||||
async toggleStar(entry, value) {
|
async toggleStar(entry) {
|
||||||
try {
|
try {
|
||||||
await this.usersCollection
|
await this.usersCollection
|
||||||
.doc(this.currentUser.uid)
|
.doc(this.currentUser.uid)
|
||||||
.collection("history")
|
.collection("history")
|
||||||
.doc(entry.id)
|
.doc(entry.id)
|
||||||
.update({ star: value })
|
.update({ star: !entry.star })
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("error deleting", entry, e)
|
console.error("error deleting", entry, e)
|
||||||
|
|
||||||
@@ -379,13 +429,13 @@ export class FirebaseInstance {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async toggleGraphqlHistoryStar(entry, value) {
|
async toggleGraphqlHistoryStar(entry) {
|
||||||
try {
|
try {
|
||||||
await this.usersCollection
|
await this.usersCollection
|
||||||
.doc(this.currentUser.uid)
|
.doc(this.currentUser.uid)
|
||||||
.collection("graphqlHistory")
|
.collection("graphqlHistory")
|
||||||
.doc(entry.id)
|
.doc(entry.id)
|
||||||
.update({ star: value })
|
.update({ star: !entry.star })
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("error deleting", entry, e)
|
console.error("error deleting", entry, e)
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,12 @@ import clone from "lodash/clone"
|
|||||||
import assign from "lodash/assign"
|
import assign from "lodash/assign"
|
||||||
import eq from "lodash/eq"
|
import eq from "lodash/eq"
|
||||||
import { settingsStore, bulkApplySettings, defaultSettings } from "./settings"
|
import { settingsStore, bulkApplySettings, defaultSettings } from "./settings"
|
||||||
|
import {
|
||||||
|
restHistoryStore,
|
||||||
|
graphqlHistoryStore,
|
||||||
|
setRESTHistoryEntries,
|
||||||
|
setGraphqlHistoryEntries,
|
||||||
|
} from "./history"
|
||||||
|
|
||||||
function checkAndMigrateOldSettings() {
|
function checkAndMigrateOldSettings() {
|
||||||
const vuexData = JSON.parse(window.localStorage.getItem("vuex") || "{}")
|
const vuexData = JSON.parse(window.localStorage.getItem("vuex") || "{}")
|
||||||
@@ -32,8 +38,30 @@ function setupSettingsPersistence() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setupHistoryPersistence() {
|
||||||
|
const restHistoryData = JSON.parse(
|
||||||
|
window.localStorage.getItem("history") || "[]"
|
||||||
|
)
|
||||||
|
|
||||||
|
const graphqlHistoryData = JSON.parse(
|
||||||
|
window.localStorage.getItem("graphqlHistory") || "[]"
|
||||||
|
)
|
||||||
|
|
||||||
|
setRESTHistoryEntries(restHistoryData)
|
||||||
|
setGraphqlHistoryEntries(graphqlHistoryData)
|
||||||
|
|
||||||
|
restHistoryStore.subject$.subscribe(({ state }) => {
|
||||||
|
window.localStorage.setItem("history", JSON.stringify(state))
|
||||||
|
})
|
||||||
|
|
||||||
|
graphqlHistoryStore.subject$.subscribe(({ state }) => {
|
||||||
|
window.localStorage.setItem("graphqlHistory", JSON.stringify(state))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export function setupLocalPersistence() {
|
export function setupLocalPersistence() {
|
||||||
checkAndMigrateOldSettings()
|
checkAndMigrateOldSettings()
|
||||||
|
|
||||||
setupSettingsPersistence()
|
setupSettingsPersistence()
|
||||||
|
setupHistoryPersistence()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -481,7 +481,6 @@ import { commonHeaders } from "~/helpers/headers"
|
|||||||
import { getPlatformSpecialKey } from "~/helpers/platformutils"
|
import { getPlatformSpecialKey } from "~/helpers/platformutils"
|
||||||
import { sendNetworkRequest } from "~/helpers/network"
|
import { sendNetworkRequest } from "~/helpers/network"
|
||||||
import { getSettingSubject } from "~/newstore/settings"
|
import { getSettingSubject } from "~/newstore/settings"
|
||||||
import { fb } from "~/helpers/fb"
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
beforeRouteLeave(_to, _from, next) {
|
beforeRouteLeave(_to, _from, next) {
|
||||||
@@ -853,12 +852,8 @@ export default {
|
|||||||
duration,
|
duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Use history store system mechanism instead!
|
||||||
this.$refs.graphqlHistoryComponent.addEntry(entry)
|
this.$refs.graphqlHistoryComponent.addEntry(entry)
|
||||||
if (fb.currentUser !== null && fb.currentSettings[2]) {
|
|
||||||
if (fb.currentSettings[2].value) {
|
|
||||||
fb.writeGraphqlHistory(entry)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.response = `${error}. ${this.$t("check_console_details")}`
|
this.response = `${error}. ${this.$t("check_console_details")}`
|
||||||
this.$nuxt.$loading.finish()
|
this.$nuxt.$loading.finish()
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
<template>
|
<template>
|
||||||
|
<!-- eslint-disable -->
|
||||||
<div class="page">
|
<div class="page">
|
||||||
<div class="content">
|
<div class="content">
|
||||||
<div class="page-columns inner-left">
|
<div class="page-columns inner-left">
|
||||||
@@ -651,6 +652,8 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
/* eslint-disable */
|
||||||
|
|
||||||
import url from "url"
|
import url from "url"
|
||||||
import querystring from "querystring"
|
import querystring from "querystring"
|
||||||
import parseCurlCommand from "~/helpers/curlparser"
|
import parseCurlCommand from "~/helpers/curlparser"
|
||||||
@@ -1406,10 +1409,8 @@ export default {
|
|||||||
entry.url = parseTemplateString(entry.url, environmentVariables)
|
entry.url = parseTemplateString(entry.url, environmentVariables)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Use the history store mechanism instead!
|
||||||
this.$refs.historyComponent.addEntry(entry)
|
this.$refs.historyComponent.addEntry(entry)
|
||||||
if (fb.currentUser !== null && this.SYNC_COLLECTIONS) {
|
|
||||||
fb.writeHistory(entry)
|
|
||||||
}
|
|
||||||
})()
|
})()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.runningRequest = false
|
this.runningRequest = false
|
||||||
@@ -1464,10 +1465,8 @@ export default {
|
|||||||
entry.url = parseTemplateString(entry.url, environmentVariables)
|
entry.url = parseTemplateString(entry.url, environmentVariables)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Use the history state mechanism instead!
|
||||||
this.$refs.historyComponent.addEntry(entry)
|
this.$refs.historyComponent.addEntry(entry)
|
||||||
if (fb.currentUser !== null && this.SYNC_HISTORY) {
|
|
||||||
fb.writeHistory(entry)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
this.response.status = error.message
|
this.response.status = error.message
|
||||||
|
|||||||
Reference in New Issue
Block a user