Hello, and please forgive me if this question seems too basic, but I'm just starting out and feeling a bit overwhelmed.
I'm attempting to allow a user to vote on and then remove their vote from an item posted by another user using the same button. Essentially, I'm trying to create a functionality similar to the favorite feature on Twitter. I've been able to implement the voting part, but I'm struggling with removing the vote.
Here is the code snippet for the upvote (which is working for me):
import { mapState } from 'vuex'
const fb = require('../firebaseConfig.js')
export default {
computed: {
...mapState(['userProfile', 'currentUser', 'items'])
},
methods: {
upvoteItem(itemId, itemUpvotes) {
let docId = `${this.currentUser.uid}_${itemId}`
fb.upvotesCollection.doc(docId).get().then(doc => {
if (doc.exists) { return }
fb.upvotesCollection.doc(docId).set({
itemId: itemId,
userId: this.currentUser.uid
}).then(() => {
// update item upvotes
fb.itemsCollection.doc(itemId).update({
upvotes: itemUpvotes + 1,
})
})
}).catch(err => {
console.log(err)
})
},
}
}
And here is the HTML for triggering the upvote (and displaying the counter):
<button @click="upvoteItem(item.id, item.upvotes)">{{ item.upvotes }}</button>
Any suggestions on how to proceed?