-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix : useScrapToggle에 낙관적 업데이트를 위해 queryKey를 넘기는 방식으로 수정 #309
Conversation
…into 306-fix-toggle-scrap-bug
// TODO(@smosco): 외부 스크랩 할 때 setQueryData 또는 invalidate 하기 위해 정확한 queryKey가 필요함 | ||
// null 값으로 오면 queryKey가 이상하므로 임시 방편으로 filter를 적용함 | ||
// 다만 나중에 sort, direction, categoryId가 들어오는 경우 순서가 유지 되지 않으므로 문제 발생 | ||
// 따라서 어디서든지 간에 default 값을 넘길 필요가 있음 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 주석은 이제 안 필요한가요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
네넴 이제 queryKey 자체로 변경해서 서치 컴포넌트에서도 할 수 있게 되었습니다~ 아직 추가는 안했지만요!
queryClient.setQueryData(queryKey, (old: any) => { | ||
if (old?.data?.contents && Array.isArray(old.data.contents)) { | ||
return { | ||
...old, | ||
data: { | ||
...old.data, | ||
contents: old.data.contents.map((content: any) => | ||
content.contentId === contentId | ||
? { ...content, isScrapped: !isScrapped } | ||
: content, | ||
), | ||
}, | ||
}; | ||
} | ||
return old; | ||
}); | ||
break; | ||
|
||
case 'paginatedListeningPreview': |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
querykey[0]가 listeningPreview, readingPreview라는 타입을 구별하는 것이군요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
네 맞아요!
// 경로별 queryKey 구성 | ||
if (pathname === '/') { | ||
// 메인 페이지 | ||
return contentType === 'READING' | ||
? ['readingPreview'] | ||
: ['listeningPreview']; | ||
} | ||
|
||
if (pathname.startsWith('/learn')) { | ||
// 학습 페이지 | ||
const type = pathname.includes('/reading') | ||
? 'paginatedReadingPreview' | ||
: pathname.includes('/listening') | ||
? 'paginatedListeningPreview' | ||
: null; | ||
|
||
if (!type) { | ||
throw new Error('Invalid path: unable to determine query type'); | ||
} | ||
|
||
return [type, page, size, sort, direction, categoryId].filter( | ||
(item) => item !== null, | ||
); // 기본값(categoryId)이 null인 경우 제거 | ||
} | ||
|
||
throw new Error('Invalid path: no matching query key logic'); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 코드는 optimistic update가 굳이 모든 경로에 다 적용될 필요없이 내가 현재 있는 그 화면에만 적용되면 되게 만들어주는 일종의 최적화 코드군요~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
수고하셨습니다~
📄 Description of the PR
🔧 What has been changed?
📸 Screenshots / GIFs (if applicable)
✅ Checklist