From c8e3503c5569834f9ba451a9fd9c8b7d9f394712 Mon Sep 17 00:00:00 2001 From: w-ho-choo Date: Fri, 10 Jan 2025 16:50:51 +0900 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20:=20userService?= =?UTF-8?q?=20=ED=8C=8C=EC=9D=BC=20=ED=83=80=EC=9E=85=EC=A7=80=EC=A0=95=20?= =?UTF-8?q?=EB=B0=8F=20=EA=B8=B0=ED=83=80=20=EC=98=A4=EB=A5=98=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20#213?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/userService.ts | 13 +++++++++---- src/pages/GoodsRecordPage/index.tsx | 15 ++++---------- src/pages/ProfilePage/ProfileMain.tsx | 2 +- src/typings/db.ts | 28 +++++++++++++++++++++++++++ src/typings/userForm.ts | 16 +++++++++++++++ 5 files changed, 58 insertions(+), 16 deletions(-) diff --git a/src/apis/userService.ts b/src/apis/userService.ts index c55becb..fc9efa8 100644 --- a/src/apis/userService.ts +++ b/src/apis/userService.ts @@ -1,15 +1,21 @@ +import { GoodsRecordApiResponse } from './../typings/db' import { ProfileEditApiResponse } from '@typings/db' import fetchApi from './ky' +import { MyDataApiReponse, UserDataApiResponse } from '@typings/userForm' const userService = { getUserInfo: async (userId: number) => { - const response = await fetchApi.get(`members/${userId}`).json() + const response: UserDataApiResponse = await fetchApi + .get(`members/${userId}`) + .json() return response.data }, getMyInfo: async (userId: number) => { - const response = await fetchApi.get(`members/me?memberId=${userId}`).json() + const response: MyDataApiReponse = await fetchApi + .get(`members/me?memberId=${userId}`) + .json() return response.data }, @@ -29,10 +35,9 @@ const userService = { callingType: string, page: number, ) => { - const response = await fetchApi + const response: GoodsRecordApiResponse = await fetchApi .get(`profile/${memberId}/goods/${callingType}?page=${page}&size=5`) .json() - return response.data }, } diff --git a/src/pages/GoodsRecordPage/index.tsx b/src/pages/GoodsRecordPage/index.tsx index a1deb33..4ae31e6 100644 --- a/src/pages/GoodsRecordPage/index.tsx +++ b/src/pages/GoodsRecordPage/index.tsx @@ -7,26 +7,17 @@ import { useLocation, useNavigate, useParams } from 'react-router-dom' import { useInfiniteQuery } from '@tanstack/react-query' import { QUERY_KEY } from '@apis/queryClient' import userService from '@apis/userService' -import { useInView } from 'react-intersection-observer' import { RefContainer } from '@styles/globalStyle' import Spinner from '@components/Spinner' import { toast } from 'react-toastify' import { ROUTE_PATH } from '@constants/ROUTE_PATH' +import { GoodsRecordData } from '@typings/db' const HEADER_TEXT = { sold: '굿즈 판매기록', bought: '굿즈 구매기록', } -interface GoodsRecord { - author: string - createdAt: string - imageUrl: string - postId: number - price: number - title: string -} - const GoodsRecordPage = () => { const location = useLocation() const navigate = useNavigate() @@ -87,7 +78,9 @@ const GoodsRecordPage = () => { if (!data) return null const { pages } = data - const goodsRecordList: GoodsRecord[] = pages.flatMap((page) => page.content) + const goodsRecordList: GoodsRecordData[] = pages.flatMap((page) => + page ? page.content : [], + ) return ( <> diff --git a/src/pages/ProfilePage/ProfileMain.tsx b/src/pages/ProfilePage/ProfileMain.tsx index 76423b1..c5be4cc 100644 --- a/src/pages/ProfilePage/ProfileMain.tsx +++ b/src/pages/ProfilePage/ProfileMain.tsx @@ -49,7 +49,7 @@ const ProfileMain = () => { const [userId, setUserId] = useState(id) const [isMyProfile, setIsMyProfile] = useState(null) - const [userInfo, setUserInfo] = useState(null) + const [userInfo, setUserInfo] = useState(undefined) const myInfoResult = useGetMyInfo(Number(loginMemberId)) const userInfoResult = useGetUserInfo( diff --git a/src/typings/db.ts b/src/typings/db.ts index 7658ca8..0c3f7dc 100644 --- a/src/typings/db.ts +++ b/src/typings/db.ts @@ -304,3 +304,31 @@ export interface ProfileEditApiResponse { status: string timestamp: string } + +// userService.getGoodsRecordList + +export interface GoodsRecordData { + postId: number + title: string + imageUrl: string + price: number + author: string + createdAt: string +} + +export interface GoodsRecordReponse { + content: GoodsRecordData[] + hasNext: boolean + pageNumber: number + pageSize: number + totalElements: number + totalPages: number +} + +export interface GoodsRecordApiResponse { + code: number + data: GoodsRecordReponse + message: string | null + status: string + timestamp: string +} diff --git a/src/typings/userForm.ts b/src/typings/userForm.ts index 2e7ff66..e9bc3e7 100644 --- a/src/typings/userForm.ts +++ b/src/typings/userForm.ts @@ -11,3 +11,19 @@ export interface UserInfo { teamName: string visitsCount?: number } + +export interface UserDataApiResponse { + code: number + data: UserInfo + message: string | null + status: string + timestamp: string +} + +export interface MyDataApiReponse { + code: number + data: UserInfo + message: string | null + status: string + timestamp: string +}