|
|
|
@ -1,19 +1,23 @@
|
|
|
|
|
import Head from 'next/head';
|
|
|
|
|
import { useRouter } from 'next/router';
|
|
|
|
|
import { useState } from 'react';
|
|
|
|
|
import { useForm } from 'react-hook-form';
|
|
|
|
|
import { ArrowSmallLeftIcon } from '@heroicons/react/24/outline';
|
|
|
|
|
import { Button, Collapsible, Select, TextArea } from '@tih/ui';
|
|
|
|
|
import { Button, Collapsible, HorizontalDivider, TextArea } from '@tih/ui';
|
|
|
|
|
|
|
|
|
|
import AnswerCommentListItem from '~/components/questions/AnswerCommentListItem';
|
|
|
|
|
import FullQuestionCard from '~/components/questions/card/question/FullQuestionCard';
|
|
|
|
|
import QuestionAnswerCard from '~/components/questions/card/QuestionAnswerCard';
|
|
|
|
|
import FullScreenSpinner from '~/components/questions/FullScreenSpinner';
|
|
|
|
|
import SortOptionsSelect from '~/components/questions/SortOptionsSelect';
|
|
|
|
|
|
|
|
|
|
import { APP_TITLE } from '~/utils/questions/constants';
|
|
|
|
|
import createSlug from '~/utils/questions/createSlug';
|
|
|
|
|
import { useFormRegister } from '~/utils/questions/useFormRegister';
|
|
|
|
|
import { trpc } from '~/utils/trpc';
|
|
|
|
|
|
|
|
|
|
import { SortOrder, SortType } from '~/types/questions.d';
|
|
|
|
|
|
|
|
|
|
export type AnswerQuestionData = {
|
|
|
|
|
answerContent: string;
|
|
|
|
|
};
|
|
|
|
@ -24,6 +28,19 @@ export type QuestionCommentData = {
|
|
|
|
|
|
|
|
|
|
export default function QuestionPage() {
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
|
|
const [answerSortOrder, setAnswerSortOrder] = useState<SortOrder>(
|
|
|
|
|
SortOrder.DESC,
|
|
|
|
|
);
|
|
|
|
|
const [answerSortType, setAnswerSortType] = useState<SortType>(SortType.NEW);
|
|
|
|
|
|
|
|
|
|
const [commentSortOrder, setCommentSortOrder] = useState<SortOrder>(
|
|
|
|
|
SortOrder.DESC,
|
|
|
|
|
);
|
|
|
|
|
const [commentSortType, setCommentSortType] = useState<SortType>(
|
|
|
|
|
SortType.NEW,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
register: ansRegister,
|
|
|
|
|
handleSubmit,
|
|
|
|
@ -54,10 +71,20 @@ export default function QuestionPage() {
|
|
|
|
|
|
|
|
|
|
const utils = trpc.useContext();
|
|
|
|
|
|
|
|
|
|
const { data: comments } = trpc.useQuery([
|
|
|
|
|
'questions.questions.comments.getQuestionComments',
|
|
|
|
|
{ questionId: questionId as string },
|
|
|
|
|
]);
|
|
|
|
|
const { data: commentData } = trpc.useInfiniteQuery(
|
|
|
|
|
[
|
|
|
|
|
'questions.questions.comments.getQuestionComments',
|
|
|
|
|
{
|
|
|
|
|
questionId: questionId as string,
|
|
|
|
|
sortOrder: commentSortOrder,
|
|
|
|
|
sortType: commentSortType,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
{
|
|
|
|
|
getNextPageParam: (lastPage) => lastPage.nextCursor,
|
|
|
|
|
keepPreviousData: true,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const { mutate: addComment } = trpc.useMutation(
|
|
|
|
|
'questions.questions.comments.user.create',
|
|
|
|
@ -70,10 +97,20 @@ export default function QuestionPage() {
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const { data: answers } = trpc.useQuery([
|
|
|
|
|
'questions.answers.getAnswers',
|
|
|
|
|
{ questionId: questionId as string },
|
|
|
|
|
]);
|
|
|
|
|
const { data: answerData } = trpc.useInfiniteQuery(
|
|
|
|
|
[
|
|
|
|
|
'questions.answers.getAnswers',
|
|
|
|
|
{
|
|
|
|
|
questionId: questionId as string,
|
|
|
|
|
sortOrder: answerSortOrder,
|
|
|
|
|
sortType: answerSortType,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
{
|
|
|
|
|
getNextPageParam: (lastPage) => lastPage.nextCursor,
|
|
|
|
|
keepPreviousData: true,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const { mutate: addAnswer } = trpc.useMutation(
|
|
|
|
|
'questions.answers.user.create',
|
|
|
|
@ -134,7 +171,7 @@ export default function QuestionPage() {
|
|
|
|
|
variant="secondary"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex w-full justify-center overflow-y-auto py-4 px-5">
|
|
|
|
|
<div className="flex w-full justify-center overflow-y-auto py-4 px-5">
|
|
|
|
|
<div className="flex max-w-7xl flex-1 flex-col gap-2">
|
|
|
|
|
<FullQuestionCard
|
|
|
|
|
{...question}
|
|
|
|
@ -159,69 +196,62 @@ export default function QuestionPage() {
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<div className="mx-2">
|
|
|
|
|
<Collapsible label={`${(comments ?? []).length} comment(s)`}>
|
|
|
|
|
<form
|
|
|
|
|
className="mb-2"
|
|
|
|
|
onSubmit={handleCommentSubmit(handleSubmitComment)}>
|
|
|
|
|
<TextArea
|
|
|
|
|
{...commentRegister('commentContent', {
|
|
|
|
|
minLength: 1,
|
|
|
|
|
required: true,
|
|
|
|
|
})}
|
|
|
|
|
label="Post a comment"
|
|
|
|
|
required={true}
|
|
|
|
|
resize="vertical"
|
|
|
|
|
rows={2}
|
|
|
|
|
/>
|
|
|
|
|
<div className="my-3 flex justify-between">
|
|
|
|
|
<div className="flex items-baseline gap-2">
|
|
|
|
|
<span aria-hidden={true} className="text-sm">
|
|
|
|
|
Sort by:
|
|
|
|
|
</span>
|
|
|
|
|
<Select
|
|
|
|
|
display="inline"
|
|
|
|
|
isLabelHidden={true}
|
|
|
|
|
label="Sort by"
|
|
|
|
|
options={[
|
|
|
|
|
{
|
|
|
|
|
label: 'Most recent',
|
|
|
|
|
value: 'most-recent',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: 'Most upvotes',
|
|
|
|
|
value: 'most-upvotes',
|
|
|
|
|
},
|
|
|
|
|
]}
|
|
|
|
|
value="most-recent"
|
|
|
|
|
onChange={(value) => {
|
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
|
console.log(value);
|
|
|
|
|
}}
|
|
|
|
|
<Collapsible label={`${question.numComments} comment(s)`}>
|
|
|
|
|
<div className="mt-4 px-4">
|
|
|
|
|
<form
|
|
|
|
|
className="mb-2"
|
|
|
|
|
onSubmit={handleCommentSubmit(handleSubmitComment)}>
|
|
|
|
|
<TextArea
|
|
|
|
|
{...commentRegister('commentContent', {
|
|
|
|
|
minLength: 1,
|
|
|
|
|
required: true,
|
|
|
|
|
})}
|
|
|
|
|
label="Post a comment"
|
|
|
|
|
required={true}
|
|
|
|
|
resize="vertical"
|
|
|
|
|
rows={2}
|
|
|
|
|
/>
|
|
|
|
|
<div className="my-3 flex justify-between">
|
|
|
|
|
<Button
|
|
|
|
|
disabled={!isCommentDirty || !isCommentValid}
|
|
|
|
|
label="Post"
|
|
|
|
|
type="submit"
|
|
|
|
|
variant="primary"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<Button
|
|
|
|
|
disabled={!isCommentDirty || !isCommentValid}
|
|
|
|
|
label="Post"
|
|
|
|
|
type="submit"
|
|
|
|
|
variant="primary"
|
|
|
|
|
/>
|
|
|
|
|
</form>
|
|
|
|
|
{/* TODO: Add button to load more */}
|
|
|
|
|
<div className="flex flex-col gap-2">
|
|
|
|
|
<div className="flex items-center justify-between gap-2">
|
|
|
|
|
<p className="text-lg">Comments</p>
|
|
|
|
|
<div className="flex items-end gap-2">
|
|
|
|
|
<SortOptionsSelect
|
|
|
|
|
sortOrderValue={commentSortOrder}
|
|
|
|
|
sortTypeValue={commentSortType}
|
|
|
|
|
onSortOrderChange={setCommentSortOrder}
|
|
|
|
|
onSortTypeChange={setCommentSortType}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
{(commentData?.pages ?? []).flatMap(
|
|
|
|
|
({ processedQuestionCommentsData: comments }) =>
|
|
|
|
|
comments.map((comment) => (
|
|
|
|
|
<AnswerCommentListItem
|
|
|
|
|
key={comment.id}
|
|
|
|
|
answerCommentId={comment.id}
|
|
|
|
|
authorImageUrl={comment.userImage}
|
|
|
|
|
authorName={comment.user}
|
|
|
|
|
content={comment.content}
|
|
|
|
|
createdAt={comment.createdAt}
|
|
|
|
|
upvoteCount={comment.numVotes}
|
|
|
|
|
/>
|
|
|
|
|
)),
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
|
|
|
|
|
{(comments ?? []).map((comment) => (
|
|
|
|
|
<AnswerCommentListItem
|
|
|
|
|
key={comment.id}
|
|
|
|
|
answerCommentId={comment.id}
|
|
|
|
|
authorImageUrl={comment.userImage}
|
|
|
|
|
authorName={comment.user}
|
|
|
|
|
content={comment.content}
|
|
|
|
|
createdAt={comment.createdAt}
|
|
|
|
|
upvoteCount={comment.numVotes}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
</Collapsible>
|
|
|
|
|
</div>
|
|
|
|
|
<HorizontalDivider />
|
|
|
|
|
<form onSubmit={handleSubmit(handleSubmitAnswer)}>
|
|
|
|
|
<TextArea
|
|
|
|
|
{...answerRegister('answerContent', {
|
|
|
|
@ -234,34 +264,6 @@ export default function QuestionPage() {
|
|
|
|
|
rows={5}
|
|
|
|
|
/>
|
|
|
|
|
<div className="mt-3 mb-1 flex justify-between">
|
|
|
|
|
<div className="flex items-baseline justify-start gap-2">
|
|
|
|
|
<p>{(answers ?? []).length} answers</p>
|
|
|
|
|
<div className="flex items-baseline gap-2">
|
|
|
|
|
<span aria-hidden={true} className="text-sm">
|
|
|
|
|
Sort by:
|
|
|
|
|
</span>
|
|
|
|
|
<Select
|
|
|
|
|
display="inline"
|
|
|
|
|
isLabelHidden={true}
|
|
|
|
|
label="Sort by"
|
|
|
|
|
options={[
|
|
|
|
|
{
|
|
|
|
|
label: 'Most recent',
|
|
|
|
|
value: 'most-recent',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: 'Most upvotes',
|
|
|
|
|
value: 'most-upvotes',
|
|
|
|
|
},
|
|
|
|
|
]}
|
|
|
|
|
value="most-recent"
|
|
|
|
|
onChange={(value) => {
|
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
|
console.log(value);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<Button
|
|
|
|
|
disabled={!isDirty || !isValid}
|
|
|
|
|
label="Contribute"
|
|
|
|
@ -270,21 +272,36 @@ export default function QuestionPage() {
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
{(answers ?? []).map((answer) => (
|
|
|
|
|
<QuestionAnswerCard
|
|
|
|
|
key={answer.id}
|
|
|
|
|
answerId={answer.id}
|
|
|
|
|
authorImageUrl={answer.userImage}
|
|
|
|
|
authorName={answer.user}
|
|
|
|
|
commentCount={answer.numComments}
|
|
|
|
|
content={answer.content}
|
|
|
|
|
createdAt={answer.createdAt}
|
|
|
|
|
href={`${router.asPath}/answer/${answer.id}/${createSlug(
|
|
|
|
|
answer.content,
|
|
|
|
|
)}`}
|
|
|
|
|
upvoteCount={answer.numVotes}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
<div className="flex items-center justify-between gap-2">
|
|
|
|
|
<p className="text-xl">{question.numAnswers} answers</p>
|
|
|
|
|
<div className="flex items-end gap-2">
|
|
|
|
|
<SortOptionsSelect
|
|
|
|
|
sortOrderValue={answerSortOrder}
|
|
|
|
|
sortTypeValue={answerSortType}
|
|
|
|
|
onSortOrderChange={setAnswerSortOrder}
|
|
|
|
|
onSortTypeChange={setAnswerSortType}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
{/* TODO: Add button to load more */}
|
|
|
|
|
{(answerData?.pages ?? []).flatMap(
|
|
|
|
|
({ processedAnswersData: answers }) =>
|
|
|
|
|
answers.map((answer) => (
|
|
|
|
|
<QuestionAnswerCard
|
|
|
|
|
key={answer.id}
|
|
|
|
|
answerId={answer.id}
|
|
|
|
|
authorImageUrl={answer.userImage}
|
|
|
|
|
authorName={answer.user}
|
|
|
|
|
commentCount={answer.numComments}
|
|
|
|
|
content={answer.content}
|
|
|
|
|
createdAt={answer.createdAt}
|
|
|
|
|
href={`${router.asPath}/answer/${answer.id}/${createSlug(
|
|
|
|
|
answer.content,
|
|
|
|
|
)}`}
|
|
|
|
|
upvoteCount={answer.numVotes}
|
|
|
|
|
/>
|
|
|
|
|
)),
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|