Skip to content

Commit

Permalink
temp: default likes while troubleshooting
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Jun 5, 2024
1 parent c95fed1 commit edbdf9f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
node_modules
/dist

pat


# local env files
.env.local
Expand Down
2 changes: 2 additions & 0 deletions pat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
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
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
60 changes: 31 additions & 29 deletions src/views/ViewBlog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ export default {
},
data() {
return {
currentBlog: null,
currentBlog: {
likes: 27,
},
liked: false,
comments: [],
newComment: '',
isLiking: false, // To prevent multiple like actions at the same time
};
},
async mounted() {
Expand All @@ -95,7 +96,6 @@ export default {
this.fetchComments();
// Check if user has already liked the post
firebase.auth().onAuthStateChanged(async (user) => {
if (user) {
const userLikesDoc = await db.collection('userLikes').doc(user.uid).get();
Expand All @@ -113,47 +113,49 @@ export default {
this.$router.push({ name: 'Register' });
return;
}
if (this.isLiking) return; // Prevent multiple like actions
this.isLiking = true;
const blogId = this.$route.params.blogid;
const db = firebase.firestore();
const blogRef = db.collection('blogPosts').doc(blogId);
const userLikesRef = db.collection('userLikes').doc(user.uid);
try {
await db.runTransaction(async (transaction) => {
const blogDoc = await transaction.get(blogRef);
const userLikesDoc = await transaction.get(userLikesRef);
const userLikesDoc = await db.collection('userLikes').doc(user.uid).get();
if (!userLikesDoc.exists) {
await db.collection('userLikes').doc(user.uid).set({ likedPosts: [] });
}
if (!blogDoc.exists) throw new Error('Blog post does not exist');
await db.runTransaction(async (transaction) => {
const doc = await transaction.get(blogRef);
if (!doc.exists) {
throw "Blog post does not exist!";
}
const currentLikes = blogDoc.data().likes;
const likedPosts = userLikesDoc.exists ? userLikesDoc.data().likedPosts || [] : [];
let newLikes;
let data = doc.data();
let likes = data.likes || 0;
if (this.liked) {
newLikes = currentLikes - 1;
const index = likedPosts.indexOf(blogId);
if (index > -1) likedPosts.splice(index, 1);
this.liked = false;
} else {
newLikes = currentLikes + 1;
likedPosts.push(blogId);
// Toggle like status
if (!this.liked) {
likes++;
data.likes = likes;
this.liked = true;
} else {
likes--;
data.likes = likes;
this.liked = false;
}
transaction.update(blogRef, { likes: newLikes });
transaction.set(userLikesRef, { likedPosts }, { merge: true });
this.currentBlog.likes = newLikes;
transaction.update(blogRef, { likes });
let userLikes = (await transaction.get(db.collection('userLikes').doc(user.uid))).data().likedPosts;
if (!userLikes.includes(blogId)) {
userLikes.push(blogId);
} else {
userLikes = userLikes.filter(id => id !== blogId);
}
transaction.update(db.collection('userLikes').doc(user.uid), { likedPosts: userLikes });
});
} catch (error) {
console.error('Transaction failed: ', error);
alert('An error occurred while updating the like status. Please try again.');
} finally {
this.isLiking = false;
}
},
async fetchComments() {
Expand Down

0 comments on commit edbdf9f

Please sign in to comment.