Created
March 5, 2019 10:30
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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