Skip to content

Commit

Permalink
update poll post try
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajistreo3 committed Jan 19, 2024
1 parent 5aa1c91 commit c1c9a78
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
6 changes: 3 additions & 3 deletions client/src/Components/FeedLaunchpad/FeedLaunchpad.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
65 changes: 52 additions & 13 deletions client/src/Components/PostsComponents/PollPost/PollPost.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -22,7 +24,10 @@ const PollPost = ({
pollSecondChoiceNumber,
pollThirdChoiceNumber,
pollFourthChoiceNumber,
}) => {
loggedInUser,
postId,
polldata,
}) {
const [surveyResults, setSurveyResults] = useState([
pollFirstChoiceNumber,
pollSecondChoiceNumber,
Expand Down Expand Up @@ -77,45 +82,81 @@ 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,
choice2: false,
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,
choice2: true,
choice3: false,
choice4: false,
});
choiceIndex = 1;
} else if (choiceNameEl.innerText === choiceName[2]) {
setChoiceSelected({
choice1: false,
choice2: false,
choice3: true,
choice4: false,
});
choiceIndex = 2;
} else if (choiceNameEl.innerText === choiceName[3]) {
setChoiceSelected({
choice1: false,
choice2: false,
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 (
<section className="pollpost-container">
<div className="pollpost-wrap">
Expand Down Expand Up @@ -180,10 +221,8 @@ const PollPost = ({
<div className="ageofpost-poll-wrap">
<div className="ageofpost-and-timeleft-poll">
<div>
{/* {pollTotalVote} */}
{/* shajeedpoll */}
votes</div>
<div>-</div>
votes: {totalVotes}
</div>
<div>
{/* {pollDate} */}
{/* {pollDateType} */}
Expand All @@ -195,5 +234,5 @@ const PollPost = ({
</div>
</section>
);
};
}
export default PollPost;
3 changes: 3 additions & 0 deletions client/src/Components/PostsComponents/PostsFeed/PostsFeed.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ function PostsFeed({
pollFourthChoiceNumber={pollFourthChoiceNumber}
// pollDate={pollDate}
pollDateType={pollDateType}
postId={id}
loggedInUser={loggedInUser}
polldata={polldata}
// pollTotalVote={pollTotalVote}
/>
);
Expand Down

0 comments on commit c1c9a78

Please sign in to comment.