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: 홈에서 카테고리 눌렀을 때 context에 저장하는 방식으로 변경 #732

Merged
merged 6 commits into from
Oct 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@ import type { Meta, StoryObj } from '@storybook/react';

import CategoryFoodList from './CategoryFoodList';

import CategoryProvider from '@/contexts/CategoryContext';

const meta: Meta<typeof CategoryFoodList> = {
title: 'common/CategoryFoodList',
component: CategoryFoodList,
decorators: [
(Story) => (
<CategoryProvider>
<Story />
</CategoryProvider>
),
],
};

export default meta;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,21 @@
import { Link } from '@fun-eat/design-system';
import { Link as RouterLink } from 'react-router-dom';
import styled from 'styled-components';

import CategoryItem from '../CategoryItem/CategoryItem';

import { CATEGORY_TYPE } from '@/constants';
import { useGA } from '@/hooks/common';
import { useCategoryFoodQuery } from '@/hooks/queries/product';

const category = CATEGORY_TYPE.FOOD;
const categoryType = CATEGORY_TYPE.FOOD;

const CategoryFoodList = () => {
const { data: categories } = useCategoryFoodQuery(category);
const { gaEvent } = useGA();

const handleHomeCategoryLinkClick = (categoryName: string) => {
gaEvent({
category: 'link',
action: `${categoryName} 카테고리 링크 클릭`,
label: '카테고리',
});
};
const { data: categories } = useCategoryFoodQuery(categoryType);

return (
<div>
<CategoryFoodListWrapper>
{categories.map((menu) => (
<Link
key={menu.id}
as={RouterLink}
to={`products/food?category=${menu.id}`}
onClick={() => handleHomeCategoryLinkClick(menu.name)}
>
<CategoryItem name={menu.name} image={menu.image} />
</Link>
))}
</CategoryFoodListWrapper>
</div>
<CategoryFoodListWrapper>
{categories.map(({ id, name, image }) => (
<CategoryItem key={id} categoryId={id} name={name} image={image} categoryType={categoryType} />
))}
</CategoryFoodListWrapper>
);
};

Expand Down
36 changes: 12 additions & 24 deletions frontend/src/components/Common/CategoryFoodTab/CategoryFoodTab.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { Button, theme } from '@fun-eat/design-system';
import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';
import styled from 'styled-components';

import { CATEGORY_TYPE } from '@/constants';
Expand All @@ -9,29 +7,20 @@ import { useCategoryActionContext, useCategoryValueContext } from '@/hooks/conte
import { useCategoryFoodQuery } from '@/hooks/queries/product/useCategoryQuery';
import { getTargetCategoryName } from '@/utils/category';

const category = CATEGORY_TYPE.FOOD;
const categoryType = CATEGORY_TYPE.FOOD;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


const CategoryFoodTab = () => {
const { data: categories } = useCategoryFoodQuery(category);
const { data: categories } = useCategoryFoodQuery(categoryType);

const { categoryIds } = useCategoryValueContext();
const { selectCategory } = useCategoryActionContext();
const currentCategoryId = categoryIds[category];

const location = useLocation();
const queryParams = new URLSearchParams(location.search);
const categoryIdFromURL = queryParams.get('category');
const currentCategoryId = categoryIds[categoryType];

const { gaEvent } = useGA();

useEffect(() => {
if (categoryIdFromURL) {
selectCategory(category, parseInt(categoryIdFromURL));
}
}, [category]);

const handleCategoryButtonClick = (menuId: number) => {
selectCategory(category, menuId);
selectCategory(categoryType, menuId);
gaEvent({
category: 'button',
action: `${getTargetCategoryName(categories, menuId)} 카테고리 버튼 클릭`,
Expand All @@ -41,10 +30,10 @@ const CategoryFoodTab = () => {

return (
<CategoryMenuContainer>
{categories.map((menu) => {
const isSelected = menu.id === currentCategoryId;
{categories.map(({ id, name }) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

const isSelected = id === currentCategoryId;
return (
<li key={menu.id}>
<li key={id}>
<CategoryButton
type="button"
customHeight="30px"
Expand All @@ -53,10 +42,10 @@ const CategoryFoodTab = () => {
weight="bold"
variant={isSelected ? 'filled' : 'outlined'}
isSelected={isSelected}
onClick={() => handleCategoryButtonClick(menu.id)}
onClick={() => handleCategoryButtonClick(id)}
aria-pressed={isSelected}
>
{menu.name}
{name}
</CategoryButton>
</li>
);
Expand All @@ -81,10 +70,9 @@ const CategoryMenuContainer = styled.ul`
const CategoryButton = styled(Button)<{ isSelected: boolean }>`
padding: 6px 12px;
${({ isSelected }) =>
isSelected
? `
isSelected &&
`
background: ${theme.colors.gray5};
color: ${theme.textColors.white};
`
: ''}
`}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@ import type { Meta, StoryObj } from '@storybook/react';

import CategoryItem from './CategoryItem';

import CategoryProvider from '@/contexts/CategoryContext';

const meta: Meta<typeof CategoryItem> = {
title: 'common/CategoryItem',
component: CategoryItem,
decorators: [
(Story) => (
<CategoryProvider>
<Story />
</CategoryProvider>
),
],
args: {
categoryId: 1,
name: '즉석 식품',
image: 'https://tqklhszfkvzk6518638.cdn.ntruss.com/product/8801771029052.jpg',
categoryType: 'food',
},
};

Expand Down
35 changes: 26 additions & 9 deletions frontend/src/components/Common/CategoryItem/CategoryItem.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
import { Button } from '@fun-eat/design-system';
import { useNavigate } from 'react-router-dom';
import styled from 'styled-components';

import { PATH } from '@/constants/path';
import { useGA } from '@/hooks/common';
import { useCategoryActionContext } from '@/hooks/context';

interface CategoryItemProps {
categoryId: number;
name: string;
image: string;
categoryType: 'food' | 'store';
}

const CategoryItem = ({ name, image }: CategoryItemProps) => {
const CategoryItem = ({ categoryId, name, image, categoryType }: CategoryItemProps) => {
const navigate = useNavigate();
const { selectCategory } = useCategoryActionContext();

const { gaEvent } = useGA();

const handleCategoryItemClick = (categoryId: number) => {
selectCategory(categoryType, categoryId);
navigate(PATH.PRODUCT_LIST + '/' + categoryType);

gaEvent({
category: 'button',
action: `${name} 카테고리 링크 클릭`,
label: '카테고리',
});
};

return (
<CategoryItemContainer variant="transparent">
<Button type="button" variant="transparent" customHeight="auto" onClick={() => handleCategoryItemClick(categoryId)}>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

<ImageWrapper>
<img src={image} width={60} height={60} alt={name} />
</ImageWrapper>
<CategoryName>{name}</CategoryName>
</CategoryItemContainer>
</Button>
);
};

export default CategoryItem;

const CategoryItemContainer = styled(Button)`
width: 60px;
height: 100px;
text-align: center;
`;

const ImageWrapper = styled.div`
display: flex;
justify-content: center;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@ import type { Meta, StoryObj } from '@storybook/react';

import CategoryStoreList from './CategoryStoreList';

import CategoryProvider from '@/contexts/CategoryContext';

const meta: Meta<typeof CategoryStoreList> = {
title: 'common/CategoryStoreList',
component: CategoryStoreList,
decorators: [
(Story) => (
<CategoryProvider>
<Story />
</CategoryProvider>
),
],
};

export default meta;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,21 @@
import { Link } from '@fun-eat/design-system';
import { Link as RouterLink } from 'react-router-dom';
import styled from 'styled-components';

import CategoryItem from '../CategoryItem/CategoryItem';

import { CATEGORY_TYPE } from '@/constants';
import { useGA } from '@/hooks/common';
import { useCategoryStoreQuery } from '@/hooks/queries/product';

const category = CATEGORY_TYPE.STORE;
const categoryType = CATEGORY_TYPE.STORE;

const CategoryStoreList = () => {
const { data: categories } = useCategoryStoreQuery(category);
const { gaEvent } = useGA();

const handleHomeCategoryLinkClick = (categoryName: string) => {
gaEvent({
category: 'link',
action: `${categoryName} 카테고리 링크 클릭`,
label: '카테고리',
});
};
const { data: categories } = useCategoryStoreQuery(categoryType);

return (
<div>
<CategoryStoreListWrapper>
{categories.map((menu) => (
<Link
key={menu.id}
as={RouterLink}
to={`products/store?category=${menu.id}`}
onClick={() => handleHomeCategoryLinkClick(menu.name)}
>
<CategoryItem name={menu.name} image={menu.image} />
</Link>
))}
</CategoryStoreListWrapper>
</div>
<CategoryStoreListWrapper>
{categories.map(({ id, name, image }) => (
<CategoryItem key={id} categoryId={id} name={name} image={image} categoryType={categoryType} />
))}
</CategoryStoreListWrapper>
);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { Button, theme } from '@fun-eat/design-system';
import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';
import styled from 'styled-components';

import { CATEGORY_TYPE } from '@/constants';
Expand All @@ -9,29 +7,19 @@ import { useCategoryActionContext, useCategoryValueContext } from '@/hooks/conte
import { useCategoryStoreQuery } from '@/hooks/queries/product/useCategoryQuery';
import { getTargetCategoryName } from '@/utils/category';

const category = CATEGORY_TYPE.STORE;
const categoryType = CATEGORY_TYPE.STORE;

const CategoryStoreTab = () => {
const { data: categories } = useCategoryStoreQuery(category);
const { data: categories } = useCategoryStoreQuery(categoryType);

const { categoryIds } = useCategoryValueContext();
const { selectCategory } = useCategoryActionContext();
const currentCategoryId = categoryIds[category];

const location = useLocation();
const queryParams = new URLSearchParams(location.search);
const categoryIdFromURL = queryParams.get('category');
const currentCategoryId = categoryIds[categoryType];

const { gaEvent } = useGA();

useEffect(() => {
if (categoryIdFromURL) {
selectCategory(category, parseInt(categoryIdFromURL));
}
}, [category]);

const handleCategoryButtonClick = (menuId: number) => {
selectCategory(category, menuId);
selectCategory(categoryType, menuId);
gaEvent({
category: 'button',
action: `${getTargetCategoryName(categories, menuId)} 카테고리 버튼 클릭`,
Expand All @@ -41,10 +29,10 @@ const CategoryStoreTab = () => {

return (
<CategoryMenuContainer>
{categories.map((menu) => {
const isSelected = menu.id === currentCategoryId;
{categories.map(({ id, name }) => {
const isSelected = id === currentCategoryId;
return (
<li key={menu.id}>
<li key={id}>
<CategoryButton
type="button"
customHeight="30px"
Expand All @@ -53,10 +41,10 @@ const CategoryStoreTab = () => {
weight="bold"
variant={isSelected ? 'filled' : 'outlined'}
isSelected={isSelected}
onClick={() => handleCategoryButtonClick(menu.id)}
onClick={() => handleCategoryButtonClick(id)}
aria-pressed={isSelected}
>
{menu.name}
{name}
</CategoryButton>
</li>
);
Expand All @@ -81,10 +69,9 @@ const CategoryMenuContainer = styled.ul`
const CategoryButton = styled(Button)<{ isSelected: boolean }>`
padding: 6px 12px;
${({ isSelected }) =>
isSelected
? `
isSelected &&
`
background: ${theme.colors.primary};
color: ${theme.textColors.default};
`
: ''}
`}
`;
Loading