From db1cd138bc62882fe028839fc4eb91fe7eed8bc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cspicecat=E2=80=9D?= Date: Fri, 22 Jul 2022 20:13:03 -0500 Subject: [PATCH] fix voting --- client/src/components/QAComponents/Answer.jsx | 10 ++--- .../components/QAComponents/AnswerComment.jsx | 4 +- .../src/components/QAComponents/Comment.jsx | 4 +- .../src/components/QAComponents/Question.jsx | 4 +- .../components/QAComponents/VoteControl.jsx | 21 ++++++---- .../QAControllers/VoteControlController.js | 38 +++++++++---------- server/routes/question/EditQuestionVote.js | 11 +++--- 7 files changed, 46 insertions(+), 46 deletions(-) diff --git a/client/src/components/QAComponents/Answer.jsx b/client/src/components/QAComponents/Answer.jsx index f7d14c9..1feb716 100644 --- a/client/src/components/QAComponents/Answer.jsx +++ b/client/src/components/QAComponents/Answer.jsx @@ -8,7 +8,7 @@ import { getAnswerVote, updateAnswerVote } from 'services/questionsServices'; export default function Answer({ accepted, - _id, + _id: answer_id, comments, creator, createdAt, @@ -17,11 +17,11 @@ export default function Answer({ text, upvotes }) { - const getVote = (username) => getAnswerVote(question_id, _id, username); - const updateVote = (username, data) => updateAnswerVote(question_id, _id, username, data); + const getVote = () => getAnswerVote(question_id, answer_id); + const updateVote = (data) => updateAnswerVote(question_id, answer_id, data); return ( - + @@ -43,7 +43,7 @@ export default function Answer({ - + diff --git a/client/src/components/QAComponents/AnswerComment.jsx b/client/src/components/QAComponents/AnswerComment.jsx index c809b51..a139db8 100644 --- a/client/src/components/QAComponents/AnswerComment.jsx +++ b/client/src/components/QAComponents/AnswerComment.jsx @@ -17,8 +17,8 @@ export default function AnswerComment({ }) { const { answerData, setAnswerData } = useAnswer(); - const getVote = (username) => getAnswerCommentVote(question_id, answer_id, comment_id, username); - const updateVote = (username, data) => updateAnswerCommentVote(question_id, answer_id, comment_id, username, data); + const getVote = () => getAnswerCommentVote(question_id, answer_id, comment_id); + const updateVote = (data) => updateAnswerCommentVote(question_id, answer_id, comment_id, data); useEffect(() => { setAnswerData(answerData + answer_id); diff --git a/client/src/components/QAComponents/Comment.jsx b/client/src/components/QAComponents/Comment.jsx index f04e50b..c4164fe 100644 --- a/client/src/components/QAComponents/Comment.jsx +++ b/client/src/components/QAComponents/Comment.jsx @@ -12,8 +12,8 @@ export default function Comment({ text, upvotes }) { - const getVote = (username) => getCommentVote(question_id, comment_id, username); - const updateVote = (username, data) => updateCommentVote(question_id, comment_id, username, data); + const getVote = () => getCommentVote(question_id, comment_id); + const updateVote = (data) => updateCommentVote(question_id, comment_id, data); return ( diff --git a/client/src/components/QAComponents/Question.jsx b/client/src/components/QAComponents/Question.jsx index 1e5c70c..8e2dda8 100644 --- a/client/src/components/QAComponents/Question.jsx +++ b/client/src/components/QAComponents/Question.jsx @@ -27,8 +27,8 @@ export default function Question({ upvotes, views }) { - const getVote = (username) => getQuestionVote(question_id, username); - const updateVote = (username, data) => updateQuestionVote(question_id, username, data); + const getVote = () => getQuestionVote(question_id); + const updateVote = (data) => updateQuestionVote(question_id, data); return ( <> diff --git a/client/src/components/QAComponents/VoteControl.jsx b/client/src/components/QAComponents/VoteControl.jsx index 63fdb76..47dae51 100644 --- a/client/src/components/QAComponents/VoteControl.jsx +++ b/client/src/components/QAComponents/VoteControl.jsx @@ -1,8 +1,9 @@ -import { ButtonGroup, Tooltip, Typography } from '@mui/material'; +import { ButtonGroup, IconButton, Tooltip, Typography } from '@mui/material'; import ThumbUpOutlinedIcon from '@mui/icons-material/ThumbUpOutlined'; import ThumbDownOutlinedIcon from '@mui/icons-material/ThumbDownOutlined'; export default function VoteControl({ + disabled, downvotes, handleDownvote, handleUpvote, @@ -15,10 +16,12 @@ export default function VoteControl({ - + > + + @@ -26,10 +29,12 @@ export default function VoteControl({ - + + + diff --git a/client/src/controllers/QAControllers/VoteControlController.js b/client/src/controllers/QAControllers/VoteControlController.js index 3bc470d..9c85f93 100644 --- a/client/src/controllers/QAControllers/VoteControlController.js +++ b/client/src/controllers/QAControllers/VoteControlController.js @@ -9,41 +9,36 @@ export default function VoteControlController({ updateVote, upvotes, }) { - const { userData: { level, username } } = useUser(); + const { userData: { level } } = useUser(); + const [disabled, setDisabled] = useState() const [vote, setVote] = useState() const [original, setOriginal] = useState() const loadVote = async () => { if (level >= 2) { - const { success, vote: newVote } = await getVote(username); - if (success) - setVote(newVote || 'none'); + const { vote: newVote } = await getVote(); + setVote(newVote); return newVote; } } const handleDownvote = async () => { - if (level >= 2) { - if (vote === 'downvoted') - await updateVote(username, { operation: 'decrement' }); - else { - if (vote === 'upvoted') - await updateVote(username, { operation: 'decrement' }); - await updateVote(username, { operation: 'increment' }); - } - await loadVote(); + if (level >= 4) { + setDisabled(true); + const { vote: newVote } = await updateVote({ operation: 'downvote' }); + if (newVote !== undefined) + setVote(newVote); + setDisabled(false); } } + const handleUpvote = async () => { if (level >= 2) { - if (vote === 'upvoted') - await updateVote(username, { operation: 'decrement' }); - else { - if (vote === 'downvoted') - await updateVote(username, { operation: 'decrement' }); - await updateVote(username, { operation: 'increment' }); - } - await loadVote(); + setDisabled(true); + const { vote: newVote } = await updateVote({ operation: 'upvote' }); + if (newVote !== undefined) + setVote(newVote); + setDisabled(false); } } @@ -55,6 +50,7 @@ export default function VoteControlController({ return downvotes !== undefined && ( VoteControl({ + disabled, downvotes: downvotes + (vote === 'downvoted') - (original === 'downvoted'), handleDownvote, handleUpvote, diff --git a/server/routes/question/EditQuestionVote.js b/server/routes/question/EditQuestionVote.js index 136daca..7cae509 100644 --- a/server/routes/question/EditQuestionVote.js +++ b/server/routes/question/EditQuestionVote.js @@ -13,17 +13,16 @@ async function EditQuestionVote(req, res) { if (!operation) return res.status(400).send(config.errorIncomplete); const userLevel = getUserLevel(user.points); - if (operation === 'upvote' && userLevel < 2) { - return res.status(403).send(config.errorForbidden); - } - if (operation === 'downvote' && userLevel < 4) { + if (operation === 'upvote' && userLevel < 2) + return res.status(403).send(config.errorForbidden); + + if (operation === 'downvote' && userLevel < 4) return res.status(403).send(config.errorForbidden); - } const URL = `/questions/${questionID}/vote/${user.username}`; - const cachedQuestion = await Question.findByID(questionID); + const cachedQuestion = await Question.findById(questionID); let cachedVote = await Vote.findOneAndDelete({ parentID: questionID, creator: user.username,