fix voting

This commit is contained in:
“spicecat” 2022-07-22 20:13:03 -05:00
commit db1cd138bc
7 changed files with 45 additions and 45 deletions

View file

@ -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 (
<span key={_id}>
<span key={answer_id}>
<ListItem disablePadding>
<ButtonGroup orientation='vertical'>
<VoteControl {...{ downvotes, getVote, orientation: 'vertical', updateVote, upvotes }} />
@ -43,7 +43,7 @@ export default function Answer({
</ListItem>
<ListItem sx={{ pl: 8 }}>
<AnswerProvider>
<AnswerCommentsList {...{ answer_id: _id, comments }} />
<AnswerCommentsList {...{ answer_id, comments }} />
</AnswerProvider>
</ListItem>
</span>

View file

@ -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);

View file

@ -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 (
<span key={comment_id}>

View file

@ -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 (
<>

View file

@ -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({
<span style={{ marginRight: 8 }}>
<ButtonGroup {...{ orientation, style: { alignItems: 'center' } }}>
<Tooltip title='upvote' placement='right'>
<ThumbUpOutlinedIcon
color={vote === 'upvoted' ? 'warning' : 'standard'}
<IconButton
disabled={disabled}
onClick={handleUpvote}
/>
>
<ThumbUpOutlinedIcon color={vote === 'upvoted' ? 'warning' : 'standard'} />
</IconButton>
</Tooltip>
<Tooltip title={`${upvotes} / ${downvotes}`} placement='right'>
<Typography style={{ margin: 4, textAlign: 'center' }}>
@ -26,10 +29,12 @@ export default function VoteControl({
</Typography>
</Tooltip>
<Tooltip title='downvote' placement='right'>
<ThumbDownOutlinedIcon
color={vote === 'downvoted' ? 'warning' : 'standard'}
onClick={handleDownvote}
/>
<IconButton
disabled={disabled}
onClick={handleUpvote}
>
<ThumbDownOutlinedIcon color={vote === 'downvoted' ? 'warning' : 'standard'} />
</IconButton>
</Tooltip>
</ButtonGroup>
</span >

View file

@ -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,

View file

@ -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,