Added functionality for refetch onSubmit
This commit is contained in:
parent
2ec4130505
commit
82be61472a
5 changed files with 35 additions and 23 deletions
|
|
@ -12,7 +12,9 @@ export default function PaginatedList({
|
|||
}) {
|
||||
return (
|
||||
<List sx={{ pl: 2, pr: 2, width: '100%', height: '100%' }}>
|
||||
{data.length ? data.map((item) => Component(item)) : noData && <NoData />}
|
||||
{data.length
|
||||
? data.map((item, index) => <Component {...item} key={index} />)
|
||||
: noData && <NoData />}
|
||||
{count > 1 && (
|
||||
<Box fullWidth display='flex' justifyContent='center' sx={{ margin: '1vh 0' }}>
|
||||
<Pagination
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ import { Button, ButtonGroup, ListItem, ListItemText, Tooltip } from '@mui/mater
|
|||
import CheckIcon from '@mui/icons-material/Check';
|
||||
import ReactMarkdown from 'react-markdown';
|
||||
|
||||
import { useQuestion } from 'contexts';
|
||||
import { CreationInfoTag } from 'controllers';
|
||||
import { AnswerCommentsList, CommentControl, VoteControl } from 'controllers/QAControllers';
|
||||
import {
|
||||
|
|
@ -22,11 +21,9 @@ export default function Answer({
|
|||
question_id,
|
||||
text,
|
||||
upvotes,
|
||||
canComment,
|
||||
canAccept,
|
||||
}) {
|
||||
const {
|
||||
permissions: { canVote, canComment, canAccept },
|
||||
} = useQuestion();
|
||||
|
||||
const getVote = () => getAnswerVote(question_id, answer_id);
|
||||
const updateVote = (data) => updateAnswerVote(question_id, answer_id, data);
|
||||
const postComment = async (data) => {
|
||||
|
|
|
|||
|
|
@ -12,12 +12,9 @@ export default function Comment({
|
|||
question_id,
|
||||
text,
|
||||
upvotes,
|
||||
|
||||
}) {
|
||||
const getVote = () => getCommentVote(question_id, comment_id);
|
||||
const updateVote = (data) => updateCommentVote(question_id, comment_id, data);
|
||||
const {permissions} = useQuestion();
|
||||
let canVote = permissions.canVote;
|
||||
return (
|
||||
<span key={comment_id}>
|
||||
<ListItem disablePadding>
|
||||
|
|
@ -25,9 +22,9 @@ export default function Comment({
|
|||
<CreationInfoTag {...{ createdAt, creator, text: 'commented' }} />
|
||||
{text}
|
||||
</ListItemText>
|
||||
<VoteControl {...{ downvotes, getVote, updateVote, upvotes, creator}} />
|
||||
<VoteControl {...{ downvotes, getVote, updateVote, upvotes, creator }} />
|
||||
</ListItem>
|
||||
<Divider />
|
||||
</span>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import { commentSchema } from 'services/schemas';
|
|||
|
||||
export default function CommentControlController({ postComment, canComment }) {
|
||||
const [show, setShow] = useState(false);
|
||||
const navigate = useNavigate();
|
||||
|
||||
const toggleShow = () => {
|
||||
setShow(!show);
|
||||
|
|
@ -16,6 +17,7 @@ export default function CommentControlController({ postComment, canComment }) {
|
|||
await postComment(data);
|
||||
|
||||
toggleShow();
|
||||
navigate('');
|
||||
};
|
||||
|
||||
return CommentControl({
|
||||
|
|
|
|||
|
|
@ -4,42 +4,56 @@ import { Answer, AnswerComment, Comment } from 'components/QAComponents';
|
|||
import { getAnswerComments, getAnswers, getQuestionComments } from 'services/questionsServices';
|
||||
|
||||
export function AnswerCommentsList({ answer_id, comments: count }) {
|
||||
const { questionData: { question_id } } = useQuestion();
|
||||
const {
|
||||
questionData: { question_id },
|
||||
} = useQuestion();
|
||||
|
||||
const getData = ({ comment_id }) =>
|
||||
getAnswerComments(question_id, answer_id, { after: comment_id })
|
||||
.then(({ comments }) => comments.map(comment => ({ ...comment, answer_id, question_id })))
|
||||
.then(({ comments }) =>
|
||||
comments.map((comment) => ({ ...comment, answer_id, question_id }))
|
||||
)
|
||||
.catch(() => []);
|
||||
|
||||
return <PaginatedList {...{ count, Component: AnswerComment, getData, noData: false }} />;
|
||||
}
|
||||
|
||||
export function AnswersList() {
|
||||
const { questionData: { answers: count, loading, question_id } } = useQuestion();
|
||||
const {
|
||||
permissions: { canComment, canAccept },
|
||||
questionData: { answers: count, loading, question_id },
|
||||
} = useQuestion();
|
||||
|
||||
const sortByPoints = answers => answers.sort((a, b) => b.upvotes - b.downvotes - a.upvotes + a.downvotes);
|
||||
const sortByPoints = (answers) =>
|
||||
answers.sort((a, b) => b.upvotes - b.downvotes - a.upvotes + a.downvotes);
|
||||
|
||||
const getData = ({ answer_id }) =>
|
||||
getAnswers(question_id, { after: answer_id })
|
||||
.then(({ answers }) => sortByPoints(answers).map(answer => ({ ...answer, question_id })))
|
||||
.then(({ answers }) =>
|
||||
sortByPoints(answers).map((answer) => ({ ...answer, question_id }))
|
||||
)
|
||||
.catch(() => []);
|
||||
|
||||
const NewAnswer = (props) => <Answer {...{ ...props, canComment, canAccept }} />;
|
||||
|
||||
return (
|
||||
!loading && <PaginatedList {...{ count, Component: Answer, getData, noData: false }} />
|
||||
!loading && <PaginatedList {...{ count, Component: NewAnswer, getData, noData: false }} />
|
||||
);
|
||||
}
|
||||
|
||||
export function CommentsList() {
|
||||
const { questionData: { comments: count, loading, question_id } } = useQuestion();
|
||||
const {
|
||||
questionData: { comments: count, loading, question_id },
|
||||
} = useQuestion();
|
||||
|
||||
const sortByCreatedAt = comments => comments.sort((a, b) => a.createdAt - b.createdAt);
|
||||
const sortByCreatedAt = (comments) => comments.sort((a, b) => a.createdAt - b.createdAt);
|
||||
|
||||
const getData = ({ comment_id }) =>
|
||||
getQuestionComments(question_id, { after: comment_id })
|
||||
.then(({ comments }) => sortByCreatedAt(comments).map(comment => ({ ...comment, question_id })))
|
||||
.then(({ comments }) =>
|
||||
sortByCreatedAt(comments).map((comment) => ({ ...comment, question_id }))
|
||||
)
|
||||
.catch(() => []);
|
||||
|
||||
return (
|
||||
!loading && <PaginatedList {...{ count, Component: Comment, getData, noData: false }} />
|
||||
);
|
||||
return !loading && <PaginatedList {...{ count, Component: Comment, getData, noData: false }} />;
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue