[questions][feat] view, post question comments

pull/347/head
Jeff Sieu 3 years ago
parent 31047450f8
commit a1925144cb

@ -34,6 +34,7 @@ export default function QuestionPage() {
const { const {
register: comRegister, register: comRegister,
handleSubmit: handleCommentSubmit, handleSubmit: handleCommentSubmit,
reset: resetComment,
formState: { isDirty: isCommentDirty, isValid: isCommentValid }, formState: { isDirty: isCommentDirty, isValid: isCommentValid },
} = useForm<QuestionCommentData>({ mode: 'onChange' }); } = useForm<QuestionCommentData>({ mode: 'onChange' });
const commentRegister = useFormRegister(comRegister); const commentRegister = useFormRegister(comRegister);
@ -45,7 +46,24 @@ export default function QuestionPage() {
{ id: questionId as string }, { id: questionId as string },
]); ]);
const comment = SAMPLE_QUESTION_COMMENT; const utils = trpc.useContext();
const { data: comments } = trpc.useQuery([
'questions.questions.comments.getQuestionComments',
{ questionId: questionId as string },
]);
const { mutate: addComment } = trpc.useMutation(
'questions.questions.comments.create',
{
onSuccess: () => {
utils.invalidateQueries(
'questions.questions.comments.getQuestionComments',
);
},
},
);
const handleBackNavigation = () => { const handleBackNavigation = () => {
router.back(); router.back();
}; };
@ -56,8 +74,11 @@ export default function QuestionPage() {
}; };
const handleSubmitComment = (data: QuestionCommentData) => { const handleSubmitComment = (data: QuestionCommentData) => {
// eslint-disable-next-line no-console addComment({
console.log(data); content: data.commentContent,
questionId: questionId as string,
});
resetComment();
}; };
if (!question) { if (!question) {
@ -134,11 +155,14 @@ export default function QuestionPage() {
</div> </div>
</form> </form>
{Array.from({ length: question.numComments }).map((_, index) => ( {(comments ?? []).map((comment) => (
<CommentListItem <CommentListItem
// eslint-disable-next-line react/no-array-index-key key={comment.id}
key={index} authorImageUrl={SAMPLE_QUESTION_COMMENT.authorImageUrl}
{...comment} authorName={comment.user}
content={comment.content}
createdAt={comment.createdAt}
upvoteCount={0}
/> />
))} ))}
</Collapsible> </Collapsible>

@ -12,56 +12,57 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
questionId: z.string(), questionId: z.string(),
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const questionCommentsData = await ctx.prisma.questionsQuestionComment.findMany({ const questionCommentsData =
include: { await ctx.prisma.questionsQuestionComment.findMany({
user: { include: {
select: { user: {
name: true, select: {
name: true,
},
},
votes: true,
}, },
}, orderBy: {
votes: true, createdAt: 'desc',
}, },
orderBy: { where: {
createdAt: 'desc', ...input,
}, },
where: { });
...input, return questionCommentsData.map((data) => {
}, const votes: number = data.votes.reduce(
}); (previousValue: number, currentValue) => {
return questionCommentsData.map((data) => { let result: number = previousValue;
const votes:number = data.votes.reduce(
(previousValue:number, currentValue) => { switch (currentValue.vote) {
let result:number = previousValue; case Vote.UPVOTE:
result += 1;
switch(currentValue.vote) { break;
case Vote.UPVOTE: case Vote.DOWNVOTE:
result += 1 result -= 1;
break; break;
case Vote.DOWNVOTE: }
result -= 1 return result;
break; },
} 0,
return result;
},
0
); );
let userName = ""; let userName = '';
if (data.user) { if (data.user) {
userName = data.user.name!; userName = data.user.name!;
} }
const questionComment: QuestionComment = { const questionComment: QuestionComment = {
content: data.content, content: data.content,
createdAt: data.createdAt, createdAt: data.createdAt,
id: data.id, id: data.id,
numVotes: votes, numVotes: votes,
user: userName, user: userName,
}; };
return questionComment; return questionComment;
}); });
} },
}) })
.mutation('create', { .mutation('create', {
input: z.object({ input: z.object({
@ -87,11 +88,12 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id; const userId = ctx.session?.user?.id;
const questionCommentToUpdate = await ctx.prisma.questionsQuestionComment.findUnique({ const questionCommentToUpdate =
where: { await ctx.prisma.questionsQuestionComment.findUnique({
id: input.id, where: {
}, id: input.id,
}); },
});
if (questionCommentToUpdate?.id !== userId) { if (questionCommentToUpdate?.id !== userId) {
throw new TRPCError({ throw new TRPCError({
@ -117,11 +119,12 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id; const userId = ctx.session?.user?.id;
const questionCommentToDelete = await ctx.prisma.questionsQuestionComment.findUnique({ const questionCommentToDelete =
where: { await ctx.prisma.questionsQuestionComment.findUnique({
id: input.id, where: {
}, id: input.id,
}); },
});
if (questionCommentToDelete?.id !== userId) { if (questionCommentToDelete?.id !== userId) {
throw new TRPCError({ throw new TRPCError({
@ -143,11 +146,11 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id; const userId = ctx.session?.user?.id;
const {questionCommentId} = input const { questionCommentId } = input;
return await ctx.prisma.questionsQuestionCommentVote.findUnique({ return await ctx.prisma.questionsQuestionCommentVote.findUnique({
where: { where: {
questionCommentId_userId : {questionCommentId,userId }, questionCommentId_userId: { questionCommentId, userId },
}, },
}); });
}, },
@ -175,13 +178,14 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id; const userId = ctx.session?.user?.id;
const {id, vote} = input const { id, vote } = input;
const voteToUpdate = await ctx.prisma.questionsQuestionCommentVote.findUnique({ const voteToUpdate =
where: { await ctx.prisma.questionsQuestionCommentVote.findUnique({
id: input.id, where: {
}, id: input.id,
}); },
});
if (voteToUpdate?.id !== userId) { if (voteToUpdate?.id !== userId) {
throw new TRPCError({ throw new TRPCError({
@ -207,10 +211,12 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id; const userId = ctx.session?.user?.id;
const voteToDelete = await ctx.prisma.questionsQuestionCommentVote.findUnique({ const voteToDelete =
where: { await ctx.prisma.questionsQuestionCommentVote.findUnique({
id: input.id, where: {
},}); id: input.id,
},
});
if (voteToDelete?.id !== userId) { if (voteToDelete?.id !== userId) {
throw new TRPCError({ throw new TRPCError({
@ -225,4 +231,4 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
}, },
}); });
}, },
}); });

Loading…
Cancel
Save