fix CreateAnswer

This commit is contained in:
“spicecat” 2022-08-02 18:42:22 -05:00
commit dc32a68fbc
2 changed files with 64 additions and 64 deletions

View file

@ -2,11 +2,10 @@ import { Button, ButtonGroup, ListItem, ListItemText, Tooltip } from '@mui/mater
import CheckIcon from '@mui/icons-material/Check';
import ReactMarkdown from 'react-markdown';
import CreationInfoTag from 'components/CreationInfoTag';
import { useUser } from 'contexts';
import { AnswerCommentsList, CommentControl, VoteControl } from 'controllers/QAControllers';
import { getAnswerVote, updateAcceptAnswer, updateAnswerVote, updateQuestion } from 'services/questionsServices';
import { useQuestion } from 'contexts';
import { postQuestionComment, postAnswerComment } from 'services/questionsServices';
import { AnswerCommentsList, CommentControl, VoteControl } from 'controllers/QAControllers';
import { getAnswerVote, postAnswerComment, updateAcceptAnswer, updateAnswerVote } from 'services/questionsServices';
export default function Answer({
accepted,
answer_id,
@ -18,19 +17,17 @@ export default function Answer({
text,
upvotes,
}) {
const { userData: { level, username } } = useUser();
const getVote = () => getAnswerVote(question_id, answer_id);
const updateVote = (data) => updateAnswerVote(question_id, answer_id, data);
const {permissions} = useQuestion();
const { permissions } = useQuestion();
let canVote = permissions.canVote;
let canComment = permissions.canComment;
let canAccept = permissions.canAccept;
const getVote = () => getAnswerVote(question_id, answer_id);
const updateVote = (data) => updateAnswerVote(question_id, answer_id, data);
function acceptAnswer(){
function acceptAnswer() {
updateAcceptAnswer(question_id, answer_id)
}
const postComment = (data) => postAnswerComment(question_id, answer_id, data);
@ -52,8 +49,14 @@ export default function Answer({
<ReactMarkdown>
{text}
</ReactMarkdown>
<CommentControl {...{canComment, postComment}} />
<Tooltip title = {!canAccept && "Only the creator can accept, or an answer is already accepted"}><span><Button onClick = {acceptAnswer} disabled = {!canAccept} style = {{'marginLeft':'10px'}}variant = "standard">Accept</Button></span></Tooltip>
<CommentControl {...{ canComment, postComment }} />
{canAccept && (
<span>
<Button onClick={acceptAnswer} style={{ 'marginLeft': '10px' }} variant="standard">
Accept
</Button>
</span>
)}
</ListItemText>
</ListItem>
<ListItem sx={{ pl: 8 }}>

View file

@ -8,14 +8,14 @@ import {
Button,
Tooltip,
} from '@mui/material';
import ReactMarkdown from 'react-markdown';
import { CreationInfoTag } from 'controllers';
import { CommentControl, VoteControl } from 'controllers/QAControllers';
import { getQuestionVote, postQuestionComment,openQuestion,protectQuestion , updateQuestion, closeQuestion, updateQuestionVote } from 'services/questionsServices';
import { Link } from 'react-router-dom';
import { useEffect, useState } from 'react';
import ReactMarkdown from 'react-markdown';
import { Link } from 'react-router-dom';
import { useQuestion, useUser } from 'contexts';
import CreateAnswer from './CreateAnswer';
import { CreationInfoTag } from 'controllers';
import { CommentControl, CreateAnswer, VoteControl } from 'controllers/QAControllers';
import { getQuestionVote, postQuestionComment, openQuestion, protectQuestion, updateQuestion, closeQuestion, updateQuestionVote } from 'services/questionsServices';
const statusColor = (status) => {
switch (status) {
case 'open':
@ -31,37 +31,37 @@ const statusColor = (status) => {
export default function Question({
answers,
close,
comments,
creator,
createdAt,
downvotes,
hasAcceptedAnswer,
protect,
reopen,
status,
title,
text,
question_id,
upvotes,
views,
protect,
close,
reopen
views
}) {
const {userData} = useUser();
const { userData } = useUser();
const [ongoingVote, setOngoingVote] = useState({})
const {setPermissions} = useQuestion();
function setVote(){
if(protect.length > 0){setOngoingVote({users : protect, type : "protect"})}
if(close.length > 0){setOngoingVote({users : close, type : "close"})}
if(reopen.length > 0){setOngoingVote({users : reopen, type : "open"})}
}
const { setPermissions } = useQuestion();
function setVote() {
if (protect.length) setOngoingVote({ users: protect, type: 'protect' })
if (close.length) setOngoingVote({ users: close, type: 'close' })
if (reopen.length) setOngoingVote({ users: reopen, type: 'open' })
}
let level = 0;
let protection = false;
const protection = status === 'protected' || status === 'closed';
let canProtect = false;
let canClose = false;
@ -69,47 +69,44 @@ export default function Question({
let canAnswer = true;
let canVote = true;
let canAccept = false;
if(userData.username){
if (userData.username) {
level = userData.level;
if(status === 'protected' || status === 'closed'){protection = true;}
if(userData.username === creator && !hasAcceptedAnswer){canAccept = true}
if(level >= 7 && ongoingVote.type !== "protect"){canClose = true}
if(level >= 6 && !protection && ongoingVote.type !== "close"){canProtect = true}
if((level >= 3 && !protection) || (status === 'protected' && level >= 5) || (userData.username === creator)){canComment = true}
if((status === 'protected' && level < 5) || status === 'closed'){canAnswer = false}
if(status === 'closed' || level < 2){canVote = false}
}else{
if (userData.username === creator && !hasAcceptedAnswer) { canAccept = true }
if (level >= 7 && ongoingVote.type !== "protect") { canClose = true }
if (level >= 6 && !protection && ongoingVote.type !== "close") { canProtect = true }
if ((level >= 3 && !protection) || (status === 'protected' && level >= 5) || (userData.username === creator)) { canComment = true }
if ((status === 'protected' && level < 5) || status === 'closed') { canAnswer = false }
if (status === 'closed' || level < 2) { canVote = false }
} else {
canAnswer = false;
}
useEffect(()=>{
setPermissions({canVote: canVote, canComment: canComment, canAccept: canAccept })
useEffect(() => {
setPermissions({ canVote: canVote, canComment: canComment, canAccept: canAccept })
setVote();
},[canVote, canComment, canAccept])
}, [canVote, canComment, canAccept])
useEffect(()=>{
updateQuestion(question_id, {views: "increment"})
},[])
useEffect(() => {
updateQuestion(question_id, { views: "increment" })
}, [])
function changeProtect(){
function changeProtect() {
protectQuestion(question_id, userData)
}
function changeClose(){
if(status === 'open'){
function changeClose() {
if (status === 'open') {
closeQuestion(question_id, userData)
}else{
} else {
openQuestion(question_id, userData)
}
}
const getVote = () => getQuestionVote(question_id);
const updateVote = (data) => updateQuestionVote(question_id, data);
const postComment = (data) => postQuestionComment(question_id, data);
return (
<>
<Box m={2}>
@ -130,17 +127,17 @@ export default function Question({
label={hasAcceptedAnswer ? 'yes' : 'no'}
size='small'
/>
<Button component={Link} to='../ask' style={{'marginLeft': '10px'}}display = 'inline' m = {1} variant = "contained">Ask question</Button>
<Button component={Link} to='../ask' style={{ 'marginLeft': '10px' }} display='inline' m={1} variant="contained">Ask question</Button>
<Tooltip title = {!canClose && "You must be level 7"}>
<Tooltip title={!canClose && "You must be level 7"}>
<span>
<Button disabled = {!canClose} style={{'marginLeft': '10px'}}display = 'inline' m = {1} variant = "contained" onClick = {changeClose}>Close/Open</Button>
<Button disabled={!canClose} style={{ 'marginLeft': '10px' }} display='inline' m={1} variant="contained" onClick={changeClose}>Close/Open</Button>
</span>
</Tooltip>
<Tooltip title = {!canProtect && "You must be level 6 and this question must be open" }>
<Tooltip title={!canProtect && "You must be level 6 and this question must be open"}>
<span>
<Button disabled = {!canProtect} style={{'marginLeft': '10px'}}display = 'inline' m = {1} variant = "contained" onClick = {changeProtect}>Protect</Button>
<Button disabled={!canProtect} style={{ 'marginLeft': '10px' }} display='inline' m={1} variant="contained" onClick={changeProtect}>Protect</Button>
</span>
</Tooltip>
@ -163,7 +160,7 @@ export default function Question({
<ReactMarkdown>
{text}
</ReactMarkdown>
<CommentControl {...{ postComment, canComment}} />
<CommentControl {...{ postComment, canComment }} />
</ListItemText>
</ListItem>
<CreateAnswer />