Cherry picking refactored fb.js from #879 by @AndrewBastin (#1286)

* Cherry picking refactored fb.js from #879 by @AndrewBastin

* Fixed a minor UI glitch in History section

* Removed logout success toast testcase

Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com>
This commit is contained in:
Liyas Thomas
2020-10-17 14:53:19 +05:30
committed by GitHub
parent b6b3cbcb9a
commit 22a3bba6ab
13 changed files with 2408 additions and 473 deletions

View File

@@ -13,213 +13,313 @@ const firebaseConfig = {
appId: process.env.APP_ID || "1:421993993223:web:ec0baa8ee8c02ffa1fc6a2",
measurementId: process.env.MEASUREMENT_ID || "G-ERJ6025CEB",
}
firebase.initializeApp(firebaseConfig)
// a reference to the users collection
const usersCollection = firebase.firestore().collection("users")
export const authProviders = {
google: () => new firebase.auth.GoogleAuthProvider(),
github: () => new firebase.auth.GithubAuthProvider(),
}
// the shared state object that any vue component
// can get access to
export const fb = {
currentUser: null,
currentFeeds: [],
currentSettings: [],
currentHistory: [],
currentCollections: [],
currentEnvironments: [],
writeFeeds: async (message, label) => {
export class FirebaseInstance {
constructor(fbapp, authProviders) {
this.app = fbapp
this.authProviders = authProviders
this.usersCollection = this.app.firestore().collection("users")
this.currentUser = null
this.currentFeeds = []
this.currentSettings = []
this.currentHistory = []
this.currentCollections = []
this.currentEnvironments = []
this.currentTeams = []
this.app.auth().onAuthStateChanged((user) => {
if (user) {
this.currentUser = user
this.currentUser.providerData.forEach((profile) => {
let us = {
updatedOn: new Date(),
provider: profile.providerId,
name: profile.displayName,
email: profile.email,
photoUrl: profile.photoURL,
uid: profile.uid,
}
this.usersCollection
.doc(this.currentUser.uid)
.set(us)
.catch((e) => console.error("error updating", us, e))
})
this.usersCollection
.doc(this.currentUser.uid)
.collection("feeds")
.orderBy("createdOn", "desc")
.onSnapshot((feedsRef) => {
const feeds = []
feedsRef.forEach((doc) => {
const feed = doc.data()
feed.id = doc.id
feeds.push(feed)
})
this.currentFeeds = feeds
})
this.usersCollection
.doc(this.currentUser.uid)
.collection("settings")
.onSnapshot((settingsRef) => {
const settings = []
settingsRef.forEach((doc) => {
const setting = doc.data()
setting.id = doc.id
settings.push(setting)
})
this.currentSettings = settings
})
this.usersCollection
.doc(this.currentUser.uid)
.collection("history")
.onSnapshot((historyRef) => {
const history = []
historyRef.forEach((doc) => {
const entry = doc.data()
entry.id = doc.id
history.push(entry)
})
this.currentHistory = history
})
this.usersCollection
.doc(this.currentUser.uid)
.collection("collections")
.onSnapshot((collectionsRef) => {
const collections = []
collectionsRef.forEach((doc) => {
const collection = doc.data()
collection.id = doc.id
collections.push(collection)
})
if (collections.length > 0) {
this.currentCollections = collections[0].collection
}
})
this.usersCollection
.doc(this.currentUser.uid)
.collection("environments")
.onSnapshot((environmentsRef) => {
const environments = []
environmentsRef.forEach((doc) => {
const environment = doc.data()
environment.id = doc.id
environments.push(environment)
})
if (environments.length > 0) {
this.currentEnvironments = environments[0].environment
}
})
this.usersCollection
.doc(this.currentUser.uid)
.collection("teams")
.onSnapshot((teamsRef) => {
const teams = []
teamsRef.forEach((doc) => {
const team = doc.data()
team.id = doc.id
teams.push(team)
})
this.currentTeams = teams[0].team
})
} else {
this.currentUser = null
}
})
}
async signInUserWithGoogle() {
return await this.app.auth().signInWithPopup(this.authProviders.google())
}
async signInUserWithGithub() {
return await this.app.auth().signInWithPopup(this.authProviders.github())
}
async signInWithEmailAndPassword(email, password) {
return await this.app.auth().signInWithEmailAndPassword(email, password)
}
async getSignInMethodsForEmail(email) {
return await this.app.auth().fetchSignInMethodsForEmail(email)
}
async signOutUser() {
if (!this.currentUser) throw new Error("No user has logged in")
await this.app.auth().signOut()
this.currentUser = null
}
async writeFeeds(message, label) {
const dt = {
createdOn: new Date(),
author: fb.currentUser.uid,
author_name: fb.currentUser.displayName,
author_image: fb.currentUser.photoURL,
author: this.currentUser.uid,
author_name: this.currentUser.displayName,
author_image: this.currentUser.photoURL,
message,
label,
}
usersCollection
.doc(fb.currentUser.uid)
.collection("feeds")
.add(dt)
.catch((e) => console.error("error inserting", dt, e))
},
deleteFeed: (id) => {
usersCollection
.doc(fb.currentUser.uid)
.collection("feeds")
.doc(id)
.delete()
.catch((e) => console.error("error deleting", id, e))
},
writeSettings: async (setting, value) => {
try {
await this.usersCollection.doc(this.currentUser.uid).collection("feeds").add(dt)
} catch (e) {
console.error("error inserting", dt, e)
throw e
}
}
async deleteFeed(id) {
try {
await this.usersCollection.doc(this.currentUser.uid).collection("feeds").doc(id).delete()
} catch (e) {
console.error("error deleting", id, e)
throw e
}
}
async writeSettings(setting, value) {
const st = {
updatedOn: new Date(),
author: fb.currentUser.uid,
author_name: fb.currentUser.displayName,
author_image: fb.currentUser.photoURL,
author: this.currentUser.uid,
author_name: this.currentUser.displayName,
author_image: this.currentUser.photoURL,
name: setting,
value,
}
usersCollection
.doc(fb.currentUser.uid)
.collection("settings")
.doc(setting)
.set(st)
.catch((e) => console.error("error updating", st, e))
},
writeHistory: async (entry) => {
try {
await this.usersCollection
.doc(this.currentUser.uid)
.collection("settings")
.doc(setting)
.set(st)
} catch (e) {
console.error("error updating", st, e)
throw e
}
}
async writeHistory(entry) {
const hs = entry
usersCollection
.doc(fb.currentUser.uid)
.collection("history")
.add(hs)
.catch((e) => 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)
try {
await this.usersCollection.doc(this.currentUser.uid).collection("history").add(hs)
} catch (e) {
console.error("error inserting", hs, e)
throw e
}
}
async deleteHistory(entry) {
try {
await this.usersCollection
.doc(this.currentUser.uid)
.collection("history")
.doc(entry.id)
.delete()
} catch (e) {
console.error("error deleting", entry, e)
throw e
}
}
async clearHistory() {
const { docs } = await this.usersCollection
.doc(this.currentUser.uid)
.collection("history")
.get()
.then(({ docs }) => {
docs.forEach((e) => fb.deleteHistory(e))
})
},
toggleStar: (entry, value) => {
usersCollection
.doc(fb.currentUser.uid)
.collection("history")
.doc(entry.id)
.update({ star: value })
.catch((e) => console.error("error deleting", entry, e))
},
writeCollections: async (collection) => {
await Promise.all(docs.map((e) => this.deleteHistory(e)))
}
async toggleStar(entry, value) {
try {
await this.usersCollection
.doc(this.currentUser.uid)
.collection("history")
.doc(entry.id)
.update({ star: value })
} catch (e) {
console.error("error deleting", entry, e)
throw e
}
}
async writeCollections(collection) {
const cl = {
updatedOn: new Date(),
author: fb.currentUser.uid,
author_name: fb.currentUser.displayName,
author_image: fb.currentUser.photoURL,
author: this.currentUser.uid,
author_name: this.currentUser.displayName,
author_image: this.currentUser.photoURL,
collection,
}
usersCollection
.doc(fb.currentUser.uid)
.collection("collections")
.doc("sync")
.set(cl)
.catch((e) => console.error("error updating", cl, e))
},
writeEnvironments: async (environment) => {
try {
await this.usersCollection
.doc(this.currentUser.uid)
.collection("collections")
.doc("sync")
.set(cl)
} catch (e) {
console.error("error updating", cl, e)
throw e
}
}
async writeEnvironments(environment) {
const ev = {
updatedOn: new Date(),
author: fb.currentUser.uid,
author_name: fb.currentUser.displayName,
author_image: fb.currentUser.photoURL,
author: this.currentUser.uid,
author_name: this.currentUser.displayName,
author_image: this.currentUser.photoURL,
environment,
}
usersCollection
.doc(fb.currentUser.uid)
.collection("environments")
.doc("sync")
.set(ev)
.catch((e) => console.error("error updating", ev, e))
},
try {
await this.usersCollection
.doc(this.currentUser.uid)
.collection("environments")
.doc("sync")
.set(ev)
} catch (e) {
console.error("error updating", ev, e)
throw e
}
}
async writeTeams(team) {
const ev = {
updatedOn: new Date(),
author: this.currentUser.uid,
author_name: this.currentUser.displayName,
author_image: this.currentUser.photoURL,
team: team,
}
try {
await this.usersCollection.doc(this.currentUser.uid).collection("teams").doc("sync").set(ev)
} catch (e) {
console.error("error updating", ev, e)
throw e
}
}
}
// When a user logs in or out, save that in the store
firebase.auth().onAuthStateChanged((user) => {
if (user) {
fb.currentUser = user
fb.currentUser.providerData.forEach((profile) => {
let us = {
updatedOn: new Date(),
provider: profile.providerId,
name: profile.displayName,
email: profile.email,
photoUrl: profile.photoURL,
uid: profile.uid,
}
usersCollection
.doc(fb.currentUser.uid)
.set(us)
.catch((e) => console.error("error updating", us, e))
})
usersCollection
.doc(fb.currentUser.uid)
.collection("feeds")
.orderBy("createdOn", "desc")
.onSnapshot((feedsRef) => {
const feeds = []
feedsRef.forEach((doc) => {
const feed = doc.data()
feed.id = doc.id
feeds.push(feed)
})
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("history")
.onSnapshot((historyRef) => {
const history = []
historyRef.forEach((doc) => {
const entry = doc.data()
entry.id = doc.id
history.push(entry)
})
fb.currentHistory = history
})
usersCollection
.doc(fb.currentUser.uid)
.collection("collections")
.onSnapshot((collectionsRef) => {
const collections = []
collectionsRef.forEach((doc) => {
const collection = doc.data()
collection.id = doc.id
collections.push(collection)
})
if (collections.length > 0) {
fb.currentCollections = collections[0].collection
}
})
usersCollection
.doc(fb.currentUser.uid)
.collection("environments")
.onSnapshot((environmentsRef) => {
const environments = []
environmentsRef.forEach((doc) => {
const environment = doc.data()
environment.id = doc.id
environments.push(environment)
})
if (environments.length > 0) {
fb.currentEnvironments = environments[0].environment
}
})
} else {
fb.currentUser = null
}
})
export const fb = new FirebaseInstance(firebase.initializeApp(firebaseConfig), authProviders)