-
Notifications
You must be signed in to change notification settings - Fork 47
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_정서현 4주차 Step2 #79
base: hyunaeri
Are you sure you want to change the base?
Changes from all commits
556d5f5
5c66012
2964853
1398355
b69e853
be1b5f2
fd9d11e
5e10f9e
91c3ca9
d50f908
d005bd3
2e2b5b5
0e26fb9
5d60c32
1b4cbae
9891534
2583991
4aba548
1e8b382
8079bcb
86243b0
87f8952
51549b9
0ed7b91
ed8050a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,30 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
|
||
import type { ProductsInfoData } from '@/types' | ||
import type { ProductsDetailData, ProductsOptionData } from '@/types' | ||
|
||
import { fetchInstance } from '../instance' | ||
|
||
const getProductsPath = (product: string) => `/v1/products/${product}/detail` | ||
const getProductsDetailPath = (product: string) => `/v1/products/${product}/detail` | ||
const getProductsOptionPath = (product: string) => `/v1/products/${product}/options` | ||
|
||
export const getProducts = async (product: string) => { | ||
const response = await fetchInstance.get<ProductsInfoData>(getProductsPath(product)) | ||
export const getProductsDetail = async (product: string) => { | ||
const response = await fetchInstance.get<ProductsDetailData>(getProductsDetailPath(product)) | ||
return response.data.detail | ||
} | ||
|
||
export const getProductsOptions = async (product: string) => { | ||
const response = await fetchInstance.get<ProductsOptionData>(getProductsOptionPath(product)) | ||
return response.data.options | ||
} | ||
|
||
export const useGetProductsInfo = (product: string) => | ||
useQuery({ | ||
queryKey: [getProductsPath(product)], | ||
queryFn: () => getProducts(product) | ||
queryKey: [getProductsDetailPath(product), getProductsOptionPath(product)], | ||
queryFn: async () => { | ||
const [detail, options] = await Promise.all([ | ||
getProductsDetail(product), | ||
getProductsOptions(product), | ||
]) | ||
return { detail, options } | ||
} | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,10 +21,36 @@ export const ProductsDetail = ({ productId }: Props) => { | |
const authInfo = useAuth() | ||
const navigate = useNavigate() | ||
|
||
// 선물 최대 제한 수량 | ||
const giftOrderLimit = data?.options?.giftOrderLimit ?? 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0으로 방어를 하셨군요...? 그래도 괜찮나요?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 어 .. 임의로 넣은 값이긴 합니다 0으로 undefined 를 방어하면 선물하기가 안되긴 하겠네요.. ㅠㅠ |
||
|
||
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const value = Number(event.target.value) | ||
if (isNaN(value)) return | ||
setItemCount(value) | ||
if (isNaN(value)) { | ||
return | ||
} | ||
|
||
if (value > giftOrderLimit) { | ||
alert(`최대 선물 가능 수량은 ${giftOrderLimit}개 입니다.`) | ||
setItemCount(giftOrderLimit) | ||
} | ||
else { | ||
setItemCount(value) | ||
} | ||
Comment on lines
+33
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. else로 연결하는 방식을 많이 사용하시는 것 같아요. 대신 얼리 리턴에 익숙해지는 것이 좋을 것 같습니다 |
||
} | ||
|
||
const handleIncrement = () => { | ||
if (itemCount < giftOrderLimit) { | ||
setItemCount(itemCount + 1) | ||
} else { | ||
alert(`최대 선물 가능 수량은 ${giftOrderLimit}개 입니다.`) | ||
} | ||
} | ||
|
||
const handleDecrement = () => { | ||
if (itemCount > 1) { | ||
setItemCount(itemCount - 1) | ||
} | ||
} | ||
|
||
const handleNavigate = () => { | ||
|
@@ -39,10 +65,11 @@ export const ProductsDetail = ({ productId }: Props) => { | |
} | ||
|
||
useEffect(() => { | ||
if (data) { | ||
setTotalPrice(data.price.basicPrice * itemCount) | ||
if (data) | ||
setTotalPrice(data.detail.price.basicPrice * itemCount) | ||
console.log(`상품명: ${data.detail.name} / 선물 최대 제한 수량: ${giftOrderLimit}`) | ||
} | ||
}, [data, itemCount]) | ||
}, [data, itemCount, giftOrderLimit]) | ||
|
||
if (isLoading) { | ||
return ( | ||
|
@@ -68,30 +95,31 @@ export const ProductsDetail = ({ productId }: Props) => { | |
p={4} | ||
> | ||
<GridItem> | ||
<Image src={data?.imageURL} alt={data?.name} /> | ||
<Image src={data?.detail?.imageURL} alt={data?.detail?.name} /> | ||
</GridItem> | ||
|
||
<GridItem> | ||
<Box> | ||
<Text fontSize="2xl" fontWeight="bold">{data?.name}</Text> | ||
<Text fontSize="xl" color="gray.600">{data?.price.basicPrice} 원</Text> | ||
<Text fontSize="2xl" fontWeight="bold">{data?.detail?.name}</Text> | ||
<Text fontSize="xl" color="gray.600">{data?.detail?.price.basicPrice} 원</Text> | ||
<Text mt={4} fontSize="md" color="gray.500"> | ||
카톡 친구가 아니어도 선물 코드로 선물 할 수 있어요! | ||
</Text> | ||
</Box> | ||
|
||
<Box mt={6}> | ||
<QuantityControl> | ||
<Button size="sm" onClick={() => setItemCount(itemCount - 1)}>-</Button> | ||
<Button size="sm" onClick={handleDecrement}>-</Button> | ||
<Input | ||
type="number" | ||
width="60px" | ||
textAlign="center" | ||
value={itemCount} | ||
onChange={handleChange} | ||
mx={2} | ||
min={1} | ||
/> | ||
<Button size="sm" onClick={() => setItemCount(itemCount + 1)}>+</Button> | ||
<Button size="sm" onClick={handleIncrement}>+</Button> | ||
</QuantityControl> | ||
</Box> | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여러 api를 호출해야 한다면 useQueries를 사용해보면 좋을 것 같아요!
https://tanstack.com/query/latest/docs/framework/react/reference/useQueries