Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bhaskar-nair2/15d21ee5247c988f62bbb532dc47bb36 to your computer and use it in GitHub Desktop.
Save bhaskar-nair2/15d21ee5247c988f62bbb532dc47bb36 to your computer and use it in GitHub Desktop.
Firebase Channel Creator for VueX: The code makes a VueX store action which modifies the state according to data received from Firebase Firestore
channelBookings({ state, commit }, user_id) {
var book = [];
db.collection("bookings").onSnapshot(res => {
const changes = res.docChanges();
changes.forEach(change => {
switch (change.type) {
case "added":
book.push({
...change.doc.data(),
id: change.doc.id
});
commit("BOOKING_CHANNEL", { type: change.type, bookings: book });
break;
case "removed":
book = state.bookings;
var index = book
.map(boo => {
return boo.id;
})
.indexOf(change.doc.id);
commit("BOOKING_CHANNEL", {
type: change.type,
booking_index: index
});
break;
case "modified":
book = state.bookings;
var index = book
.map(boo => {
return boo.id;
})
.indexOf(change.doc.id);
commit("BOOKING_CHANNEL", {
type: change.type,
booking_index: index,
bookings: change.doc.data()
});
break;
}
});
});
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment