api-client/functions/fb.js

174 lines
4.4 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-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-01-21 03:02:43 +00:00
currentUser: {},
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: [],
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,
label
2020-01-20 16:55:48 +00:00
};
2020-01-21 12:25:35 +00:00
try {
2020-01-23 02:21:23 +00:00
return usersCollection
.doc(fb.currentUser.uid)
.collection("feeds")
.add(dt);
2020-01-21 12:25:35 +00:00
} catch (e) {
return console.error("error inserting", dt, e);
}
2020-01-21 03:02:43 +00:00
},
2020-01-22 07:17:09 +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-01-23 13:37:36 +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-01-21 12:25:35 +00:00
value
};
try {
2020-01-23 02:21:23 +00:00
return usersCollection
.doc(fb.currentUser.uid)
.collection("settings")
2020-01-23 13:37:36 +00:00
.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-23 13:37:36 +00:00
},
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));
});
},
toggleStar: (entry, value) => {
usersCollection
.doc(fb.currentUser.uid)
.collection("history")
.doc(entry.id)
.update({ star: value })
.catch(e => console.error("error deleting", entry, e));
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
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
};
try {
usersCollection.doc(fb.currentUser.uid).set(us);
} catch (e) {
console.error("error updating", us, e);
}
2020-01-20 16:55:48 +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")
.onSnapshot(feedsRef => {
const feeds = [];
feedsRef.forEach(doc => {
const feed = doc.data();
feed.id = doc.id;
feeds.push(feed);
});
2020-01-23 13:37:36 +00:00
fb.currentFeeds = feeds;
2020-01-23 02:21:23 +00:00
});
2020-01-20 16:55:48 +00:00
2020-01-23 13:37:36 +00:00
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);
2020-01-23 02:21:23 +00:00
});
2020-01-23 13:37:36 +00:00
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;
});
2020-01-23 02:21:23 +00:00
} else {
fb.currentUser = null;
}
2020-01-20 16:55:48 +00:00
});