Skip to content

Commit

Permalink
fix(socket): 좋아요 소켓 메시지 받으면 좋아요순대로 갱신한다 (#465)
Browse files Browse the repository at this point in the history
  • Loading branch information
leegwae authored and github-actions[bot] committed Sep 17, 2023
1 parent 190d610 commit 694a289
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/pages/Panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function Panel(): JSX.Element {
): void => {
queryClient.setQueryData<InfiniteData<QuestionPage>>(
queryKey.question.list(panel.sid),
updateLikeNum(questionId, likeNum),
updateLikeNumAndSort(questionId, likeNum),
);

queryClient.setQueryData<InfiniteData<QuestionPage>>(
Expand Down Expand Up @@ -301,6 +301,25 @@ const updateLikeNum =
});
});

const updateLikeNumAndSort =
(questionId: Question['id'], likeNum: Question['likeNum']) =>
(prevQuestions?: InfiniteData<QuestionPage>) =>
produce(prevQuestions, (draft) => {
if (!draft) return;

const allQuestions = draft.pages.flatMap((page) => page.questions);
const questionIdx = allQuestions.findIndex(
(question) => question.id === questionId,
);
if (questionIdx !== -1) {
allQuestions[questionIdx].likeNum = likeNum;
allQuestions.sort((a, b) => b.likeNum - a.likeNum);
draft.pages.forEach((page) => {
page.questions = allQuestions.splice(0, 30);
});
}
});

const increaseAnswerNum =
(questionId: Question['id']) =>
(prevQuestions?: InfiniteData<QuestionPage>) =>
Expand Down

0 comments on commit 694a289

Please sign in to comment.