[question][feat] update question answer comments optimistically

pull/521/head
Jeff Sieu 3 years ago
parent 3aaeaa8085
commit 89e18675db

@ -158,19 +158,18 @@ export default function QuestionPage() {
</div> </div>
</div> </div>
{/* TODO: Allow to load more pages */} {/* TODO: Allow to load more pages */}
{(answerCommentsData?.pages ?? []).flatMap( {(answerCommentsData?.pages ?? []).flatMap(({ data: comments }) =>
({ processedQuestionAnswerCommentsData: comments }) => comments.map((comment) => (
comments.map((comment) => ( <AnswerCommentListItem
<AnswerCommentListItem key={comment.id}
key={comment.id} answerCommentId={comment.id}
answerCommentId={comment.id} authorImageUrl={comment.userImage}
authorImageUrl={comment.userImage} authorName={comment.user}
authorName={comment.user} content={comment.content}
content={comment.content} createdAt={comment.createdAt}
createdAt={comment.createdAt} upvoteCount={comment.numVotes}
upvoteCount={comment.numVotes} />
/> )),
)),
)} )}
<PaginationLoadMoreButton query={answerCommentInfiniteQuery} /> <PaginationLoadMoreButton query={answerCommentInfiniteQuery} />
</div> </div>

@ -56,35 +56,36 @@ export const questionsAnswerCommentRouter = createRouter().query(
answerId, answerId,
}, },
}); });
const processedQuestionAnswerCommentsData = questionAnswerCommentsData.map((data) => { const processedQuestionAnswerCommentsData =
const votes: number = data.votes.reduce( questionAnswerCommentsData.map((data) => {
(previousValue: number, currentValue) => { const votes: number = data.votes.reduce(
let result: number = previousValue; (previousValue: number, currentValue) => {
let result: number = previousValue;
switch (currentValue.vote) { switch (currentValue.vote) {
case Vote.UPVOTE: case Vote.UPVOTE:
result += 1; result += 1;
break; break;
case Vote.DOWNVOTE: case Vote.DOWNVOTE:
result -= 1; result -= 1;
break; break;
} }
return result; return result;
}, },
0, 0,
); );
const answerComment: AnswerComment = { const answerComment: AnswerComment = {
content: data.content, content: data.content,
createdAt: data.createdAt, createdAt: data.createdAt,
id: data.id, id: data.id,
numVotes: votes, numVotes: votes,
updatedAt: data.updatedAt, updatedAt: data.updatedAt,
user: data.user?.name ?? '', user: data.user?.name ?? '',
userImage: data.user?.image ?? '', userImage: data.user?.image ?? '',
}; };
return answerComment; return answerComment;
}); });
let nextCursor: typeof cursor | undefined = undefined; let nextCursor: typeof cursor | undefined = undefined;
@ -98,9 +99,9 @@ export const questionsAnswerCommentRouter = createRouter().query(
} }
return { return {
data: processedQuestionAnswerCommentsData,
nextCursor, nextCursor,
processedQuestionAnswerCommentsData, };
}
}, },
}, },
); );

@ -5,7 +5,12 @@ import { Vote } from '@prisma/client';
import { trpc } from '../trpc'; import { trpc } from '../trpc';
import type { Answer, Question, QuestionComment } from '~/types/questions'; import type {
Answer,
AnswerComment,
Question,
QuestionComment,
} from '~/types/questions';
type UseVoteOptions = { type UseVoteOptions = {
setDownVote: () => void; setDownVote: () => void;
@ -253,9 +258,48 @@ export const useQuestionCommentVote = (id: string) => {
}; };
export const useAnswerCommentVote = (id: string) => { export const useAnswerCommentVote = (id: string) => {
const utils = trpc.useContext();
return useVote(id, { return useVote(id, {
idKey: 'answerCommentId', idKey: 'answerCommentId',
invalidateKeys: ['questions.answers.comments.getAnswerComments'], invalidateKeys: [],
onMutate: async (voteValueChange) => {
// Update answer comment list
const answerCommentQueries = utils.queryClient.getQueriesData([
'questions.answers.comments.getAnswerComments',
]);
if (answerCommentQueries !== undefined) {
for (const [key, query] of answerCommentQueries) {
if (query === undefined) {
continue;
}
const { pages, ...restQuery } = query as InfiniteData<{
data: Array<AnswerComment>;
}>;
const newQuery = {
pages: pages.map(({ data, ...restPage }) => ({
data: data.map((answerComment) => {
if (answerComment.id === id) {
const { numVotes, ...restAnswerComment } = answerComment;
return {
numVotes: numVotes + voteValueChange,
...restAnswerComment,
};
}
return answerComment;
}),
...restPage,
})),
...restQuery,
};
utils.queryClient.setQueryData(key, newQuery);
}
}
},
query: 'questions.answers.comments.user.getVote', query: 'questions.answers.comments.user.getVote',
setDownVoteKey: 'questions.answers.comments.user.setDownVote', setDownVoteKey: 'questions.answers.comments.user.setDownVote',
setNoVoteKey: 'questions.answers.comments.user.setNoVote', setNoVoteKey: 'questions.answers.comments.user.setNoVote',

Loading…
Cancel
Save