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>
<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

View File

@@ -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;
}

View File

@@ -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 = "";

View File

@@ -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;
}

View File

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

View File

@@ -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;

View File

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