Fixed QA components
This commit is contained in:
parent
27bc21e2cf
commit
49605a6333
6 changed files with 85 additions and 34 deletions
|
|
@ -1,21 +1,38 @@
|
|||
import { Button, Card, CardContent, Tooltip } from "@mui/material"
|
||||
import { AnswerForm } from "controllers/FormControllers"
|
||||
import { useState } from "react"
|
||||
import MdPreview from "./MdPreview"
|
||||
import { Button, Card, CardContent, Tooltip } from '@mui/material';
|
||||
import { AnswerForm } from 'controllers/FormControllers';
|
||||
import { useState } from 'react';
|
||||
import MdPreview from './MdPreview';
|
||||
export default function CreateAnswer({ canAnswer, question_id }) {
|
||||
const [show, setShow] = useState(false)
|
||||
|
||||
const [show, setShow] = useState(false);
|
||||
|
||||
function toggleShow() {
|
||||
setShow(!show)
|
||||
setShow(!show);
|
||||
}
|
||||
return (
|
||||
<div>
|
||||
<Tooltip title={canAnswer && 'You need to be authenticated to answer or level 5 if the question is protected'}>
|
||||
{canAnswer ? (
|
||||
<span>
|
||||
<Button
|
||||
disabled={!canAnswer}
|
||||
variant='contained'
|
||||
onClick={toggleShow}
|
||||
>
|
||||
Add Answer
|
||||
</Button>
|
||||
</span>
|
||||
) : (
|
||||
<Tooltip title='You need to be authenticated to answer or level 5 if the question is protected.'>
|
||||
<span>
|
||||
<Button disabled={!canAnswer} variant="contained" onClick={toggleShow}>Add Answer</Button>
|
||||
<Button
|
||||
disabled={!canAnswer}
|
||||
variant='contained'
|
||||
onClick={toggleShow}
|
||||
>
|
||||
Add Answer
|
||||
</Button>
|
||||
</span>
|
||||
</Tooltip>
|
||||
)}
|
||||
{show && (
|
||||
<Card>
|
||||
<CardContent>
|
||||
|
|
@ -24,9 +41,6 @@ export default function CreateAnswer({ canAnswer, question_id }) {
|
|||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
|
||||
</div>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,11 @@ import {
|
|||
CommentControl,
|
||||
VoteControl,
|
||||
} from 'controllers/QAControllers';
|
||||
import { getAnswerVote, updateAnswerVote } from 'services/questionsServices';
|
||||
import {
|
||||
getAnswerVote,
|
||||
updateAnswerVote,
|
||||
postAnswerComment,
|
||||
} from 'services/questionsServices';
|
||||
|
||||
export default function Answer({
|
||||
accepted,
|
||||
|
|
@ -25,7 +29,10 @@ export default function Answer({
|
|||
const getVote = () => getAnswerVote(question_id, answer_id);
|
||||
const updateVote = (data) => updateAnswerVote(question_id, answer_id, data);
|
||||
var canComment =
|
||||
userData.level >= 3 || creator === userData.username ? true : false;
|
||||
userData?.level >= 3 || creator === userData?.username ? true : false;
|
||||
|
||||
const postComment = (data) =>
|
||||
postAnswerComment(question_id, answer_id, data);
|
||||
|
||||
if (userData.username)
|
||||
return (
|
||||
|
|
@ -57,7 +64,7 @@ export default function Answer({
|
|||
{...{ createdAt, creator, text: 'answered' }}
|
||||
/>
|
||||
<ReactMarkdown>{text}</ReactMarkdown>
|
||||
<CommentControl {...{ canComment }} />
|
||||
<CommentControl {...{ canComment, postComment }} />
|
||||
</ListItemText>
|
||||
</ListItem>
|
||||
<ListItem sx={{ pl: 8 }}>
|
||||
|
|
|
|||
|
|
@ -143,7 +143,7 @@ export default function Question({
|
|||
Ask question
|
||||
</Button>
|
||||
|
||||
<Tooltip title={!close && 'You must be level 7'}>
|
||||
{close ? (
|
||||
<span>
|
||||
<Button
|
||||
disabled={!close}
|
||||
|
|
@ -156,14 +156,24 @@ export default function Question({
|
|||
Close/Open
|
||||
</Button>
|
||||
</span>
|
||||
</Tooltip>
|
||||
) : (
|
||||
<Tooltip title={!close && 'You must be level 7'}>
|
||||
<span>
|
||||
<Button
|
||||
disabled={!close}
|
||||
style={{ marginLeft: '10px' }}
|
||||
display='inline'
|
||||
m={1}
|
||||
variant='contained'
|
||||
onClick={changeProtect}
|
||||
>
|
||||
Close/Open
|
||||
</Button>
|
||||
</span>
|
||||
</Tooltip>
|
||||
)}
|
||||
|
||||
<Tooltip
|
||||
title={
|
||||
!protect &&
|
||||
'You must be level 6 and this question must be open'
|
||||
}
|
||||
>
|
||||
{protect ? (
|
||||
<span>
|
||||
<Button
|
||||
disabled={!protect}
|
||||
|
|
@ -176,7 +186,27 @@ export default function Question({
|
|||
Protect
|
||||
</Button>
|
||||
</span>
|
||||
</Tooltip>
|
||||
) : (
|
||||
<Tooltip
|
||||
title={
|
||||
!protect &&
|
||||
'You must be level 6 and this question must be open'
|
||||
}
|
||||
>
|
||||
<span>
|
||||
<Button
|
||||
disabled={!protect}
|
||||
style={{ marginLeft: '10px' }}
|
||||
display='inline'
|
||||
m={1}
|
||||
variant='contained'
|
||||
onClick={changeClose}
|
||||
>
|
||||
Protect
|
||||
</Button>
|
||||
</span>
|
||||
</Tooltip>
|
||||
)}
|
||||
</Box>
|
||||
<Divider />
|
||||
|
||||
|
|
|
|||
|
|
@ -19,8 +19,6 @@ export function AnswersList() {
|
|||
|
||||
const sortByPoints = answers => answers.sort((a, b) => b.upvotes - b.downvotes - a.upvotes + a.downvotes);
|
||||
|
||||
console.log(question_id)
|
||||
|
||||
const getData = ({ answer_id }) =>
|
||||
getAnswers(question_id, { after: answer_id })
|
||||
.then(({ answers }) => sortByPoints(answers).map(answer => ({ ...answer, question_id })))
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ const updateAnswerVote = async (question_id, answer_id, data) =>
|
|||
const getAnswerComments = async (question_id, answer_id) =>
|
||||
callQuestionsAPI('get', `/${question_id}/answers/${answer_id}/comments`);
|
||||
|
||||
const postAnswerComments = async (question_id, answer_id, data) =>
|
||||
const postAnswerComment = async (question_id, answer_id, data) =>
|
||||
callQuestionsAPI(
|
||||
'post',
|
||||
`/${question_id}/answers/${answer_id}/comments`,
|
||||
|
|
@ -109,7 +109,7 @@ export {
|
|||
getQuestion,
|
||||
getQuestionComments,
|
||||
postAnswer,
|
||||
postAnswerComments,
|
||||
postAnswerComment,
|
||||
postQuestion,
|
||||
postQuestionComment,
|
||||
searchQuestions,
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ const createRequest = require('server/utils/api');
|
|||
const getUserLevel = require('server/utils/getUserLevel');
|
||||
|
||||
async function CreateAnswerComment(req, res) {
|
||||
const { username } = req.user;
|
||||
const user = req.user;
|
||||
const { question_id, answer_id } = req.params;
|
||||
const { text } = req.body;
|
||||
|
||||
|
|
@ -20,20 +20,22 @@ async function CreateAnswerComment(req, res) {
|
|||
return res.status(403).send(config.errorForbidden);
|
||||
|
||||
// Verify user has permissions to comment
|
||||
if (getUserLevel(user.points) < 3 && cachedAnswer?.creator !== username) {
|
||||
if (
|
||||
getUserLevel(user.points) < 3 &&
|
||||
cachedAnswer?.creator !== user.username
|
||||
) {
|
||||
return res.status(403).send(config.errorForbidden);
|
||||
}
|
||||
|
||||
// Verify text is included in request body
|
||||
if (!text)
|
||||
return res.status(400).send({ success: false });
|
||||
if (!text) return res.status(400).send({ success: false });
|
||||
|
||||
// Post question with BDPA server
|
||||
const { success, comment } = await createRequest(
|
||||
'post',
|
||||
`/questions/${question_id}/answers/${answer_id}/comments`,
|
||||
{
|
||||
creator: username,
|
||||
creator: user.username,
|
||||
text,
|
||||
}
|
||||
);
|
||||
|
|
|
|||
Reference in a new issue