Skip to content
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] feat: 배너 조회 API 연결 및 배너 컴포넌트 작성 #790

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions frontend/src/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export const recipeApi = new ApiClient('/recipes');
export const searchApi = new ApiClient('/search');
export const logoutApi = new ApiClient('/logout');
export const reviewApi = new ApiClient('/reviews');
export const bannerApi = new ApiClient('/banners');
26 changes: 26 additions & 0 deletions frontend/src/components/Common/Banner/Banner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Link } from '@fun-eat/design-system';
import styled from 'styled-components';

import { useBannerQuery } from '@/hooks/queries/banner';

const Banner = () => {
const { data: banners } = useBannerQuery();
const { link, image } = banners[Math.floor(Math.random() * banners.length)];

if (!link) {
return <BannerImage src={image} width={600} height={360} alt="배너" />;
}

return (
<Link href={link} isExternal>
<BannerImage src={image} width={600} height={360} alt="배너" />
</Link>
);
};

export default Banner;

const BannerImage = styled.img`
width: 100%;
height: auto;
`;
1 change: 1 addition & 0 deletions frontend/src/components/Common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export { default as CategoryItem } from './CategoryItem/CategoryItem';
export { default as CategoryFoodList } from './CategoryFoodList/CategoryFoodList';
export { default as CategoryStoreList } from './CategoryStoreList/CategoryStoreList';
export { default as Skeleton } from './Skeleton/Skeleton';
export { default as Banner } from './Banner/Banner';
1 change: 1 addition & 0 deletions frontend/src/hooks/queries/banner/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as useBannerQuery } from './useBannerQuery';
16 changes: 16 additions & 0 deletions frontend/src/hooks/queries/banner/useBannerQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useSuspendedQuery } from '../useSuspendedQuery';

import { bannerApi } from '@/apis';
import type { Banner } from '@/types/banner';

const fetchBanner = async () => {
const response = await bannerApi.get({});
const data: Banner[] = await response.json();
return data;
};

const useBannerQuery = () => {
return useSuspendedQuery(['banner'], () => fetchBanner());
};

export default useBannerQuery;
4 changes: 3 additions & 1 deletion frontend/src/mocks/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
recipeHandlers,
searchHandlers,
logoutHandlers,
bannerHandlers,
} from './handlers';

export const worker = setupWorker(
Expand All @@ -19,5 +20,6 @@ export const worker = setupWorker(
...memberHandlers,
...recipeHandlers,
...searchHandlers,
...logoutHandlers
...logoutHandlers,
...bannerHandlers
);
17 changes: 17 additions & 0 deletions frontend/src/mocks/data/banners.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"id": 3,
"link": "https://www.youtube.com/embed/3QlYJ0VY7zg",
"image": "https://i.ytimg.com/vi/3QlYJ0VY7zg/maxresdefault.jpg"
},
{
"id": 2,
"link": "https://www.youtube.com/embed/3QlYJ0VY7zg",
"image": "https://i.ytimg.com/vi/3QlYJ0VY7zg/maxresdefault.jpg"
},
{
"id": 1,
"link": "https://www.youtube.com/embed/3QlYJ0VY7zg",
"image": "https://i.ytimg.com/vi/3QlYJ0VY7zg/maxresdefault.jpg"
}
]
9 changes: 9 additions & 0 deletions frontend/src/mocks/handlers/bannerHandlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { rest } from 'msw';

import banners from '../data/banners.json';

export const bannerHandlers = [
rest.get('/api/banners', (req, res, ctx) => {
return res(ctx.status(200), ctx.json(banners));
}),
];
1 change: 1 addition & 0 deletions frontend/src/mocks/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './memberHandlers';
export * from './recipeHandlers';
export * from './searchHandlers';
export * from './logoutHandlers';
export * from './bannerHandlers';
9 changes: 2 additions & 7 deletions frontend/src/pages/HomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {
CategoryFoodList,
CategoryStoreList,
SvgIcon,
Banner,
} from '@/components/Common';
import { ProductRankingList, ReviewRankingList, RecipeRankingList } from '@/components/Rank';
import { IMAGE_URL } from '@/constants';
import channelTalk from '@/service/channelTalk';

export const HomePage = () => {
Expand All @@ -28,7 +28,7 @@ export const HomePage = () => {
return (
<>
<section>
<Banner src={`${IMAGE_URL}banner.png`} width={600} height={360} alt="이벤트 배너" />
<Banner />
</section>
<Spacing size={40} />
<SectionWrapper>
Expand Down Expand Up @@ -100,11 +100,6 @@ export const HomePage = () => {
);
};

const Banner = styled.img`
width: 100%;
height: auto;
`;

const SectionWrapper = styled.section`
padding: 0 20px;
`;
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/types/banner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Banner {
id: number;
link: string;
image: string;
}
Loading