-
Notifications
You must be signed in to change notification settings - Fork 3
♻️ Refactor(#237): queryCache를 이용한 리다이렉트 개선 #241
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
10a603e
♻️ refactor(#237): queryCache를 이용한 리다이렉트 개선
un0211 bde4363
Merge branch 'develop' into refactor/redirect
un0211 f3d4a21
🛠 fix(#237): 핸들링하지 않는 에러는 다시 throw, 쿼리는 올바른 id 얻은 뒤부터
un0211 9ec4681
Merge branch 'develop' into refactor/redirect
un0211 dafe7b1
🛠 fix(#237): queryClient가 캐시를 잘 할 수 있게 전역 배치
un0211 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,45 +1,63 @@ | ||
| import { useQueryClient } from '@tanstack/react-query'; | ||
| import { AxiosError } from 'axios'; | ||
| import { useRouter } from 'next/router'; | ||
| import { useEffect } from 'react'; | ||
| import { useSelector } from 'react-redux'; | ||
|
|
||
| import useRedirectIfAuth from '@/hooks/useRedirectIfAuth'; | ||
| import useRedirectIfNotAuth from '@/hooks/useRedirectIfNotAuth'; | ||
| import useRedirect from '@/hooks/useRedirect'; | ||
| import { RootState } from '@/store/store'; | ||
|
|
||
| // NOTE: 권한에 따라 페이지 이동 | ||
| export default function Redirect({ children }: { children: React.ReactNode }) { | ||
| const router = useRouter(); | ||
| const redirect = useRedirect(); | ||
| const { user } = useSelector((state: RootState) => state.user); | ||
| const router = useRouter(); | ||
| const currentPath = router.pathname; | ||
|
|
||
| const redirectIfAuth = useRedirectIfAuth(); | ||
| const redirectIfNotAuth = useRedirectIfNotAuth(); | ||
|
|
||
| // NOTE: 로그인 한 경우 mydashboard를 default로 | ||
| if (currentPath === '/' && user) { | ||
| router.replace('/mydashboard'); | ||
| } | ||
|
|
||
| // NOTE: 잘못된 주소 접근 시 3초 뒤 적절한 페이지로 이동 | ||
| if (currentPath === '/404') { | ||
| const nextPath = user ? '/mydashboard' : '/'; | ||
| setTimeout(() => router.push(nextPath), 3000); | ||
| } | ||
|
|
||
| // NOTE: 로그인한 사용자가 로그인/회원가입 접근 시 mydashboard로 이동 | ||
| if (['/signup', '/signin'].includes(currentPath)) { | ||
| const isRedirecting = redirectIfAuth(); | ||
| if (isRedirecting) { | ||
| return <></>; | ||
| // NOTE: QueryCache로 글로벌 콜백 설정 | ||
| const queryClient = useQueryClient(); | ||
| const queryCache = queryClient.getQueryCache(); | ||
| queryCache.config = { | ||
| onError: (error) => { | ||
| if (error instanceof AxiosError) { | ||
| switch (error.response?.status) { | ||
| case 401: | ||
| redirect('/signin', '로그인이 필요합니다'); | ||
| break; | ||
| case 403: | ||
| case 404: | ||
| if (user) redirect('/mydashboard', '접근 권한이 없습니다'); | ||
| else redirect('/signin', '접근 권한이 없습니다'); | ||
| break; | ||
| default: | ||
| throw error; | ||
| } | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| // NOTE: 로그인 한 경우 mydashboard를 default로 | ||
| if (currentPath === '/' && user) { | ||
| router.replace('/mydashboard'); | ||
| } | ||
|
|
||
| // NOTE: 잘못된 주소 접근 시 3초 뒤 적절한 페이지로 이동 | ||
| if (currentPath === '/404') { | ||
| const nextPath = user ? '/mydashboard' : '/'; | ||
| setTimeout(() => router.push(nextPath), 3000); | ||
| } | ||
|
|
||
| // NOTE: 로그인/회원가입 - 로그인 O -> 내 대시보드 | ||
| if (['/signup', '/signin'].includes(currentPath) && user) { | ||
| redirect('/mydashboard', '이미 로그인했습니다'); | ||
| } | ||
| } | ||
|
|
||
| // TODO: 추후 리팩토링 예정 | ||
| if (['/mypage', '/mydashboard'].includes(currentPath)) { | ||
| const isRedirecting = redirectIfNotAuth(); | ||
| if (isRedirecting) { | ||
| return <></>; | ||
| // NOTE: 내 대시보드/계정관리 - 로그인 X -> 로그인 | ||
| if (['/mypage', '/mydashboard'].includes(currentPath) && !user) { | ||
| redirect('/signin', '로그인이 필요합니다'); | ||
| } | ||
| } | ||
| }, [currentPath, router.query.id]); | ||
|
|
||
| return children; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,13 +8,11 @@ import Column from './Column'; | |
|
|
||
| import useFetchData from '@/hooks/useFetchData'; | ||
| import useModal from '@/hooks/useModal'; | ||
| import useRedirectIfNotMember from '@/hooks/useRedirectIfNotMember'; | ||
| import instance from '@/services/axios'; | ||
| import { getColumnsList, getDashboard } from '@/services/getService'; | ||
| import { getColumnsList } from '@/services/getService'; | ||
| import { moveToOtherColumn } from '@/services/putService'; | ||
| import { RootState } from '@/store/store'; | ||
| import { ColumnsResponse } from '@/types/Column.interface'; | ||
| import { checkPublic } from '@/utils/shareAccount'; | ||
|
|
||
| interface ColumnsSectionProps { | ||
| dashboardId: string; | ||
|
|
@@ -23,10 +21,8 @@ interface ColumnsSectionProps { | |
| export default function ColumnsSection({ dashboardId }: ColumnsSectionProps) { | ||
| const queryClient = useQueryClient(); | ||
| const { openNewColumnModal, openNotificationModal } = useModal(); | ||
| const redirectIfNotMember = useRedirectIfNotMember(); | ||
| const { user } = useSelector((state: RootState) => state.user); | ||
| const [isMember, setIsMember] = useState(true); | ||
| const [isPublic, setIsPublic] = useState(false); | ||
|
|
||
| const { | ||
| data: columns, | ||
|
|
@@ -37,31 +33,17 @@ export default function ColumnsSection({ dashboardId }: ColumnsSectionProps) { | |
| const columnList = columns?.data || []; | ||
|
|
||
| useEffect(() => { | ||
| const handleRedirect = async () => { | ||
| const handleCheckMember = async () => { | ||
| if (!dashboardId) return; | ||
| try { | ||
| const newIsPublic = await checkPublic(Number(dashboardId)); | ||
| setIsPublic(newIsPublic); | ||
| if (!newIsPublic && dashboardId) { | ||
| await getDashboard(String(dashboardId)); | ||
| } | ||
| await instance.get(`/dashboards/${dashboardId}`, { | ||
| headers: { memberTest: true }, | ||
| }); | ||
| } catch { | ||
| redirectIfNotMember(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 리다이렉션 관련 코드 다 지웠습니다! |
||
| } | ||
| }; | ||
|
|
||
| const handleCheckMember = async () => { | ||
| if (dashboardId) { | ||
| try { | ||
| await instance.get(`/dashboards/${dashboardId}`, { | ||
| headers: { memberTest: true }, | ||
| }); | ||
| } catch { | ||
| setIsMember(false); | ||
| } | ||
| setIsMember(false); | ||
| } | ||
| }; | ||
|
|
||
| handleRedirect(); | ||
| handleCheckMember(); | ||
| }, [dashboardId, user]); | ||
|
|
||
|
|
@@ -106,7 +88,7 @@ export default function ColumnsSection({ dashboardId }: ColumnsSectionProps) { | |
| ) : ( | ||
| <DragDropContext onDragEnd={onDragEnd}> | ||
| <section | ||
| className={`block h-full overflow-x-auto lg:flex lg:h-[calc(100dvh-70px)] ${isPublic && !user ? 'lg:w-screen' : 'lg:w-[calc(100dvw-300px)]'}`} | ||
| className={`block h-full overflow-x-auto lg:flex lg:h-[calc(100dvh-70px)] ${!user ? 'lg:w-screen' : 'lg:w-[calc(100dvw-300px)]'}`} | ||
| > | ||
| <ul className='block lg:flex'> | ||
| {columnList.map((column, index) => ( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,13 +14,11 @@ const useFetchData = <T>( | |
| const response = await getService(); | ||
| return response.data; | ||
| } catch (error) { | ||
| throw new Error('데이터를 불러오는 중 에러 발생: ' + error); | ||
| throw error; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 원본 에러 보내주도록 했습니다. |
||
| } | ||
| }, | ||
| retry: 1, // NOTE: 요청 실패시 1회까지만 재시도하도록 제한 | ||
| refetchInterval: refetchInterval, // NOTE: 주기적인 초대내역 수신을 위해 설정 | ||
| enabled: enabled, | ||
| refetchInterval: refetchInterval, | ||
| refetchInterval: refetchInterval, // NOTE: 주기적인 초대내역 수신을 위해 설정 | ||
| }); | ||
| }; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { useRouter } from 'next/router'; | ||
| import { useEffect, useState } from 'react'; | ||
|
|
||
| import useModal from '@/hooks/useModal'; | ||
|
|
||
| const useRedirect = () => { | ||
| const { openNotificationModal } = useModal(); | ||
| const router = useRouter(); | ||
| const { id } = router.query; | ||
| const [initialCheck, setInitialCheck] = useState(true); | ||
|
|
||
| useEffect(() => { | ||
| setInitialCheck(true); | ||
| }, [router.pathname, id]); | ||
|
|
||
| return (path: string, text: string) => { | ||
| if (initialCheck) { | ||
| openNotificationModal({ text }); | ||
| router.replace(path); | ||
| setInitialCheck(false); | ||
| } | ||
| }; | ||
| }; | ||
|
|
||
| export default useRedirect; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
멤버인지 체크하는 원리는, 멤버만 할 수 있는 요청을 보내고 catch에서 실패할 경우 멤버가 아니라고 판단하는 것입니다.
이게 여전히 잘 동작하는 걸로 봐서, Query Cache의 onError는 모든 재요청 후, 실패에 대한 try catch까지 거친 뒤에도 핸들링되지 않은 요청을 받는 것 같습니다.
retry가 있는 경우 리다이렉션이 느려져서 실패한 경우에도 retry하지 않도록 설정했습니다.
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.
리트라이는 더블체크용도 였어서 중요한 부분은 아니여서 없어도 괜찮을거 같아요 테스트해보다가 문제가 있다면 그때 다시 추가하면 될거 같습니다