From c1c9a7838312c260941f78d5c54b33920970e96a Mon Sep 17 00:00:00 2001 From: shajeed Date: Thu, 18 Jan 2024 22:42:35 -0500 Subject: [PATCH] update poll post try --- .../Components/FeedLaunchpad/FeedLaunchpad.js | 6 +- .../PostsComponents/PollPost/PollPost.js | 65 +++++++++++++++---- .../PostsComponents/PostsFeed/PostsFeed.js | 3 + 3 files changed, 58 insertions(+), 16 deletions(-) diff --git a/client/src/Components/FeedLaunchpad/FeedLaunchpad.js b/client/src/Components/FeedLaunchpad/FeedLaunchpad.js index 2389e2bf..888e522e 100644 --- a/client/src/Components/FeedLaunchpad/FeedLaunchpad.js +++ b/client/src/Components/FeedLaunchpad/FeedLaunchpad.js @@ -24,9 +24,9 @@ const FeedLaunchpad = () => { return { // launchpad: feedLaunchpad, launch_date: feedLaunchpad.launch_date, - collection_avatar: nftCollectionsData.collection_avatar, - collection_title: nftCollectionsData.collection_title, - collection_address: nftCollectionsData.collection_address, + collection_avatar: nftCollectionsData?.collection_avatar, + collection_title: nftCollectionsData?.collection_title, + collection_address: nftCollectionsData?.collection_address, athlete_id: userData.id, display_name: userData.display_name, profile_avatar: userData.profile_avatar, diff --git a/client/src/Components/PostsComponents/PollPost/PollPost.js b/client/src/Components/PostsComponents/PollPost/PollPost.js index fcc770af..1345133a 100644 --- a/client/src/Components/PostsComponents/PollPost/PollPost.js +++ b/client/src/Components/PostsComponents/PollPost/PollPost.js @@ -6,9 +6,11 @@ import AddCommentInput from "../AddCommentInput/AddCommentInput"; import checkMark from "../../../Assets/Image/checkmark.svg"; import DropDownMenu from "../DropDownMenu/DropDownMenu"; import ProgressBarPollPost from "./ProgressBarPollPost/ProgressBarPollPost"; +import { db } from "../../../Configs/firebase"; +import { updateDoc, doc, getDoc } from "firebase/firestore"; import { Link } from "react-router-dom"; -const PollPost = ({ +function PollPost({ choiceNumber, pollFirstChoice, pollSecondChoice, @@ -22,7 +24,10 @@ const PollPost = ({ pollSecondChoiceNumber, pollThirdChoiceNumber, pollFourthChoiceNumber, -}) => { + loggedInUser, + postId, + polldata, +}) { const [surveyResults, setSurveyResults] = useState([ pollFirstChoiceNumber, pollSecondChoiceNumber, @@ -77,9 +82,10 @@ const PollPost = ({ choiceNumber4, ]; - const showSurveyResult = (e) => { + const showSurveyResult = async (e) => { setIsVoted(true); const choiceNameEl = e.target.querySelector(".choice-name"); + let choiceIndex; if (choiceNameEl.innerText === choiceName[0]) { setChoiceSelected({ choice1: true, @@ -87,11 +93,7 @@ const PollPost = ({ choice3: false, choice4: false, }); - // créer un tableau dans l'index 0 du tableau choices qui est déjà dans firebase - // dans firebase choices est un tableau avec 4 index. - // quand l'index est 0 par exemple alors on push l'id de l'utilisateur dans un tableau que l'on créer dans l'index 0 de choices - // shajeedpoll - + choiceIndex = 0; } else if (choiceNameEl.innerText === choiceName[1]) { setChoiceSelected({ choice1: false, @@ -99,6 +101,7 @@ const PollPost = ({ choice3: false, choice4: false, }); + choiceIndex = 1; } else if (choiceNameEl.innerText === choiceName[2]) { setChoiceSelected({ choice1: false, @@ -106,6 +109,7 @@ const PollPost = ({ choice3: true, choice4: false, }); + choiceIndex = 2; } else if (choiceNameEl.innerText === choiceName[3]) { setChoiceSelected({ choice1: false, @@ -113,9 +117,46 @@ const PollPost = ({ choice3: false, choice4: true, }); + choiceIndex = 3; + } + + // Firestore update + if (choiceIndex !== undefined) { + const feedPostRef = doc(db, "feed_post", postId); + try { + const docSnap = await getDoc(feedPostRef); + + if (docSnap.exists()) { + let pollChoices = docSnap.data().pollData.choices; + + // Initialize userIds array if it doesn't exist + if (!pollChoices[choiceIndex].userIds) { + pollChoices[choiceIndex].userIds = []; + } + + // Add the loggedInUser's UID if not already included + if (!pollChoices[choiceIndex].userIds.includes(loggedInUser.id)) { + pollChoices[choiceIndex].userIds.push(loggedInUser.id); + } + + await updateDoc(feedPostRef, { + "pollData.choices": pollChoices, + }); + + console.log("Poll data updated successfully!"); + } else { + console.log("No such document!"); + } + } catch (error) { + console.error("Error updating poll data: ", error); + } } }; + const totalVotes = polldata?.choices.reduce((total, choice) => { + return total + (choice.userIds ? choice.userIds.length : 0); + }, 0); + return (
@@ -180,10 +221,8 @@ const PollPost = ({
- {/* {pollTotalVote} */} - {/* shajeedpoll */} - votes
-
-
+ votes: {totalVotes} +
{/* {pollDate} */} {/* {pollDateType} */} @@ -195,5 +234,5 @@ const PollPost = ({
); -}; +} export default PollPost; diff --git a/client/src/Components/PostsComponents/PostsFeed/PostsFeed.js b/client/src/Components/PostsComponents/PostsFeed/PostsFeed.js index 29fdb0b2..2215fd3d 100644 --- a/client/src/Components/PostsComponents/PostsFeed/PostsFeed.js +++ b/client/src/Components/PostsComponents/PostsFeed/PostsFeed.js @@ -112,6 +112,9 @@ function PostsFeed({ pollFourthChoiceNumber={pollFourthChoiceNumber} // pollDate={pollDate} pollDateType={pollDateType} + postId={id} + loggedInUser={loggedInUser} + polldata={polldata} // pollTotalVote={pollTotalVote} /> );