Skip to content

Commit

Permalink
refactor: add useProductData hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
kang-kibong committed Jul 19, 2024
1 parent b100d8e commit 5654331
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
1 change: 0 additions & 1 deletion src/components/features/Product/ProductInfo/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import styled from '@emotion/styled';
import React from 'react';
import { Image } from '@chakra-ui/react';

import { Container } from '@components/common';

const IMAGE_SIZE = 450;
Expand Down
2 changes: 0 additions & 2 deletions src/components/features/Product/ProductOrder/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export default function ProductOrder({ name, giftOrderLimit }: ProductOrderProps
const { isAuthenticated } = useAuth();
const navigate = useNavigate();

console.info(giftOrderLimit);

const handleOrderClick = () => {
const targetPath = isAuthenticated ? ROUTE_PATH.ORDER : ROUTE_PATH.LOGIN;
navigate(targetPath);
Expand Down
26 changes: 26 additions & 0 deletions src/pages/Product/hooks/useProductData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useQuery } from '@tanstack/react-query';
import { useParams } from 'react-router-dom';
import { getProductsDetail, getProductsOptions } from '@apis/products';
import { ProductDetailResponse, ProductOptionResponse } from '@internalTypes/responseTypes';
import { AxiosError } from 'axios';

interface UseProductDataResult {
productDetailData?: ProductDetailResponse;
productOptionData?: ProductOptionResponse;
}

export default function useProductData(): UseProductDataResult {
const { productId } = useParams<{ productId: string }>();

const { data: productDetailData } = useQuery<ProductDetailResponse, AxiosError>({
queryKey: ['productDetail', productId],
queryFn: () => getProductsDetail({ productId }),
});

const { data: productOptionData } = useQuery<ProductOptionResponse, AxiosError>({
queryKey: ['productOption', productId],
queryFn: () => getProductsOptions({ productId }),
});

return { productDetailData, productOptionData };
}
17 changes: 2 additions & 15 deletions src/pages/Product/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,12 @@ import ProductInfo from '@components/features/Product/ProductInfo';
import ProductOrder from '@components/features/Product/ProductOrder';
import { ROUTE_PATH } from '@routes/path';
import useRedirectIfNoParam from '@hooks/useRedirectIfNoParam';
import { useQuery } from '@tanstack/react-query';
import { useParams } from 'react-router-dom';
import { getProductsDetail, getProductsOptions } from '@apis/products';
import { ProductDetailResponse, ProductOptionResponse } from '@internalTypes/responseTypes';
import { AxiosError } from 'axios';
import useProductData from './hooks/useProductData';

export default function Product() {
const { productId } = useParams<{ productId: string }>();
const { data: productDetailData } = useQuery<ProductDetailResponse, AxiosError>({
queryKey: ['productDetail', productId],
queryFn: () => getProductsDetail({ productId }),
});
const { productDetailData, productOptionData } = useProductData();
useRedirectIfNoParam('productId', ROUTE_PATH.HOME);

const { data: productOptionData } = useQuery<ProductOptionResponse, AxiosError>({
queryKey: ['productOption', productId],
queryFn: () => getProductsOptions({ productId }),
});

return (
<Layout>
<CenteredContainer maxWidth="lg">
Expand Down

0 comments on commit 5654331

Please sign in to comment.