From ccc8eed1d67a6514c8ed595782717c6d6cb7e35e Mon Sep 17 00:00:00 2001 From: Jeff Sieu Date: Mon, 10 Oct 2022 03:54:58 +0800 Subject: [PATCH] [questions][feat] view, post answer comments --- .../answer/[answerId]/[answerSlug]/index.tsx | 45 ++++-- .../[questionId]/[questionSlug]/index.tsx | 18 ++- .../router/questions-answer-comment-router.ts | 142 +++++++++--------- apps/portal/src/types/questions.d.ts | 3 + 4 files changed, 119 insertions(+), 89 deletions(-) diff --git a/apps/portal/src/pages/questions/[questionId]/[questionSlug]/answer/[answerId]/[answerSlug]/index.tsx b/apps/portal/src/pages/questions/[questionId]/[questionSlug]/answer/[answerId]/[answerSlug]/index.tsx index 61a5f62b..b2251a37 100644 --- a/apps/portal/src/pages/questions/[questionId]/[questionSlug]/answer/[answerId]/[answerSlug]/index.tsx +++ b/apps/portal/src/pages/questions/[questionId]/[questionSlug]/answer/[answerId]/[answerSlug]/index.tsx @@ -9,7 +9,6 @@ import CommentListItem from '~/components/questions/CommentListItem'; import { SAMPLE_ANSWER, SAMPLE_ANSWER_COMMENT, - SAMPLE_QUESTION, } from '~/utils/questions/constants'; import { useFormRegister } from '~/utils/questions/useFormRegister'; import { trpc } from '~/utils/trpc'; @@ -23,28 +22,48 @@ export default function QuestionPage() { const { register: comRegister, + reset: resetComment, handleSubmit: handleCommentSubmit, formState: { isDirty: isCommentDirty, isValid: isCommentValid }, } = useForm({ mode: 'onChange' }); const commentRegister = useFormRegister(comRegister); - const question = SAMPLE_QUESTION; - const comment = SAMPLE_ANSWER_COMMENT; - const { answerId } = router.query; + const utils = trpc.useContext(); + const { data: answer } = trpc.useQuery([ 'questions.answers.getAnswerById', { answerId: answerId as string }, ]); + const { data: comments } = trpc.useQuery([ + 'questions.answers.comments.getAnswerComments', + { answerId: answerId as string }, + ]); + + const { mutate: addComment } = trpc.useMutation( + 'questions.answers.comments.create', + { + onSuccess: () => { + utils.invalidateQuery([ + 'questions.answers.comments.getAnswerComments', + { answerId: answerId as string }, + ]); + }, + }, + ); + const handleBackNavigation = () => { router.back(); }; const handleSubmitComment = (data: AnswerCommentData) => { - // eslint-disable-next-line no-console - console.log(data); + resetComment(); + addComment({ + answerId: answerId as string, + content: data.commentContent, + }); }; if (!answer) { @@ -109,7 +128,8 @@ export default function QuestionPage() { onChange={(value) => { // eslint-disable-next-line no-console console.log(value); - }}> + }} + />