Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions client/src/components/Poll/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,30 @@ import { CREATE_VOTE } from "../../utils/mutations";
import Auth from "../../utils/auth";

const Poll = ({ poll }) => {
const [voteFeedback, setVoteFeedback] = useState('');
const [createVote, { error, data }] = useMutation(CREATE_VOTE);
const currentUserId = Auth.getProfile().authenticatedPerson._id;

let hasUserVoted = false;
if (Auth.loggedIn()) {
const currentUserId = Auth.getProfile().authenticatedPerson._id;

const hasUserVoted = poll.votes.some(
(vote) => vote.user._id === currentUserId
);
console.log(hasUserVoted, poll.header);
console.log(poll);
hasUserVoted = poll.votes.some(
(vote) => vote.user._id === currentUserId
);

//console.log(hasUserVoted, poll.header);
//console.log(poll);
}



const handleChoiceClick = async (choiceId) => {
if (!poll || !poll._id) {
console.error("Poll or poll ID is undefined");
return;
} else if (hasUserVoted) {
setVoteFeedback('Already Voted');
return;
}

console.log("Choice Id:", choiceId);
Expand All @@ -27,11 +38,18 @@ const Poll = ({ poll }) => {
const { data } = await createVote({
variables: { pollId: poll._id, choiceId: choiceId },
});

window.location.reload();
} catch (error) {
console.error("error:", error);
}
};

//Prompts user to login to vote
const ifLoggedOut = () => {
setVoteFeedback('Please login to vote');
}

return (
<div className="poll" id={poll._id}>
<div>
Expand All @@ -43,11 +61,12 @@ const Poll = ({ poll }) => {
className="pollChoice"
id={choice._id}
key={index}
onClick={hasUserVoted ? null : () => handleChoiceClick(choice._id)}
onClick={Auth.loggedIn() ? () => handleChoiceClick(choice._id) : ifLoggedOut}
>
{choice.text}
</p>
))}
<p className="voteFeedback">{voteFeedback}</p>
<p className="createdBy">Created by: {poll.creator.username}</p>
</div>
);
Expand Down