From 8c4232ff2b1fe85075a7d0d8477708af3b68f804 Mon Sep 17 00:00:00 2001 From: eunjin Date: Tue, 9 Jul 2024 21:33:01 +0900 Subject: [PATCH 1/8] =?UTF-8?q?docs=20:=20Readme.md=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index e69de29b..a4e93369 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,16 @@ +# 3주차 상품리스트 구현 - api +## step1 📝구현사항 ++ 첨부된 oas.yaml 파일을 토대로 Request, Response Type을 정의하기 ++ axios 라이브러리 설치 +* 메인페이지 - theme + * /api/v1/themes API를 사용하여 Section을 구현 + * API는 Axios또는 React Query 등을 모두 활용해서 구현 가능 +* 메인 페이지 - 실시간 급상승 선물랭킹 + * /api/v1/ranking/products API를 사용하여 Section을 구현 + * 필터 조건을 선택 하면 해당 조건에 맞게 API를 요청하여 보여지게 해야함 +* Theme 페이지 - header + * url의 pathParams와 /api/v1/themes API를 사용하여 Section을 구현 + * themeKey가 잘못 된 경우 메인 페이지로 연결 +* Theme 페이지 - 상품 목록 섹션 + * /api/v1/themes/{themeKey}/products API를 사용하여 상품 목록을 구현 + *API 요청 시 한번에 20개의 상품 목록이 내려오게 구현 From 911266177941a230a9d1ed607e978a0b2131f4d5 Mon Sep 17 00:00:00 2001 From: eunjin Date: Tue, 9 Jul 2024 21:34:24 +0900 Subject: [PATCH 2/8] =?UTF-8?q?Feat=20:=20axios=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 29 ++++++++++++++++------------- package.json | 1 + 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index 89581c64..74216d7d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "dependencies": { "@emotion/react": "^11.11.3", "@emotion/styled": "^11.11.0", + "axios": "^1.7.2", "react": "^18.2.0", "react-dom": "^18.2.0", "react-router-dom": "^6.22.1" @@ -11988,8 +11989,7 @@ "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", - "dev": true + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" }, "node_modules/at-least-node": { "version": "1.0.0", @@ -12061,6 +12061,16 @@ "node": ">=4" } }, + "node_modules/axios": { + "version": "1.7.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.2.tgz", + "integrity": "sha512-2A8QhOMrbomlDuiLeK9XibIBzuHeRcqqNOHp0Cyp5EoJ1IFDh+XZH3A6BkXtv0K4gFGCI0Y4BM7B1wOEi0Rmgw==", + "dependencies": { + "follow-redirects": "^1.15.6", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/axobject-query": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-3.2.1.tgz", @@ -13498,7 +13508,6 @@ "version": "1.0.8", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "dev": true, "dependencies": { "delayed-stream": "~1.0.0" }, @@ -15481,7 +15490,6 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", - "dev": true, "engines": { "node": ">=0.4.0" } @@ -18405,10 +18413,9 @@ } }, "node_modules/follow-redirects": { - "version": "1.15.5", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz", - "integrity": "sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==", - "dev": true, + "version": "1.15.6", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz", + "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==", "funding": [ { "type": "individual", @@ -18614,7 +18621,6 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", - "dev": true, "dependencies": { "asynckit": "^0.4.0", "combined-stream": "^1.0.8", @@ -24744,7 +24750,6 @@ "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", - "dev": true, "engines": { "node": ">= 0.6" } @@ -24753,7 +24758,6 @@ "version": "2.1.35", "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", - "dev": true, "dependencies": { "mime-db": "1.52.0" }, @@ -27576,8 +27580,7 @@ "node_modules/proxy-from-env": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", - "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==", - "dev": true + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" }, "node_modules/psl": { "version": "1.9.0", diff --git a/package.json b/package.json index 0a6f0b8f..f1f65cfa 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "dependencies": { "@emotion/react": "^11.11.3", "@emotion/styled": "^11.11.0", + "axios": "^1.7.2", "react": "^18.2.0", "react-dom": "^18.2.0", "react-router-dom": "^6.22.1" From 2d46f943c598bae8867c63632822218e09c132b4 Mon Sep 17 00:00:00 2001 From: eunjin Date: Tue, 9 Jul 2024 23:10:31 +0900 Subject: [PATCH 3/8] =?UTF-8?q?feat:=20api/index.ts=20theme=20=EC=B9=B4?= =?UTF-8?q?=ED=85=8C=EA=B3=A0=EB=A6=AC=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/index.ts | 16 +++ .../Home/GoodsRankingSection/index.tsx | 2 - .../Home/ThemeCategorySection/index.tsx | 100 +++++------------- src/types/index.ts | 1 + src/types/mock.ts | 2 + 5 files changed, 46 insertions(+), 75 deletions(-) create mode 100644 src/api/index.ts diff --git a/src/api/index.ts b/src/api/index.ts new file mode 100644 index 00000000..367897af --- /dev/null +++ b/src/api/index.ts @@ -0,0 +1,16 @@ +import axios from 'axios'; + +import type { ThemeData } from '@/types'; + +const api = axios.create({ + baseURL: 'https://react-gift-mock-api-eunjin.vercel.app/',}); + +export const getTheme = async (): Promise => { + const response = await api.get<{ themes: ThemeData[] }>('/api/v1/themes'); + return response.data.themes; +}; + + + +export default api; + diff --git a/src/components/features/Home/GoodsRankingSection/index.tsx b/src/components/features/Home/GoodsRankingSection/index.tsx index 9464d67c..e90631ed 100644 --- a/src/components/features/Home/GoodsRankingSection/index.tsx +++ b/src/components/features/Home/GoodsRankingSection/index.tsx @@ -1,11 +1,9 @@ import styled from '@emotion/styled'; import { useState } from 'react'; - import { Container } from '@/components/common/layouts/Container'; import { breakpoints } from '@/styles/variants'; import type { RankingFilterOption } from '@/types'; import { GoodsMockList } from '@/types/mock'; - import { GoodsRankingFilter } from './Filter'; import { GoodsRankingList } from './List'; diff --git a/src/components/features/Home/ThemeCategorySection/index.tsx b/src/components/features/Home/ThemeCategorySection/index.tsx index d82e3afe..1cf3710e 100644 --- a/src/components/features/Home/ThemeCategorySection/index.tsx +++ b/src/components/features/Home/ThemeCategorySection/index.tsx @@ -1,14 +1,32 @@ import styled from '@emotion/styled'; +import { useEffect, useState } from 'react'; import { Link } from 'react-router-dom'; +import { getTheme } from '@/api'; import { Container } from '@/components/common/layouts/Container'; import { Grid } from '@/components/common/layouts/Grid'; import { getDynamicPath } from '@/routes/path'; import { breakpoints } from '@/styles/variants'; +import type { ThemeData } from '@/types'; import { ThemeCategoryItem } from './ThemeCategoryItem'; -export const ThemeCategorySection = () => { +export const ThemeCategorySection: React.FC = () => { + const [themes, setThemes] = useState([]); + + useEffect(() => { + const fetchThemes = async () => { + try { + const data = await getTheme(); + setThemes(data); + } catch (error) { + console.error('Failed to fetch themes:', error); + } + }; + + fetchThemes(); + }, []); + return ( @@ -18,78 +36,14 @@ export const ThemeCategorySection = () => { md: 6, }} > - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + {themes.map((theme) => ( + + + + ))} diff --git a/src/types/index.ts b/src/types/index.ts index 9d76b97b..fcf55803 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -5,6 +5,7 @@ export type ThemeData = { title: string; description?: string; backgroundColor: string; + imageURL: string; }; export type RankingFilterOption = { diff --git a/src/types/mock.ts b/src/types/mock.ts index cdd90cf7..ff0892cc 100644 --- a/src/types/mock.ts +++ b/src/types/mock.ts @@ -7,8 +7,10 @@ export const ThemeMockData: ThemeData = { title: '예산은 가볍게, 감동은 무겁게❤️', description: '당신의 센스를 뽐내줄 부담 없는 선물', backgroundColor: '#4b4d50', + imageURL: 'https://example.com/image.png', }; + export const ThemeMockList = [ThemeMockData]; export const GoodsMockData: GoodsData = { From a15a16b2aa8a80ef5da0db63bf87f9cb9476e338 Mon Sep 17 00:00:00 2001 From: eunjin Date: Wed, 10 Jul 2024 02:06:56 +0900 Subject: [PATCH 4/8] =?UTF-8?q?add=20:=20=EC=8B=A4=EC=8B=9C=EA=B0=84=20?= =?UTF-8?q?=EA=B8=89=EC=83=81=EC=8A=B9=20=EB=9E=AD=ED=82=B9=20api=20?= =?UTF-8?q?=EC=97=B0=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/index.ts | 9 ++++- .../Home/GoodsRankingSection/index.tsx | 39 ++++++++++++++++--- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/api/index.ts b/src/api/index.ts index 367897af..e6234bec 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,6 +1,6 @@ import axios from 'axios'; -import type { ThemeData } from '@/types'; +import type { GoodsData, RankingFilterOption,ThemeData } from '@/types'; const api = axios.create({ baseURL: 'https://react-gift-mock-api-eunjin.vercel.app/',}); @@ -10,6 +10,13 @@ export const getTheme = async (): Promise => { return response.data.themes; }; +export const getRankingGoods = async (filterOption: RankingFilterOption): Promise => { + const response = await api.get<{ products: GoodsData[] }>('/api/v1/ranking/products', { + params: filterOption, + }); + return response.data.products; +}; + export default api; diff --git a/src/components/features/Home/GoodsRankingSection/index.tsx b/src/components/features/Home/GoodsRankingSection/index.tsx index e90631ed..f4c61cfa 100644 --- a/src/components/features/Home/GoodsRankingSection/index.tsx +++ b/src/components/features/Home/GoodsRankingSection/index.tsx @@ -1,26 +1,46 @@ import styled from '@emotion/styled'; -import { useState } from 'react'; +import { useEffect, useState } from 'react'; + +import { getRankingGoods } from '@/api'; import { Container } from '@/components/common/layouts/Container'; import { breakpoints } from '@/styles/variants'; -import type { RankingFilterOption } from '@/types'; -import { GoodsMockList } from '@/types/mock'; +import type { GoodsData, RankingFilterOption } from '@/types'; + import { GoodsRankingFilter } from './Filter'; import { GoodsRankingList } from './List'; -export const GoodsRankingSection = () => { +export const GoodsRankingSection: React.FC = () => { const [filterOption, setFilterOption] = useState({ targetType: 'ALL', rankType: 'MANY_WISH', }); + const [goodsList, setGoodsList] = useState([]); + const [errorMessage, setErrorMessage] = useState(null); // 변수 이름 변경 + + useEffect(() => { + const fetchGoods = async () => { + try { + const data = await getRankingGoods(filterOption); + setGoodsList(data); + } catch (err) { + setErrorMessage('데이터를 불러오는데 실패하였습니다.'); + + } + }; - // GoodsMockData를 21번 반복 생성 + fetchGoods(); + }, [filterOption]); return ( 실시간 급상승 선물랭킹 - + {errorMessage ? ( + {errorMessage} + ) : ( + + )} ); @@ -48,3 +68,10 @@ const Title = styled.h2` line-height: 50px; } `; + +const ErrorMessage = styled.div` + color: #ff0000; + text-align: center; + font-size: 16px; + margin-top: 20px; +`; From 024b798a1a38561610cf391216f98942098daabe Mon Sep 17 00:00:00 2001 From: eunjin Date: Wed, 10 Jul 2024 18:04:27 +0900 Subject: [PATCH 5/8] =?UTF-8?q?fix=20:=20=EB=8D=B0=EC=9D=B4=ED=84=B0?= =?UTF-8?q?=EA=B0=80=20=EC=97=86=EC=9D=84=20=EB=95=8C=20=EB=B0=9C=EC=83=9D?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EC=98=A4=EB=A5=98=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/index.ts | 27 ++++++++++++++----- .../Home/GoodsRankingSection/index.tsx | 2 +- .../Home/ThemeCategorySection/index.tsx | 11 ++++---- .../features/Theme/ThemeHeroSection/index.tsx | 24 ++++++++++++----- src/pages/Theme/index.tsx | 17 ++++++------ 5 files changed, 55 insertions(+), 26 deletions(-) diff --git a/src/api/index.ts b/src/api/index.ts index e6234bec..2070dde5 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,15 +1,33 @@ +// src/api/index.ts import axios from 'axios'; -import type { GoodsData, RankingFilterOption,ThemeData } from '@/types'; +import type { GoodsData, RankingFilterOption, ThemeData } from '@/types'; const api = axios.create({ - baseURL: 'https://react-gift-mock-api-eunjin.vercel.app/',}); + baseURL: 'https://kakao-tech-campus-mock-server.vercel.app', +}); -export const getTheme = async (): Promise => { +export const fetchTheme = async (): Promise => { const response = await api.get<{ themes: ThemeData[] }>('/api/v1/themes'); return response.data.themes; }; +export const fetchThemes = async (themeKey: string): Promise => { + console.log('Fetching theme with key:', themeKey); // 콘솔 로그 추가 + const response = await api.get(`/api/v1/themes/${themeKey}`); + console.log('Fetched theme data:', response.data); + return response.data; +}; + +export const getThemeProducts = async (themeKey: string): Promise => { + const response = await api.get<{ products: GoodsData[] }>(`/api/v1/themes/${themeKey}/products`, { + params: { + maxResults: 20, + }, + }); + return response.data.products; +}; + export const getRankingGoods = async (filterOption: RankingFilterOption): Promise => { const response = await api.get<{ products: GoodsData[] }>('/api/v1/ranking/products', { params: filterOption, @@ -17,7 +35,4 @@ export const getRankingGoods = async (filterOption: RankingFilterOption): Promis return response.data.products; }; - - export default api; - diff --git a/src/components/features/Home/GoodsRankingSection/index.tsx b/src/components/features/Home/GoodsRankingSection/index.tsx index f4c61cfa..9be99bd3 100644 --- a/src/components/features/Home/GoodsRankingSection/index.tsx +++ b/src/components/features/Home/GoodsRankingSection/index.tsx @@ -19,12 +19,12 @@ export const GoodsRankingSection: React.FC = () => { useEffect(() => { const fetchGoods = async () => { + setErrorMessage(null); try { const data = await getRankingGoods(filterOption); setGoodsList(data); } catch (err) { setErrorMessage('데이터를 불러오는데 실패하였습니다.'); - } }; diff --git a/src/components/features/Home/ThemeCategorySection/index.tsx b/src/components/features/Home/ThemeCategorySection/index.tsx index 1cf3710e..e43389ed 100644 --- a/src/components/features/Home/ThemeCategorySection/index.tsx +++ b/src/components/features/Home/ThemeCategorySection/index.tsx @@ -2,7 +2,7 @@ import styled from '@emotion/styled'; import { useEffect, useState } from 'react'; import { Link } from 'react-router-dom'; -import { getTheme } from '@/api'; +import { fetchTheme } from '@/api'; import { Container } from '@/components/common/layouts/Container'; import { Grid } from '@/components/common/layouts/Grid'; import { getDynamicPath } from '@/routes/path'; @@ -15,18 +15,19 @@ export const ThemeCategorySection: React.FC = () => { const [themes, setThemes] = useState([]); useEffect(() => { - const fetchThemes = async () => { + const fetchThemesData = async () => { try { - const data = await getTheme(); + const data = await fetchTheme(); setThemes(data); } catch (error) { console.error('Failed to fetch themes:', error); } }; - fetchThemes(); + fetchThemesData(); }, []); - + + themes.map((theme)=> console.log(theme.key)); return ( diff --git a/src/components/features/Theme/ThemeHeroSection/index.tsx b/src/components/features/Theme/ThemeHeroSection/index.tsx index 36cfc038..ec9e4963 100644 --- a/src/components/features/Theme/ThemeHeroSection/index.tsx +++ b/src/components/features/Theme/ThemeHeroSection/index.tsx @@ -1,16 +1,32 @@ +// src/components/features/Theme/ThemeHeroSection.tsx import styled from '@emotion/styled'; +import { useEffect, useState } from 'react'; +import { fetchTheme } from '@/api'; import { Container } from '@/components/common/layouts/Container'; import { breakpoints } from '@/styles/variants'; import type { ThemeData } from '@/types'; -import { ThemeMockList } from '@/types/mock'; type Props = { themeKey: string; }; export const ThemeHeroSection = ({ themeKey }: Props) => { - const currentTheme = getCurrentTheme(themeKey, ThemeMockList); + const [currentTheme, setCurrentTheme] = useState(null); + + useEffect(() => { + const getThemeData = async () => { + try { + const themes = await fetchTheme(); + const foundTheme = themes.find((theme) => theme.key === themeKey); + setCurrentTheme(foundTheme || null); + } catch (error) { + console.error('Failed to fetch themes:', error); + } + }; + + getThemeData(); + }, [themeKey]); if (!currentTheme) { return null; @@ -82,7 +98,3 @@ const Description = styled.p` line-height: 32px; } `; - -export const getCurrentTheme = (themeKey: string, themeList: ThemeData[]) => { - return themeList.find((theme) => theme.key === themeKey); -}; diff --git a/src/pages/Theme/index.tsx b/src/pages/Theme/index.tsx index 4d02e6c1..40ec4d9a 100644 --- a/src/pages/Theme/index.tsx +++ b/src/pages/Theme/index.tsx @@ -1,17 +1,18 @@ -import { Navigate, useParams } from 'react-router-dom'; +import { useParams } from 'react-router-dom'; import { ThemeGoodsSection } from '@/components/features/Theme/ThemeGoodsSection'; -import { getCurrentTheme, ThemeHeroSection } from '@/components/features/Theme/ThemeHeroSection'; -import { RouterPath } from '@/routes/path'; -import { ThemeMockList } from '@/types/mock'; +import { ThemeHeroSection } from '@/components/features/Theme/ThemeHeroSection'; +// getCurrentTheme, +// import { RouterPath } from '@/routes/path'; +// import { ThemeMockList } from '@/types/mock'; export const ThemePage = () => { const { themeKey = '' } = useParams<{ themeKey: string }>(); - const currentTheme = getCurrentTheme(themeKey, ThemeMockList); + // const currentTheme = getCurrentTheme(themeKey, ThemeMockList); - if (!currentTheme) { - return ; - } + // if (!currentTheme) { + // return ; + // } return ( <> From 854d8658ca3e808e0cc71dca6c6fe55cb57803a4 Mon Sep 17 00:00:00 2001 From: eunjin Date: Wed, 10 Jul 2024 23:23:50 +0900 Subject: [PATCH 6/8] =?UTF-8?q?feat=20:=20themeGoodsection=20api=20?= =?UTF-8?q?=EC=97=B0=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Theme/ThemeGoodsSection/index.tsx | 28 +++++++++++++++++-- .../features/Theme/ThemeHeroSection/index.tsx | 8 +++++- src/pages/Theme/index.tsx | 10 +------ 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/components/features/Theme/ThemeGoodsSection/index.tsx b/src/components/features/Theme/ThemeGoodsSection/index.tsx index 8edbf70e..f72a87da 100644 --- a/src/components/features/Theme/ThemeGoodsSection/index.tsx +++ b/src/components/features/Theme/ThemeGoodsSection/index.tsx @@ -1,16 +1,38 @@ import styled from '@emotion/styled'; +import { useEffect, useState } from 'react'; +import { getThemeProducts } from '@/api'; import { DefaultGoodsItems } from '@/components/common/GoodsItem/Default'; import { Container } from '@/components/common/layouts/Container'; import { Grid } from '@/components/common/layouts/Grid'; import { breakpoints } from '@/styles/variants'; -import { GoodsMockList } from '@/types/mock'; +import type { GoodsData } from '@/types'; type Props = { themeKey: string; }; -export const ThemeGoodsSection = ({}: Props) => { +export const ThemeGoodsSection = ({ themeKey }: Props) => { + const [goods, setGoods] = useState([]); + const [error, setError] = useState(null); + + useEffect(() => { + const fetchGoods = async () => { + try { + const data = await getThemeProducts(themeKey); + setGoods(data); + } catch (err) { + setError('상품을 가져오는데 실패하였습니다.'); + } + }; + + fetchGoods(); + }, [themeKey]); + + if (error) { + return
{error}
; + } + return ( @@ -21,7 +43,7 @@ export const ThemeGoodsSection = ({}: Props) => { }} gap={16} > - {GoodsMockList.map(({ id, imageURL, name, price, brandInfo }) => ( + {goods.map(({ id, imageURL, name, price, brandInfo }) => ( { const [currentTheme, setCurrentTheme] = useState(null); + const [errorMessage, setErrorMessage] = useState(null); useEffect(() => { const getThemeData = async () => { @@ -21,13 +24,16 @@ export const ThemeHeroSection = ({ themeKey }: Props) => { const foundTheme = themes.find((theme) => theme.key === themeKey); setCurrentTheme(foundTheme || null); } catch (error) { - console.error('Failed to fetch themes:', error); + setErrorMessage('error'); } }; getThemeData(); }, [themeKey]); + if (errorMessage) { + return ; + } if (!currentTheme) { return null; } diff --git a/src/pages/Theme/index.tsx b/src/pages/Theme/index.tsx index 40ec4d9a..16a55a39 100644 --- a/src/pages/Theme/index.tsx +++ b/src/pages/Theme/index.tsx @@ -1,18 +1,10 @@ import { useParams } from 'react-router-dom'; import { ThemeGoodsSection } from '@/components/features/Theme/ThemeGoodsSection'; -import { ThemeHeroSection } from '@/components/features/Theme/ThemeHeroSection'; -// getCurrentTheme, -// import { RouterPath } from '@/routes/path'; -// import { ThemeMockList } from '@/types/mock'; +import {ThemeHeroSection } from '@/components/features/Theme/ThemeHeroSection'; export const ThemePage = () => { const { themeKey = '' } = useParams<{ themeKey: string }>(); - // const currentTheme = getCurrentTheme(themeKey, ThemeMockList); - - // if (!currentTheme) { - // return ; - // } return ( <> From 81c7ece894b25c8bc68e678178c1d7c134bdce93 Mon Sep 17 00:00:00 2001 From: eunjin Date: Thu, 11 Jul 2024 00:20:52 +0900 Subject: [PATCH 7/8] =?UTF-8?q?Feat=20:=20error=20=EB=B0=8F=20loading=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/index.ts | 4 +- .../Theme/ThemeGoodsSection/index.tsx | 51 ++++++++++++++++++- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/api/index.ts b/src/api/index.ts index 2070dde5..dc51caeb 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -4,7 +4,7 @@ import axios from 'axios'; import type { GoodsData, RankingFilterOption, ThemeData } from '@/types'; const api = axios.create({ - baseURL: 'https://kakao-tech-campus-mock-server.vercel.app', + baseURL: 'https://react-gift-mock-api-eunjin.vercel.app', }); export const fetchTheme = async (): Promise => { @@ -13,7 +13,7 @@ export const fetchTheme = async (): Promise => { }; export const fetchThemes = async (themeKey: string): Promise => { - console.log('Fetching theme with key:', themeKey); // 콘솔 로그 추가 + console.log('Fetching theme with key:', themeKey); const response = await api.get(`/api/v1/themes/${themeKey}`); console.log('Fetched theme data:', response.data); return response.data; diff --git a/src/components/features/Theme/ThemeGoodsSection/index.tsx b/src/components/features/Theme/ThemeGoodsSection/index.tsx index f72a87da..1a007938 100644 --- a/src/components/features/Theme/ThemeGoodsSection/index.tsx +++ b/src/components/features/Theme/ThemeGoodsSection/index.tsx @@ -15,24 +15,37 @@ type Props = { export const ThemeGoodsSection = ({ themeKey }: Props) => { const [goods, setGoods] = useState([]); const [error, setError] = useState(null); + const [isLoading, setisLoading] = useState(true); + useEffect(() => { const fetchGoods = async () => { try { + setisLoading(true); const data = await getThemeProducts(themeKey); setGoods(data); + setisLoading(false); } catch (err) { + setisLoading(true); setError('상품을 가져오는데 실패하였습니다.'); + setisLoading(false); } }; fetchGoods(); }, [themeKey]); - if (error) { - return
{error}
; + if (isLoading) { + return ( + + + + ); } + if (error) { + return {error}; + } return ( @@ -66,3 +79,37 @@ const Wrapper = styled.section` padding: 40px 16px 360px; } `; + + +const ErrorMessage = styled.div` + color: black; + text-align: center; + font-size: 16px; + margin-top: 20px; + display: flex; + justify-content: center; + align-items: center; + height: 100px; +`; + +const SpinnerWrapper = styled.div` + display: flex; + justify-content: center; + align-items: center; + height: 100px; +`; + +const Spinner = styled.div` + border: 4px solid white; + border-left-color: rgba(0,0,0,0.5); + border-radius: 50%; + width: 36px; + height: 36px; + animation: spin 1s linear infinite; + + @keyframes spin { + to { + transform: rotate(360deg); + } + } +`; From 724989910d9f65b424c4df46694303d0a4cbba98 Mon Sep 17 00:00:00 2001 From: eunjin Date: Sat, 13 Jul 2024 12:53:58 +0900 Subject: [PATCH 8/8] =?UTF-8?q?fix=20:=20Loading=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EB=B6=88=ED=95=84=EC=9A=94=ED=95=9C=20=EB=B6=80=EB=B6=84=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=20finally=EB=AC=B8=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/features/Theme/ThemeGoodsSection/index.tsx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/components/features/Theme/ThemeGoodsSection/index.tsx b/src/components/features/Theme/ThemeGoodsSection/index.tsx index 1a007938..51275db6 100644 --- a/src/components/features/Theme/ThemeGoodsSection/index.tsx +++ b/src/components/features/Theme/ThemeGoodsSection/index.tsx @@ -20,14 +20,13 @@ export const ThemeGoodsSection = ({ themeKey }: Props) => { useEffect(() => { const fetchGoods = async () => { + setisLoading(true); try { - setisLoading(true); const data = await getThemeProducts(themeKey); setGoods(data); - setisLoading(false); } catch (err) { - setisLoading(true); setError('상품을 가져오는데 실패하였습니다.'); + } finally { setisLoading(false); } };