Added History sync

This commit is contained in:
Liyas Thomas
2020-01-23 19:07:36 +05:30
parent ab336de732
commit 65e9e7a73b
7 changed files with 91 additions and 25 deletions

View File

@@ -1,12 +1,12 @@
<template> <template>
<virtual-list <virtual-list
v-if="fb.feedsInFeed.length !== 0" v-if="fb.currentFeeds.length !== 0"
class="virtual-list" class="virtual-list"
:class="{ filled: fb.feedsInFeed.length }" :class="{ filled: fb.currentFeeds.length }"
:size="56" :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"> <div class="show-on-large-screen">
<li> <li>
<input <input

View File

@@ -46,7 +46,7 @@ export default {
}, },
methods: { methods: {
formPost() { formPost() {
fb.writeFeed(this.message, this.label); fb.writeFeeds(this.message, this.label);
this.message = null; this.message = null;
this.label = null; this.label = null;
} }

View File

@@ -331,6 +331,7 @@ ol li {
<script> <script>
import { findStatusGroup } from "../pages/index"; import { findStatusGroup } from "../pages/index";
import { fb } from "../functions/fb";
const updateOnLocalStorage = (propertyName, property) => const updateOnLocalStorage = (propertyName, property) =>
window.localStorage.setItem(propertyName, JSON.stringify(property)); window.localStorage.setItem(propertyName, JSON.stringify(property));
@@ -341,11 +342,11 @@ export default {
VirtualList: () => import("vue-virtual-scroll-list") VirtualList: () => import("vue-virtual-scroll-list")
}, },
data() { data() {
const localStorageHistory = JSON.parse(
window.localStorage.getItem("history")
);
return { return {
history: localStorageHistory || [], history:
fb.currentUser !== null
? fb.currentHistory
: JSON.parse(window.localStorage.getItem("history")) || [],
filterText: "", filterText: "",
showFilter: false, showFilter: false,
isClearingHistory: false, isClearingHistory: false,
@@ -359,6 +360,10 @@ export default {
}, },
computed: { computed: {
filteredHistory() { filteredHistory() {
this.history =
fb.currentUser !== null
? fb.currentHistory
: JSON.parse(window.localStorage.getItem("history")) || [];
return this.history.filter(entry => { return this.history.filter(entry => {
const filterText = this.filterText.toLowerCase(); const filterText = this.filterText.toLowerCase();
return Object.keys(entry).some(key => { return Object.keys(entry).some(key => {
@@ -371,6 +376,9 @@ export default {
}, },
methods: { methods: {
clearHistory() { clearHistory() {
if (fb.currentUser !== null) {
fb.clearHistory();
}
this.history = []; this.history = [];
this.filterText = ""; this.filterText = "";
this.disableHistoryClearing(); this.disableHistoryClearing();
@@ -391,6 +399,9 @@ export default {
); );
}, },
deleteHistory(entry) { deleteHistory(entry) {
if (fb.currentUser !== null) {
fb.deleteHistory(entry);
}
this.history.splice(this.history.indexOf(entry), 1); this.history.splice(this.history.indexOf(entry), 1);
if (this.history.length === 0) { if (this.history.length === 0) {
this.filterText = ""; this.filterText = "";

View File

@@ -21,10 +21,11 @@ const usersCollection = firebase.firestore().collection("users");
// the shared state object that any vue component // the shared state object that any vue component
// can get access to // can get access to
export const fb = { export const fb = {
feedsInFeed: [],
currentUser: {}, currentUser: {},
currentFeeds: [],
currentSettings: [], currentSettings: [],
writeFeed: async (message, label) => { currentHistory: [],
writeFeeds: async (message, label) => {
const dt = { const dt = {
createdOn: new Date(), createdOn: new Date(),
author: fb.currentUser.uid, author: fb.currentUser.uid,
@@ -48,7 +49,7 @@ export const fb = {
.collection("feeds") .collection("feeds")
.doc(id) .doc(id)
.delete() .delete()
.catch(e => console.error("error deleting", dt, e)); .catch(e => console.error("error deleting", id, e));
}, },
writeSettings: async (setting, value) => { writeSettings: async (setting, value) => {
const st = { const st = {
@@ -63,10 +64,39 @@ export const fb = {
return usersCollection return usersCollection
.doc(fb.currentUser.uid) .doc(fb.currentUser.uid)
.collection("settings") .collection("settings")
.doc(setting).set(st); .doc(setting)
.set(st);
} catch (e) { } catch (e) {
return console.error("error updating", st, 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,7 +131,7 @@ firebase.auth().onAuthStateChanged(user => {
feed.id = doc.id; feed.id = doc.id;
feeds.push(feed); feeds.push(feed);
}); });
fb.feedsInFeed = feeds; fb.currentFeeds = feeds;
}); });
usersCollection usersCollection
@@ -116,6 +146,19 @@ firebase.auth().onAuthStateChanged(user => {
}); });
fb.currentSettings = settings; 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 { } else {
fb.currentUser = null; fb.currentUser = null;
} }

View File

@@ -684,6 +684,7 @@ export default {
}, },
logout() { logout() {
fb.currentUser = null;
firebase firebase
.auth() .auth()
.signOut() .signOut()

View File

@@ -2129,6 +2129,11 @@ export default {
star: false star: false
}; };
this.$refs.historyComponent.addEntry(entry); this.$refs.historyComponent.addEntry(entry);
if (fb.currentUser !== null) {
if (fb.currentSettings[1].value) {
fb.writeHistory(entry);
}
}
})(); })();
} catch (error) { } catch (error) {
console.error(error); console.error(error);
@@ -2150,6 +2155,11 @@ export default {
preRequestScript: this.preRequestScript preRequestScript: this.preRequestScript
}; };
this.$refs.historyComponent.addEntry(entry); this.$refs.historyComponent.addEntry(entry);
if (fb.currentUser !== null) {
if (fb.currentSettings[1].value) {
fb.writeHistory(entry);
}
}
return; return;
} else { } else {
this.response.status = error.message; this.response.status = error.message;

View File

@@ -379,6 +379,7 @@ export default {
this.$store.commit("postwoman/applySetting", [key, this.settings[key]]); this.$store.commit("postwoman/applySetting", [key, this.settings[key]]);
}, },
logout() { logout() {
fb.currentUser = null;
firebase firebase
.auth() .auth()
.signOut() .signOut()