Skip to content

Commit

Permalink
refactor: CategoryContext를 Action과 Value Context로 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
xodms0309 committed Sep 14, 2023
1 parent 40954dc commit a80dfed
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 32 deletions.
5 changes: 3 additions & 2 deletions frontend/src/components/Common/CategoryMenu/CategoryMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Button, theme } from '@fun-eat/design-system';
import type { CSSProp } from 'styled-components';
import styled from 'styled-components';

import { useCategoryContext } from '@/hooks/context';
import { useCategoryValueContext, useCategoryActionContext } from '@/hooks/context';
import { useCategoryQuery } from '@/hooks/queries/product';
import type { CategoryVariant } from '@/types/common';

Expand All @@ -12,7 +12,8 @@ interface CategoryMenuProps {

const CategoryMenu = ({ menuVariant }: CategoryMenuProps) => {
const { data: categories } = useCategoryQuery(menuVariant);
const { categoryIds, selectCategory } = useCategoryContext();
const { categoryIds } = useCategoryValueContext();
const { selectCategory } = useCategoryActionContext();
const currentCategoryId = categoryIds[menuVariant];

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import PBProductItem from '../PBProductItem/PBProductItem';

import { MoreButton } from '@/components/Common';
import { PATH } from '@/constants/path';
import { useCategoryContext } from '@/hooks/context';
import { useCategoryValueContext } from '@/hooks/context';
import { useInfiniteProductsQuery } from '@/hooks/queries/product';
import displaySlice from '@/utils/displaySlice';

Expand All @@ -15,7 +15,7 @@ interface PBProductListProps {
}

const PBProductList = ({ isHomePage }: PBProductListProps) => {
const { categoryIds } = useCategoryContext();
const { categoryIds } = useCategoryValueContext();

const { data: pbProductListResponse } = useInfiniteProductsQuery(categoryIds.store);
const pbProducts = pbProductListResponse.pages.flatMap((page) => page.products);
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/Product/ProductList/ProductList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ProductItem from '../ProductItem/ProductItem';

import { PATH } from '@/constants/path';
import { useIntersectionObserver, useScrollRestoration } from '@/hooks/common';
import { useCategoryContext } from '@/hooks/context';
import { useCategoryValueContext } from '@/hooks/context';
import { useInfiniteProductsQuery } from '@/hooks/queries/product';
import type { CategoryVariant, SortOption } from '@/types/common';
import displaySlice from '@/utils/displaySlice';
Expand All @@ -21,7 +21,7 @@ interface ProductListProps {
const ProductList = ({ category, isHomePage, selectedOption }: ProductListProps) => {
const scrollRef = useRef<HTMLDivElement>(null);

const { categoryIds } = useCategoryContext();
const { categoryIds } = useCategoryValueContext();

const { fetchNextPage, hasNextPage, data } = useInfiniteProductsQuery(
categoryIds[category],
Expand Down
26 changes: 16 additions & 10 deletions frontend/src/contexts/CategoryContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@ type CategoryIds = {
[k in CategoryVariant]: number;
};

interface CategoryState {
interface CategoryValue {
categoryIds: CategoryIds;
selectCategory: (menuVariant: string, categoryId: number) => void;
currentTabScroll: { [key: number]: number };
}

interface CategoryAction {
selectCategory: (menuVariant: string, categoryId: number) => void;
saveCurrentTabScroll: (categoryId: number, scrollY: number) => void;
}

export const CategoryContext = createContext<CategoryState>({
categoryIds: initialState,
selectCategory: () => {},
currentTabScroll: {},
saveCurrentTabScroll: () => {},
});
export const CategoryValueContext = createContext<CategoryValue | null>(null);
export const CategoryActionContext = createContext<CategoryAction | null>(null);

const CategoryProvider = ({ children }: PropsWithChildren) => {
const [categoryIds, setCategoryIds] = useState(initialState);
Expand All @@ -38,14 +37,21 @@ const CategoryProvider = ({ children }: PropsWithChildren) => {
setCurrentTabScroll((prevState) => ({ ...prevState, [categoryId]: scrollY }));
};

const categoryState: CategoryState = {
const categoryValue = {
categoryIds,
currentTabScroll,
};

const categoryAction = {
selectCategory,
saveCurrentTabScroll,
};

return <CategoryContext.Provider value={categoryState}>{children}</CategoryContext.Provider>;
return (
<CategoryActionContext.Provider value={categoryAction}>
<CategoryValueContext.Provider value={categoryValue}>{children}</CategoryValueContext.Provider>
</CategoryActionContext.Provider>
);
};

export default CategoryProvider;
4 changes: 2 additions & 2 deletions frontend/src/hooks/common/useScrollRestoration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import type { RefObject } from 'react';
import { useEffect } from 'react';

import useTimeout from './useTimeout';
import { useCategoryContext } from '../context';
import { useCategoryActionContext } from '../context';

const useScrollRestoration = (currentCategoryId: number, ref: RefObject<HTMLElement>) => {
const { saveCurrentTabScroll } = useCategoryContext();
const { saveCurrentTabScroll } = useCategoryActionContext();

const handleScroll = () => {
if (!ref.current) return;
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/hooks/context/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { default as useCategoryContext } from './useCategoryContext';
export { default as useCategoryValueContext } from './useCategoryValueContext';
export { default as useCategoryActionContext } from './useCategoryActionContext';
export { default as useReviewFormActionContext } from './useReviewFormActionContext';
export { default as useReviewFormValueContext } from './useReviewFormValueContext';
export { default as useRecipeFormActionContext } from './useRecipeFormActionContext';
Expand Down
14 changes: 14 additions & 0 deletions frontend/src/hooks/context/useCategoryActionContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useContext } from 'react';

import { CategoryActionContext } from '@/contexts/CategoryContext';

const useCategoryActionContext = () => {
const categoryAction = useContext(CategoryActionContext);
if (categoryAction === null || categoryAction === undefined) {
throw new Error('useCategoryActionContext는 Category Provider 안에서 사용해야 합니다.');
}

return categoryAction;
};

export default useCategoryActionContext;
11 changes: 0 additions & 11 deletions frontend/src/hooks/context/useCategoryContext.ts

This file was deleted.

14 changes: 14 additions & 0 deletions frontend/src/hooks/context/useCategoryValueContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useContext } from 'react';

import { CategoryValueContext } from '@/contexts/CategoryContext';

const useCategoryValueContext = () => {
const categoryValue = useContext(CategoryValueContext);
if (categoryValue === null || categoryValue === undefined) {
throw new Error('useCategoryValueContext는 Category Provider 안에서 사용해야 합니다.');
}

return categoryValue;
};

export default useCategoryValueContext;
4 changes: 2 additions & 2 deletions frontend/src/pages/ProductListPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { ProductList } from '@/components/Product';
import { PRODUCT_SORT_OPTIONS } from '@/constants';
import { PATH } from '@/constants/path';
import { useScrollRestoration, useSortOption } from '@/hooks/common';
import { useCategoryContext } from '@/hooks/context';
import { useCategoryValueContext } from '@/hooks/context';
import { isCategoryVariant } from '@/types/common';

const PAGE_TITLE = { food: '공통 상품', store: 'PB 상품' };
Expand All @@ -34,7 +34,7 @@ const ProductListPage = () => {
const { selectedOption, selectSortOption } = useSortOption(PRODUCT_SORT_OPTIONS[0]);
const { reset } = useQueryErrorResetBoundary();

const { categoryIds, currentTabScroll } = useCategoryContext();
const { categoryIds, currentTabScroll } = useCategoryValueContext();
const currentCategoryId = categoryIds[category];

const productListRef = useRef<HTMLDivElement>(null);
Expand Down

0 comments on commit a80dfed

Please sign in to comment.