Skip to content

Commit

Permalink
finished css and small bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vanesssalai committed Jul 9, 2024
1 parent 936a8bc commit bde5f3b
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/components/forumcards/ForumCards.css
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
display: inline-block;
}

.forum-card-like {
.forum-card-like, .forum-card-comment {
display: flex;
align-items: center;
gap: 2px;
Expand Down
26 changes: 26 additions & 0 deletions src/components/forumcards/ForumCards.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function ForumCards({ forumDetail }) {
const [showResults, setShowResults] = useState(false);
const [isLiked, setIsLiked] = useState(false);
const [likeCount, setLikeCount] = useState(0);
const [commentCount, setCommentCount] = useState(0);

useEffect(() => {
const fetchUser = async () => {
Expand All @@ -52,6 +53,22 @@ function ForumCards({ forumDetail }) {
}
};

const fetchAndCountComments = async () => {
const commentsRef = collection(db, 'Forum', forumDetail.id, 'Comments');
const commentsSnapshot = await getDocs(commentsRef);

const fetchedComments = await Promise.all(commentsSnapshot.docs.map(async doc => {
const comment = { id: doc.id, ...doc.data() };
const repliesRef = collection(db, 'Forum', forumDetail.id, 'Comments', doc.id, 'Replies');
const repliesSnapshot = await getDocs(repliesRef);
comment.replies = repliesSnapshot.docs.map(replyDoc => ({ id: replyDoc.id, ...replyDoc.data() }));
return comment;
}));

const totalCount = countCommentsAndReplies(fetchedComments);
setCommentCount(totalCount);
};

const checkLikeStatus = async () => {
if (currentUser) {
const likesRef = collection(db, 'Forum', forumDetail.id, 'Likes');
Expand All @@ -70,6 +87,7 @@ function ForumCards({ forumDetail }) {
fetchUser();
checkLikeStatus();
getLikeCount();
fetchAndCountComments();

if (currentUser) {
if (currentUser.uid === forumDetail.userID) {
Expand Down Expand Up @@ -251,6 +269,12 @@ function ForumCards({ forumDetail }) {
}
};

const countCommentsAndReplies = (comments) => {
return comments.reduce((total, comment) => {
return total + 1 + (comment.replies ? countCommentsAndReplies(comment.replies) : 0);
}, 0);
};

const tagColors = {
"Questions": "#FF4B2B",
"Modding": "#4CAF50",
Expand Down Expand Up @@ -360,6 +384,7 @@ function ForumCards({ forumDetail }) {
</div>
<div className="forum-card-comment forum-card-icon">
<FaCommentDots fill="grey"/>
<span>{commentCount}</span>
</div>
</div>
<div className="forum-card-share forum-card-icon" onClick={handleShare}>
Expand All @@ -370,4 +395,5 @@ function ForumCards({ forumDetail }) {
</div>
);
}

export default ForumCards;
37 changes: 18 additions & 19 deletions src/components/header/likecounter/LikeCounter.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { createContext, useContext, useState, useEffect } from 'react';
import React, { createContext, useContext, useState, useEffect, useCallback } from 'react';
import { db } from '../../../lib/firebaseConfig';
import { doc, getDoc, setDoc, onSnapshot, collection } from 'firebase/firestore';
import { useAuth } from '../../../Auth';
Expand All @@ -11,28 +11,27 @@ export const LikeCountProvider = ({ children }) => {
const { currentUser } = useAuth();
const [likeCount, setLikeCount] = useState(0);

useEffect(() => {
const fetchLikeCount = useCallback(async () => {
if (currentUser) {
const fetchLikeCount = async () => {
const likeCountDoc = doc(db, 'Users', currentUser.uid, 'likeCount', 'counter');
const likeCountDoc = doc(db, 'Users', currentUser.uid, 'likeCount', 'counter');
try {
const likeCountSnapshot = await getDoc(likeCountDoc);

if (likeCountSnapshot.exists()) {
setLikeCount(likeCountSnapshot.data().count);
} else {
try {
await setDoc(likeCountDoc, { count: 0 });
setLikeCount(0);
} catch (err) {
console.log(err.message);
}
await setDoc(likeCountDoc, { count: 0 });
setLikeCount(0);
}
};

fetchLikeCount();
} catch (err) {
console.log(err.message);
}
}
}, [currentUser]);

useEffect(() => {
fetchLikeCount();
}, [fetchLikeCount]);

useEffect(() => {
if (currentUser) {
const unsubscribe = onSnapshot(doc(db, 'Users', currentUser.uid, 'likeCount', 'counter'), (doc) => {
Expand All @@ -52,21 +51,21 @@ export const LikeCountProvider = ({ children }) => {
}
};

const increaseLikeCount = () => {
const increaseLikeCount = useCallback(() => {
setLikeCount((prevCount) => {
const newCount = prevCount + 1;
updateLikeCountInFirestore(newCount);
return newCount;
});
};
}, [updateLikeCountInFirestore]);

const decreaseLikeCount = () => {
const decreaseLikeCount = useCallback(() => {
setLikeCount((prevCount) => {
const newCount = prevCount > 0 ? prevCount - 1 : 0;
updateLikeCountInFirestore(newCount);
return newCount;
});
};
}, [updateLikeCountInFirestore]);

useEffect(() => {
if (currentUser) {
Expand All @@ -82,7 +81,7 @@ export const LikeCountProvider = ({ children }) => {

return () => unsubscribe();
}
}, [currentUser]);
}, [currentUser, decreaseLikeCount]);

return (
<LikeCounter.Provider value={{ likeCount, increaseLikeCount, decreaseLikeCount }}>
Expand Down
7 changes: 5 additions & 2 deletions src/pages/forumpost/ForumPost.css
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@
display: inline-block;
}

.forum-card-like {
.forum-card-like, .forum-card-comment {
display: flex;
align-items: center;
gap: 2px;
Expand Down Expand Up @@ -310,7 +310,6 @@

.comment {
border-left: 1px solid rgb(194, 194, 194);
margin: 16px;
padding: 8px;

.comment-header {
Expand All @@ -323,6 +322,7 @@

.comment-profile {
display: flex;
cursor: pointer;
flex-direction: row;
align-items: center;
gap: 4px;
Expand All @@ -339,6 +339,7 @@

.comment-content {
margin: 0;
padding-bottom: 8px;
}

.comment-reply-button {
Expand All @@ -363,6 +364,8 @@
}

.comment-reply {
margin: 8px 0;

textarea {
font-size: 16px;
border-radius: 12px;
Expand Down
Loading

0 comments on commit bde5f3b

Please sign in to comment.