api-client/helpers/fb.js

226 lines
6.3 KiB
JavaScript
Raw Normal View History

import firebase from "firebase/app"
import "firebase/firestore"
import "firebase/auth"
2020-01-20 16:55:48 +00:00
// Initialize Firebase, copied from cloud console
2020-01-20 17:31:31 +00:00
const firebaseConfig = {
2020-05-12 13:45:17 +00:00
apiKey: process.env.API_KEY || "AIzaSyCMsFreESs58-hRxTtiqQrIcimh4i1wbsM",
authDomain: process.env.AUTH_DOMAIN || "postwoman-api.firebaseapp.com",
databaseURL: process.env.DATABASE_URL || "https://postwoman-api.firebaseio.com",
projectId: process.env.PROJECT_ID || "postwoman-api",
storageBucket: process.env.STORAGE_BUCKET || "postwoman-api.appspot.com",
messagingSenderId: process.env.MESSAGING_SENDER_ID || "421993993223",
appId: process.env.APP_ID || "1:421993993223:web:ec0baa8ee8c02ffa1fc6a2",
measurementId: process.env.MEASUREMENT_ID || "G-ERJ6025CEB",
2020-02-24 18:44:50 +00:00
}
firebase.initializeApp(firebaseConfig)
2020-01-20 16:55:48 +00:00
2020-01-23 02:21:23 +00:00
// a reference to the users collection
const usersCollection = firebase.firestore().collection("users")
2020-01-20 16:55:48 +00:00
// the shared state object that any vue component
// can get access to
2020-01-21 12:25:35 +00:00
export const fb = {
2020-05-10 01:40:32 +00:00
currentUser: null,
2020-01-23 13:37:36 +00:00
currentFeeds: [],
2020-01-21 12:25:35 +00:00
currentSettings: [],
2020-01-23 13:37:36 +00:00
currentHistory: [],
2020-01-25 06:51:47 +00:00
currentCollections: [],
2020-02-23 19:00:22 +00:00
currentEnvironments: [],
2020-01-23 13:37:36 +00:00
writeFeeds: async (message, label) => {
2020-01-20 16:55:48 +00:00
const dt = {
createdOn: new Date(),
2020-01-21 12:25:35 +00:00
author: fb.currentUser.uid,
author_name: fb.currentUser.displayName,
author_image: fb.currentUser.photoURL,
2020-01-22 01:57:38 +00:00
message,
2020-02-24 18:44:50 +00:00
label,
}
2020-01-25 06:51:47 +00:00
usersCollection
.doc(fb.currentUser.uid)
.collection("feeds")
2020-01-25 06:51:47 +00:00
.add(dt)
2020-05-10 01:40:32 +00:00
.catch((e) => console.error("error inserting", dt, e))
2020-01-21 03:02:43 +00:00
},
2020-05-10 01:40:32 +00:00
deleteFeed: (id) => {
2020-01-23 02:21:23 +00:00
usersCollection
.doc(fb.currentUser.uid)
.collection("feeds")
2020-01-21 03:02:43 +00:00
.doc(id)
.delete()
2020-05-10 01:40:32 +00:00
.catch((e) => console.error("error deleting", id, e))
2020-01-22 07:17:09 +00:00
},
2020-01-21 16:57:52 +00:00
writeSettings: async (setting, value) => {
2020-01-21 12:25:35 +00:00
const st = {
updatedOn: new Date(),
author: fb.currentUser.uid,
author_name: fb.currentUser.displayName,
author_image: fb.currentUser.photoURL,
2020-01-21 16:57:52 +00:00
name: setting,
2020-02-24 18:44:50 +00:00
value,
}
2020-01-25 06:51:47 +00:00
usersCollection
.doc(fb.currentUser.uid)
.collection("settings")
2020-01-25 06:51:47 +00:00
.doc(setting)
.set(st)
2020-05-10 01:40:32 +00:00
.catch((e) => console.error("error updating", st, e))
2020-01-23 13:37:36 +00:00
},
2020-05-10 01:40:32 +00:00
writeHistory: async (entry) => {
2020-02-24 18:44:50 +00:00
const hs = entry
2020-01-25 06:51:47 +00:00
usersCollection
.doc(fb.currentUser.uid)
.collection("history")
2020-01-25 06:51:47 +00:00
.add(hs)
2020-05-10 01:40:32 +00:00
.catch((e) => console.error("error inserting", hs, e))
2020-01-23 13:37:36 +00:00
},
2020-05-10 01:40:32 +00:00
deleteHistory: (entry) => {
2020-01-23 13:37:36 +00:00
usersCollection
.doc(fb.currentUser.uid)
.collection("history")
2020-01-23 13:37:36 +00:00
.doc(entry.id)
.delete()
2020-05-10 01:40:32 +00:00
.catch((e) => console.error("error deleting", entry, e))
2020-01-23 13:37:36 +00:00
},
clearHistory: () => {
usersCollection
.doc(fb.currentUser.uid)
.collection("history")
2020-01-23 13:37:36 +00:00
.get()
.then(({ docs }) => {
2020-05-10 01:40:32 +00:00
docs.forEach((e) => fb.deleteHistory(e))
2020-02-24 18:44:50 +00:00
})
},
toggleStar: (entry, value) => {
usersCollection
.doc(fb.currentUser.uid)
.collection("history")
.doc(entry.id)
.update({ star: value })
2020-05-10 01:40:32 +00:00
.catch((e) => console.error("error deleting", entry, e))
2020-01-25 06:51:47 +00:00
},
2020-05-10 01:40:32 +00:00
writeCollections: async (collection) => {
2020-01-25 06:51:47 +00:00
const cl = {
updatedOn: new Date(),
author: fb.currentUser.uid,
author_name: fb.currentUser.displayName,
author_image: fb.currentUser.photoURL,
2020-06-11 14:25:40 +00:00
collection,
2020-02-24 18:44:50 +00:00
}
2020-01-25 06:51:47 +00:00
usersCollection
.doc(fb.currentUser.uid)
.collection("collections")
.doc("sync")
2020-01-25 06:51:47 +00:00
.set(cl)
2020-05-10 01:40:32 +00:00
.catch((e) => console.error("error updating", cl, e))
2020-02-23 19:00:22 +00:00
},
2020-05-10 01:40:32 +00:00
writeEnvironments: async (environment) => {
2020-02-23 19:00:22 +00:00
const ev = {
updatedOn: new Date(),
author: fb.currentUser.uid,
author_name: fb.currentUser.displayName,
author_image: fb.currentUser.photoURL,
2020-06-11 14:25:40 +00:00
environment,
2020-02-24 18:44:50 +00:00
}
2020-02-23 19:00:22 +00:00
usersCollection
.doc(fb.currentUser.uid)
.collection("environments")
.doc("sync")
2020-02-23 19:00:22 +00:00
.set(ev)
2020-05-10 01:40:32 +00:00
.catch((e) => console.error("error updating", ev, e))
2020-02-24 18:44:50 +00:00
},
}
2020-01-20 16:55:48 +00:00
2020-01-23 02:21:23 +00:00
// When a user logs in or out, save that in the store
2020-05-10 01:40:32 +00:00
firebase.auth().onAuthStateChanged((user) => {
2020-01-23 02:21:23 +00:00
if (user) {
2020-02-24 18:44:50 +00:00
fb.currentUser = user
2020-05-10 01:40:32 +00:00
fb.currentUser.providerData.forEach((profile) => {
2020-01-23 02:21:23 +00:00
let us = {
updatedOn: new Date(),
provider: profile.providerId,
name: profile.displayName,
email: profile.email,
photoUrl: profile.photoURL,
2020-02-24 18:44:50 +00:00
uid: profile.uid,
}
2020-01-25 06:51:47 +00:00
usersCollection
.doc(fb.currentUser.uid)
.set(us)
2020-05-10 01:40:32 +00:00
.catch((e) => console.error("error updating", us, e))
2020-02-24 18:44:50 +00:00
})
2020-01-21 12:25:35 +00:00
2020-01-23 02:21:23 +00:00
usersCollection
.doc(fb.currentUser.uid)
.collection("feeds")
.orderBy("createdOn", "desc")
2020-05-10 01:40:32 +00:00
.onSnapshot((feedsRef) => {
2020-02-24 18:44:50 +00:00
const feeds = []
2020-05-10 01:40:32 +00:00
feedsRef.forEach((doc) => {
2020-02-24 18:44:50 +00:00
const feed = doc.data()
feed.id = doc.id
feeds.push(feed)
})
fb.currentFeeds = feeds
})
2020-01-20 16:55:48 +00:00
2020-01-23 13:37:36 +00:00
usersCollection
.doc(fb.currentUser.uid)
.collection("settings")
2020-05-10 01:40:32 +00:00
.onSnapshot((settingsRef) => {
2020-02-24 18:44:50 +00:00
const settings = []
2020-05-10 01:40:32 +00:00
settingsRef.forEach((doc) => {
2020-02-24 18:44:50 +00:00
const setting = doc.data()
setting.id = doc.id
settings.push(setting)
})
fb.currentSettings = settings
})
2020-01-23 13:37:36 +00:00
usersCollection
.doc(fb.currentUser.uid)
.collection("history")
2020-05-10 01:40:32 +00:00
.onSnapshot((historyRef) => {
2020-02-24 18:44:50 +00:00
const history = []
2020-05-10 01:40:32 +00:00
historyRef.forEach((doc) => {
2020-02-24 18:44:50 +00:00
const entry = doc.data()
entry.id = doc.id
history.push(entry)
})
fb.currentHistory = history
})
2020-01-25 06:51:47 +00:00
usersCollection
.doc(fb.currentUser.uid)
.collection("collections")
2020-05-10 01:40:32 +00:00
.onSnapshot((collectionsRef) => {
2020-02-24 18:44:50 +00:00
const collections = []
2020-05-10 01:40:32 +00:00
collectionsRef.forEach((doc) => {
2020-02-24 18:44:50 +00:00
const collection = doc.data()
collection.id = doc.id
collections.push(collection)
})
if (collections.length > 0) {
fb.currentCollections = collections[0].collection
}
2020-02-24 18:44:50 +00:00
})
2020-02-23 19:00:22 +00:00
usersCollection
.doc(fb.currentUser.uid)
.collection("environments")
2020-05-10 01:40:32 +00:00
.onSnapshot((environmentsRef) => {
2020-02-24 18:44:50 +00:00
const environments = []
2020-05-10 01:40:32 +00:00
environmentsRef.forEach((doc) => {
2020-02-24 18:44:50 +00:00
const environment = doc.data()
environment.id = doc.id
environments.push(environment)
})
if (environments.length > 0) {
fb.currentEnvironments = environments[0].environment
}
2020-02-24 18:44:50 +00:00
})
2020-01-23 02:21:23 +00:00
} else {
2020-02-24 18:44:50 +00:00
fb.currentUser = null
2020-01-23 02:21:23 +00:00
}
2020-02-24 18:44:50 +00:00
})