-
Notifications
You must be signed in to change notification settings - Fork 6
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
[FE] Refactor/#642: 북마크 및 전체보기 페이지 API 로직 수정 및 React-Query 적용 #644
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
3717ebd
feat: Axios http 정의 및 get 메서드 구현
semnil5202 9ab2f36
refactor: Bookmarks 페이지 TopicCardList 분리 코드 합병
semnil5202 ab32302
design: Skeleton 컴포넌트 스타일 변경 및 적용
semnil5202 6f12bb4
refactor: SkeletonBox 공통 컴포넌트 구현 및 convertCSS 유틸 함수 구현
semnil5202 971a3b6
refactor: Skeleton 컴포넌트 Bookmark 페이지에 적용
semnil5202 76b4ae7
refactor: http api 수정 및 useGetBookmark isLoading 상태 사용
semnil5202 5ca854e
refactor: SeeAllNearTopics 페이지 TopicCardList 분리 및 코드 합병
semnil5202 89c5256
refactor: API 로직 반환값 타입 지정
semnil5202 7dd724a
refactor: Bookmark 스켈레톤 수정
semnil5202 6038f39
refactor: SeeAllLatestTopics 페이지 TopicCardList 분리 및 코드 합병
semnil5202 0efd169
refactor: SeeAllPopularTopics 페이지 TopicCardList 분리 및 코드 합병
semnil5202 fb7234b
refactor: AllTopics Query key 수정 및 시맨틱 태그 수정
semnil5202 10e9713
rename: API 명세와 페이지 이름 통일화
semnil5202 5c97955
refactor: 전체보기 페이지 명칭 수정 router 적용
semnil5202 e9a8463
feat: 리프레쉬 토큰 요청 기능 추가
semnil5202 a53b435
feat: query default option 설정
semnil5202 8db43f2
refactor: useSuspenseQuery 를 통한 선언적으로 로딩상태 처리
semnil5202 4b3e0f9
fix: token 없을 때 Authorization 빈 객체로 세팅하여 비로그인 오류 해결
semnil5202 f2fca6e
refactor: withCredentials 옵션 잠시 보류
semnil5202 645fbf2
refactor: 01.17 회의를 통한 변경
semnil5202 1e1a7a3
Merge branch 'develop-FE' into refactor/#642
jiwonh423 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 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,14 @@ | ||
import { useSuspenseQuery } from '@tanstack/react-query'; | ||
|
||
import { getTopics } from '../../apis/new'; | ||
|
||
const useGetTopics = (url: string) => { | ||
const { data: topics, refetch } = useSuspenseQuery({ | ||
queryKey: ['topics', url], | ||
queryFn: () => getTopics(url), | ||
}); | ||
|
||
return { topics, refetch }; | ||
}; | ||
|
||
export default useGetTopics; |
This file contains 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,101 @@ | ||
import axios, { | ||
AxiosInstance, | ||
AxiosRequestConfig, | ||
AxiosRequestHeaders, | ||
} from 'axios'; | ||
|
||
const API_POSTFIX = 'api'; | ||
const BASE_URL = process.env.APP_URL || `https://mapbefine.com/${API_POSTFIX}`; | ||
const token = localStorage.getItem('userToken'); | ||
|
||
const axiosInstance = axios.create({ | ||
baseURL: BASE_URL, | ||
headers: token ? { Authorization: `Bearer ${token}` } : {}, | ||
// withCredentials: true, | ||
}); | ||
|
||
let refreshResponse: Promise<Response> | null = null; | ||
|
||
export interface HttpClient extends AxiosInstance { | ||
get<T = unknown>(url: string, config?: AxiosRequestConfig): Promise<T>; | ||
post<T = unknown>( | ||
url: string, | ||
data?: any, | ||
config?: AxiosRequestConfig, | ||
): Promise<T>; | ||
patch<T = unknown>( | ||
url: string, | ||
data?: any, | ||
config?: AxiosRequestConfig, | ||
): Promise<T>; | ||
put<T = unknown>( | ||
url: string, | ||
data?: any, | ||
config?: AxiosRequestConfig, | ||
): Promise<T>; | ||
delete<T = unknown>(url: string, config?: AxiosRequestConfig): Promise<T>; | ||
} | ||
|
||
export const http: HttpClient = axiosInstance; | ||
|
||
http.interceptors.response.use((res) => res.data); | ||
http.interceptors.request.use( | ||
async (config) => { | ||
const userToken = localStorage.getItem('userToken'); | ||
|
||
if (userToken && isTokenExpired(userToken)) { | ||
await updateToken(config.headers); | ||
} | ||
return config; | ||
}, | ||
(error) => Promise.reject(error), | ||
); | ||
|
||
const isTokenExpired = (token: string) => { | ||
const decodedPayloadObject = decodeToken(token); | ||
return decodedPayloadObject.exp * 1000 < Date.now(); | ||
}; | ||
|
||
const decodeToken = (token: string) => { | ||
const tokenParts = token.split('.'); | ||
|
||
if (tokenParts.length !== 3) { | ||
throw new Error('토큰이 잘못되었습니다.'); | ||
} | ||
|
||
const decodedPayloadString = atob(tokenParts[1]); | ||
|
||
return JSON.parse(decodedPayloadString); | ||
}; | ||
|
||
async function updateToken(headers: AxiosRequestHeaders) { | ||
const response = await refreshToken(headers); | ||
const responseCloned = response.clone(); | ||
const newToken = await responseCloned.json(); | ||
|
||
localStorage.setItem('userToken', newToken.accessToken); | ||
} | ||
|
||
async function refreshToken(headers: AxiosRequestHeaders): Promise<Response> { | ||
if (refreshResponse !== null) { | ||
return refreshResponse; | ||
} | ||
|
||
const accessToken = localStorage.getItem('userToken'); | ||
refreshResponse = fetch(`${BASE_URL}/refresh-token`, { | ||
method: 'POST', | ||
headers, | ||
body: JSON.stringify({ | ||
accessToken, | ||
}), | ||
}); | ||
|
||
const responseData = await refreshResponse; | ||
refreshResponse = null; | ||
|
||
if (!responseData.ok) { | ||
throw new Error('Failed to refresh access token.'); | ||
} | ||
|
||
return responseData; | ||
} |
This file contains 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,4 @@ | ||
import { TopicCardProps } from '../../types/Topic'; | ||
import { http } from './http'; | ||
|
||
export const getTopics = (url: string) => http.get<TopicCardProps[]>(url); |
This file contains 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,53 +1,21 @@ | ||
import { keyframes, styled } from 'styled-components'; | ||
|
||
import Flex from '../common/Flex'; | ||
import Box from '../common/Box'; | ||
import Space from '../common/Space'; | ||
import SkeletonBox from './common/SkeletonBox'; | ||
|
||
function TopicCardSkeleton() { | ||
return ( | ||
<Flex $flexDirection="row"> | ||
<SkeletonImg /> | ||
<Space size={2} /> | ||
<Flex $flexDirection="column"> | ||
<SkeletonTitle /> | ||
<Space size={5} /> | ||
<SkeletonDescription /> | ||
</Flex> | ||
</Flex> | ||
<Box> | ||
<SkeletonBox width="100%" $maxWidth={212} ratio="1.6 / 1" /> | ||
<Space size={1} /> | ||
<SkeletonBox width={212} height={25} /> | ||
<Space size={5} /> | ||
<SkeletonBox width={100} height={25} /> | ||
<Space size={1} /> | ||
<SkeletonBox width={212} height={46} /> | ||
</Box> | ||
); | ||
} | ||
|
||
const skeletonAnimation = keyframes` | ||
from { | ||
opacity: 0.1; | ||
} | ||
to { | ||
opacity: 1; | ||
} | ||
`; | ||
|
||
const SkeletonImg = styled.div` | ||
width: 138px; | ||
height: 138px; | ||
|
||
border-radius: 8px; | ||
|
||
background: ${({ theme }) => theme.color.lightGray}; | ||
animation: ${skeletonAnimation} 1s infinite; | ||
`; | ||
|
||
const SkeletonTitle = styled.div` | ||
width: 172px; | ||
height: 32px; | ||
|
||
border-radius: 8px; | ||
|
||
background: ${({ theme }) => theme.color.lightGray}; | ||
animation: ${skeletonAnimation} 1s infinite; | ||
`; | ||
|
||
const SkeletonDescription = styled(SkeletonTitle)` | ||
height: 80px; | ||
`; | ||
|
||
export default TopicCardSkeleton; |
This file contains 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,26 +1,40 @@ | ||
import { styled } from 'styled-components'; | ||
|
||
import Space from '../common/Space'; | ||
import SkeletonBox from './common/SkeletonBox'; | ||
import TopicCardSkeleton from './TopicCardSkeleton'; | ||
|
||
function TopicCardContainerSkeleton() { | ||
function TopicListSkeleton() { | ||
return ( | ||
<Wrapper> | ||
<TopicCardSkeleton /> | ||
<TopicCardSkeleton /> | ||
<TopicCardSkeleton /> | ||
<TopicCardSkeleton /> | ||
<TopicCardSkeleton /> | ||
<TopicCardSkeleton /> | ||
</Wrapper> | ||
<> | ||
<Space size={5} /> | ||
<SkeletonBox width={160} height={32} /> | ||
<Space size={4} /> | ||
<Space size={5} /> | ||
<TopicCardWrapper> | ||
<TopicCardSkeleton /> | ||
<TopicCardSkeleton /> | ||
<TopicCardSkeleton /> | ||
<TopicCardSkeleton /> | ||
<TopicCardSkeleton /> | ||
</TopicCardWrapper> | ||
<Space size={4} /> | ||
<TopicCardWrapper> | ||
<TopicCardSkeleton /> | ||
<TopicCardSkeleton /> | ||
<TopicCardSkeleton /> | ||
<TopicCardSkeleton /> | ||
<TopicCardSkeleton /> | ||
</TopicCardWrapper> | ||
</> | ||
); | ||
} | ||
|
||
const Wrapper = styled.section` | ||
const TopicCardWrapper = styled.section` | ||
display: flex; | ||
flex-wrap: wrap; | ||
gap: 20px; | ||
width: 1036px; | ||
height: 300px; | ||
width: 1140px; | ||
`; | ||
|
||
export default TopicCardContainerSkeleton; | ||
export default TopicListSkeleton; |
This file contains 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,38 @@ | ||
import styled, { keyframes } from 'styled-components'; | ||
|
||
import { convertCSS } from '../../../utils/convertCSS'; | ||
|
||
interface Props { | ||
width?: number | string; | ||
height?: number | string; | ||
$maxWidth?: number | string; | ||
$maxHeight?: number | string; | ||
ratio?: string; | ||
radius?: number | string; | ||
} | ||
|
||
const skeletonAnimation = keyframes` | ||
from { | ||
opacity: 1; | ||
} | ||
50% { | ||
opacity: 0.6; | ||
} | ||
to { | ||
opacity: 1; | ||
} | ||
`; | ||
|
||
const SkeletonBox = styled.div<Props>` | ||
width: ${({ width }) => width && convertCSS(width)}; | ||
height: ${({ height }) => height && convertCSS(height)}; | ||
max-width: ${({ $maxWidth }) => $maxWidth && convertCSS($maxWidth)}; | ||
max-height: ${({ $maxHeight }) => $maxHeight && convertCSS($maxHeight)}; | ||
aspect-ratio: ${({ ratio }) => ratio}; | ||
border-radius: ${({ radius, theme }) => | ||
(radius && convertCSS(radius)) || theme.radius.small}; | ||
background: ${({ theme }) => theme.color.lightGray}; | ||
animation: ${skeletonAnimation} 1s infinite; | ||
`; | ||
|
||
export default SkeletonBox; |
This file contains 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 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
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.
styled component로 만들었던 SkeletonImg나 SkeletonTitle을 효율적으로 쓸 수 있도록 SkeletonBox를 만들었군요 아주 멋집니다 👍
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.
스켈레톤 한 번 싹 정리해볼 필요가 있겠네용. 예전 디자인이랑 많이 달라져서..