-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* FE-43✨ 상세페이지 Epigram API연동 * FE-43⚡ ️axios 에러 핸들링 추가 * FE-43🏗️ 상세페이지 Layout 구조개선 * FE-43📝 주석 추가 * FE-43🔥 사용안하는 파일 삭제 * FE-43✏️ 오타 수정 * FE-43 🐛 id없을때 useQuery실행되는 문제 해결 * FE-43♻️ interface->zod 변경 --------- Co-authored-by: 우지석 <[email protected]>
- Loading branch information
Showing
11 changed files
with
168 additions
and
49 deletions.
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,47 @@ | ||
import axios, { AxiosError } from 'axios'; | ||
import { GetEpigramResponseType, GetEpigramRequestType } from '@/schema/epigram'; | ||
|
||
const BASE_URL = 'https://fe-project-epigram-api.vercel.app/5-9'; | ||
|
||
const getEpigram = async (request: GetEpigramRequestType): Promise<GetEpigramResponseType> => { | ||
const { id } = request; | ||
|
||
// id가 undefined인 경우 에러 throw | ||
if (id === undefined) { | ||
throw new Error('Epigram ID가 제공되지 않았습니다.'); | ||
} | ||
|
||
// NOTE : 임시로 테스트계정의 토큰을 변수로 선언해서 사용 | ||
const TOKEN = | ||
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MjIsInRlYW1JZCI6IjUtOSIsInNjb3BlIjoiYWNjZXNzIiwiaWF0IjoxNzIwODM4NTE5LCJleHAiOjE3MjA4NDAzMTksImlzcyI6InNwLWVwaWdyYW0ifQ.cPwm4T2LRi985p9NDqXrR0fSY5n-cvxzjGh8vmshoSM'; | ||
|
||
try { | ||
const response = await axios.get(`${BASE_URL}/epigrams/${id}`, { | ||
headers: { | ||
Authorization: `Bearer ${TOKEN}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
return response.data; | ||
} catch (error) { | ||
// 에러가 Axios에러인지 확인 | ||
if (axios.isAxiosError(error)) { | ||
const axiosError = error as AxiosError; | ||
if (axiosError.response) { | ||
// 서버에 요청 성공, 응답으로 200번대 이외의 상태를 응답으로 받았을때 | ||
throw new Error(`API 에러: ${axiosError.response.status}`); | ||
} else if (axiosError.request) { | ||
// 요청이 이루어졌으나 응답을 받지 못한 경우 | ||
throw new Error('서버로부터 응답을 받지 못했습니다.'); | ||
} else { | ||
// 요청 설정 중에 오류가 발생한 경우 | ||
throw new Error('요청 설정 중 오류가 발생했습니다.'); | ||
} | ||
} else { | ||
// axios 에러가 아닌 경우 | ||
throw new Error('예상치 못한 오류가 발생했습니다.'); | ||
} | ||
} | ||
}; | ||
|
||
export default getEpigram; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
import type { GetUserReponseType, GetUserRequestType, PatchMeRequestType } from '@/schema/user'; | ||
import type { GetUserResponseType, GetUserRequestType, PatchMeRequestType } from '@/schema/user'; | ||
import httpClient from '.'; | ||
|
||
export const getMe = async (): Promise<GetUserReponseType> => { | ||
export const getMe = async (): Promise<GetUserResponseType> => { | ||
const response = await httpClient.get('/users/me'); | ||
return response.data; | ||
}; | ||
|
||
export const getUser = async (request: GetUserRequestType): Promise<GetUserReponseType> => { | ||
export const getUser = async (request: GetUserRequestType): Promise<GetUserResponseType> => { | ||
const { id } = request; | ||
const response = await httpClient.get(`/users/${id}`); | ||
return response.data; | ||
}; | ||
|
||
export const updateMe = async (request: PatchMeRequestType): Promise<GetUserReponseType> => { | ||
export const updateMe = async (request: PatchMeRequestType): Promise<GetUserResponseType> => { | ||
const response = await httpClient.patch('/users/me', { ...request }); | ||
return response.data; | ||
}; |
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,11 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import queries from '@/apis/queries'; | ||
import { GetEpigramRequestType } from '@/schema/epigram'; | ||
|
||
const useEpigramQuery = (request: GetEpigramRequestType | undefined, enabled = true) => | ||
useQuery({ | ||
...queries.epigram.getEpigram(request ?? { id: undefined }), | ||
enabled: enabled && request?.id !== undefined, | ||
}); | ||
|
||
export default useEpigramQuery; |
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
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 was deleted.
Oops, something went wrong.
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,7 +1,34 @@ | ||
import EpigramLayout from '@/pageLayout/Epigram/EpigramLayout'; | ||
import { GetEpigramRequestSchema } from '@/schema/epigram'; | ||
import useEpigramQuery from '@/hooks/epigramQueryHook'; | ||
import CommentSection from '@/pageLayout/Epigram/EpigramComment'; | ||
import EpigramFigure from '@/pageLayout/Epigram/EpigramFigure'; | ||
import Image from 'next/image'; | ||
import { useRouter } from 'next/router'; | ||
|
||
function DetailPage() { | ||
return <EpigramLayout />; | ||
const router = useRouter(); | ||
const { id } = router.query; | ||
|
||
const parsedId = GetEpigramRequestSchema.safeParse({ id }); | ||
|
||
const { data: epigram, isLoading, error } = useEpigramQuery(parsedId.success ? parsedId.data : undefined, parsedId.success); | ||
|
||
if (isLoading) return <div>로딩 중...</div>; | ||
if (!parsedId.success) return <div>잘못된 Epigram ID입니다.</div>; | ||
if (error) return <div>에러 발생!! {(error as Error).message}</div>; | ||
if (!epigram) return <div>Epigram을 찾을 수 없습니다.</div>; | ||
|
||
return ( | ||
<div className='flex flex-col '> | ||
<nav className='flex justify-between border-b-2 px-6 py-4'> | ||
<Image src='/arrow-left.svg' alt='뒤로가기 버튼' width={36} height={36} /> | ||
<Image src='/logo.svg' alt='Epigram 로고' width={172} height={48} /> | ||
<Image src='/share.svg' alt='공유 버튼' width={36} height={36} /> | ||
</nav> | ||
<EpigramFigure epigram={epigram} /> | ||
<CommentSection /> | ||
</div> | ||
); | ||
} | ||
|
||
export default DetailPage; |
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,32 @@ | ||
import { z } from 'zod'; | ||
|
||
// Tag 스키마 | ||
const TagSchema = z.object({ | ||
name: z.string().min(1).max(10), | ||
id: z.number().int().positive(), | ||
}); | ||
|
||
// GetEpigramResponseType 스키마 | ||
const GetEpigramResponseSchema = z.object({ | ||
id: z.number().int().positive(), | ||
content: z.string().min(1).max(500), | ||
author: z.string().min(1).max(30), | ||
referenceTitle: z.string().max(100).nullable().optional(), | ||
referenceUrl: z.string().url().nullable().optional(), | ||
writerId: z.number().int().positive(), | ||
tags: z.array(TagSchema), | ||
likeCount: z.number(), | ||
isLiked: z.boolean().optional(), | ||
}); | ||
|
||
// GetEpigramRequestType 스키마 | ||
const GetEpigramRequestSchema = z.object({ | ||
id: z.union([z.string(), z.number(), z.undefined()]), | ||
}); | ||
|
||
// 타입 추론 | ||
export type Tag = z.infer<typeof TagSchema>; | ||
export type GetEpigramResponseType = z.infer<typeof GetEpigramResponseSchema>; | ||
export type GetEpigramRequestType = z.infer<typeof GetEpigramRequestSchema>; | ||
|
||
export { TagSchema, GetEpigramResponseSchema, GetEpigramRequestSchema }; |
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