Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

:sparkles: Added History sync

Liyas Thomas65e9e7a73b44
diff --git a/components/firebase/feeds.vue b/components/firebase/feeds.vue
index 89bd4c1fc..547a8cecd 100644
--- a/components/firebase/feeds.vue
+++ b/components/firebase/feeds.vue
@@ -1,12 +1,12 @@
<template>
<virtual-list
- v-if="fb.feedsInFeed.length !== 0"
+ v-if="fb.currentFeeds.length !== 0"
class="virtual-list"
- :class="{ filled: fb.feedsInFeed.length }"
+ :class="{ filled: fb.currentFeeds.length }"
:size="56"
- :remain="Math.min(8, fb.feedsInFeed.length)"
+ :remain="Math.min(8, fb.currentFeeds.length)"
>
- <ul v-for="feed in fb.feedsInFeed" :key="feed.id">
+ <ul v-for="feed in fb.currentFeeds" :key="feed.id">
<div class="show-on-large-screen">
<li>
<input
diff --git a/components/firebase/inputform.vue b/components/firebase/inputform.vue
index 10f120df5..0eb0a9e62 100644
--- a/components/firebase/inputform.vue
+++ b/components/firebase/inputform.vue
@@ -46,7 +46,7 @@ export default {
},
methods: {
formPost() {
- fb.writeFeed(this.message, this.label);
+ fb.writeFeeds(this.message, this.label);
this.message = null;
this.label = null;
}
diff --git a/components/history.vue b/components/history.vue
index 289f5586b..e186cd2ea 100644
--- a/components/history.vue
+++ b/components/history.vue
@@ -331,6 +331,7 @@ ol li {
<script>
import { findStatusGroup } from "../pages/index";
+import { fb } from "../functions/fb";
const updateOnLocalStorage = (propertyName, property) =>
window.localStorage.setItem(propertyName, JSON.stringify(property));
@@ -341,11 +342,11 @@ export default {
VirtualList: () => import("vue-virtual-scroll-list")
},
data() {
- const localStorageHistory = JSON.parse(
- window.localStorage.getItem("history")
- );
return {
- history: localStorageHistory || [],
+ history:
+ fb.currentUser !== null
+ ? fb.currentHistory
+ : JSON.parse(window.localStorage.getItem("history")) || [],
filterText: "",
showFilter: false,
isClearingHistory: false,
@@ -359,6 +360,10 @@ export default {
},
computed: {
filteredHistory() {
+ this.history =
+ fb.currentUser !== null
+ ? fb.currentHistory
+ : JSON.parse(window.localStorage.getItem("history")) || [];
return this.history.filter(entry => {
const filterText = this.filterText.toLowerCase();
return Object.keys(entry).some(key => {
@@ -371,6 +376,9 @@ export default {
},
methods: {
clearHistory() {
+ if (fb.currentUser !== null) {
+ fb.clearHistory();
+ }
this.history = [];
this.filterText = "";
this.disableHistoryClearing();
@@ -391,6 +399,9 @@ export default {
);
},
deleteHistory(entry) {
+ if (fb.currentUser !== null) {
+ fb.deleteHistory(entry);
+ }
this.history.splice(this.history.indexOf(entry), 1);
if (this.history.length === 0) {
this.filterText = "";
diff --git a/functions/fb.js b/functions/fb.js
index 9de219aca..ff906994e 100644
--- a/functions/fb.js
+++ b/functions/fb.js
@@ -21,10 +21,11 @@ const usersCollection = firebase.firestore().collection("users");
// the shared state object that any vue component
// can get access to
export const fb = {
- feedsInFeed: [],
currentUser: {},
+ currentFeeds: [],
currentSettings: [],
- writeFeed: async (message, label) => {
+ currentHistory: [],
+ writeFeeds: async (message, label) => {
const dt = {
createdOn: new Date(),
author: fb.currentUser.uid,
@@ -48,7 +49,7 @@ export const fb = {
.collection("feeds")
.doc(id)
.delete()
- .catch(e => console.error("error deleting", dt, e));
+ .catch(e => console.error("error deleting", id, e));
},
writeSettings: async (setting, value) => {
const st = {
@@ -63,10 +64,39 @@ export const fb = {
return usersCollection
.doc(fb.currentUser.uid)
.collection("settings")
- .doc(setting).set(st);
+ .doc(setting)
+ .set(st);
} catch (e) {
return console.error("error updating", st, e);
}
+ },
+ writeHistory: async entry => {
+ const hs = entry;
+ try {
+ return usersCollection
+ .doc(fb.currentUser.uid)
+ .collection("history")
+ .add(hs);
+ } catch (e) {
+ return console.error("error inserting", hs, e);
+ }
+ },
+ deleteHistory: entry => {
+ usersCollection
+ .doc(fb.currentUser.uid)
+ .collection("history")
+ .doc(entry.id)
+ .delete()
+ .catch(e => console.error("error deleting", entry, e));
+ },
+ clearHistory: () => {
+ usersCollection
+ .doc(fb.currentUser.uid)
+ .collection("history")
+ .get()
+ .then(({ docs }) => {
+ docs.forEach(e => fb.deleteHistory(e));
+ });
}
};
@@ -101,21 +131,34 @@ firebase.auth().onAuthStateChanged(user => {
feed.id = doc.id;
feeds.push(feed);
});
- fb.feedsInFeed = feeds;
+ fb.currentFeeds = feeds;
});
- usersCollection
- .doc(fb.currentUser.uid)
- .collection("settings")
- .onSnapshot(settingsRef => {
- const settings = [];
- settingsRef.forEach(doc => {
- const setting = doc.data();
- setting.id = doc.id;
- settings.push(setting);
- });
- fb.currentSettings = settings;
+ usersCollection
+ .doc(fb.currentUser.uid)
+ .collection("settings")
+ .onSnapshot(settingsRef => {
+ const settings = [];
+ settingsRef.forEach(doc => {
+ const setting = doc.data();
+ setting.id = doc.id;
+ settings.push(setting);
});
+ fb.currentSettings = settings;
+ });
+
+ usersCollection
+ .doc(fb.currentUser.uid)
+ .collection("history")
+ .onSnapshot(historyRef => {
+ const history = [];
+ historyRef.forEach(doc => {
+ const entry = doc.data();
+ entry.id = doc.id;
+ history.push(entry);
+ });
+ fb.currentHistory = history;
+ });
} else {
fb.currentUser = null;
}
diff --git a/layouts/default.vue b/layouts/default.vue
index 60e80d43c..15626a247 100644
--- a/layouts/default.vue
+++ b/layouts/default.vue
@@ -684,6 +684,7 @@ export default {
},
logout() {
+ fb.currentUser = null;
firebase
.auth()
.signOut()
diff --git a/pages/index.vue b/pages/index.vue
index 31bacee7d..12845ead1 100644
--- a/pages/index.vue
+++ b/pages/index.vue
@@ -2129,6 +2129,11 @@ export default {
star: false
};
this.$refs.historyComponent.addEntry(entry);
+ if (fb.currentUser !== null) {
+ if (fb.currentSettings[1].value) {
+ fb.writeHistory(entry);
+ }
+ }
})();
} catch (error) {
console.error(error);
@@ -2150,6 +2155,11 @@ export default {
preRequestScript: this.preRequestScript
};
this.$refs.historyComponent.addEntry(entry);
+ if (fb.currentUser !== null) {
+ if (fb.currentSettings[1].value) {
+ fb.writeHistory(entry);
+ }
+ }
return;
} else {
this.response.status = error.message;
diff --git a/pages/settings.vue b/pages/settings.vue
index 920446d95..0bba79853 100644
--- a/pages/settings.vue
+++ b/pages/settings.vue
@@ -379,6 +379,7 @@ export default {
this.$store.commit("postwoman/applySetting", [key, this.settings[key]]);
},
logout() {
+ fb.currentUser = null;
firebase
.auth()
.signOut()