api-client/functions/fb.js

99 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-01-20 16:55:48 +00:00
import firebase from "firebase/app";
import "firebase/firestore";
import "firebase/auth";
// Initialize Firebase, copied from cloud console
2020-01-20 17:31:31 +00:00
const firebaseConfig = {
2020-01-20 16:55:48 +00:00
apiKey: "AIzaSyCMsFreESs58-hRxTtiqQrIcimh4i1wbsM",
authDomain: "postwoman-api.firebaseapp.com",
databaseURL: "https://postwoman-api.firebaseio.com",
projectId: "postwoman-api",
storageBucket: "postwoman-api.appspot.com",
messagingSenderId: "421993993223",
appId: "1:421993993223:web:ec0baa8ee8c02ffa1fc6a2",
measurementId: "G-ERJ6025CEB"
};
2020-01-20 17:31:31 +00:00
firebase.initializeApp(firebaseConfig);
2020-01-20 16:55:48 +00:00
2020-01-21 03:02:43 +00:00
// a reference to the Feeds collection
const feedsCollection = firebase.firestore().collection("feeds");
2020-01-21 12:25:35 +00:00
const settingsCollection = firebase.firestore().collection("settings");
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-01-21 03:02:43 +00:00
feedsInFeed: [],
currentUser: {},
2020-01-21 12:25:35 +00:00
currentSettings: [],
2020-01-22 01:57:38 +00:00
writeFeed: 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,
label
2020-01-20 16:55:48 +00:00
};
2020-01-21 12:25:35 +00:00
try {
return feedsCollection.add(dt);
} catch (e) {
return console.error("error inserting", dt, e);
}
2020-01-21 03:02:43 +00:00
},
2020-01-21 12:25:35 +00:00
deleteFeed: id =>
feedsCollection
2020-01-21 03:02:43 +00:00
.doc(id)
.delete()
2020-01-21 12:25:35 +00:00
.catch(e => console.error("error deleting", dt, e)),
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-01-21 12:25:35 +00:00
value
};
try {
2020-01-21 16:57:52 +00:00
return settingsCollection.doc(setting).set(st);
2020-01-21 12:25:35 +00:00
} catch (e) {
2020-01-21 16:57:52 +00:00
return console.error("error updating", st, e);
2020-01-21 12:25:35 +00:00
}
2020-01-20 16:55:48 +00:00
}
};
// onSnapshot is executed every time the data
// in the underlying firestore collection changes
// It will get passed an array of references to
// the documents that match your query
2020-01-21 03:02:43 +00:00
feedsCollection
2020-01-20 16:55:48 +00:00
.orderBy("createdOn", "desc")
2020-01-21 03:02:43 +00:00
// .limit(0)
.onSnapshot(feedsRef => {
const feeds = [];
feedsRef.forEach(doc => {
const feed = doc.data();
feed.id = doc.id;
feeds.push(feed);
2020-01-20 16:55:48 +00:00
});
2020-01-21 12:25:35 +00:00
fb.feedsInFeed = feeds;
});
settingsCollection
2020-01-21 16:57:52 +00:00
// .orderBy("updatedOn", "desc")
// .limit(2)
2020-01-21 12:25:35 +00:00
.onSnapshot(settingsRef => {
const settings = [];
settingsRef.forEach(doc => {
const setting = doc.data();
setting.id = doc.id;
settings.push(setting);
});
fb.currentSettings = settings;
2020-01-20 16:55:48 +00:00
});
// When a user logs in or out, save that in the store
firebase.auth().onAuthStateChanged(user => {
2020-01-21 12:25:35 +00:00
fb.currentUser = user;
2020-01-20 16:55:48 +00:00
});