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

Feat/355 #362

Merged
merged 3 commits into from
Jun 13, 2024
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
38 changes: 33 additions & 5 deletions src/components/feature/ProductItem/CartButton/index.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
import { MouseEvent } from 'react';

import Spinner from 'components/ui/Spinner';

import { useAxios } from 'hooks/useAxios';
import { useLogin } from 'hooks/useLogin';

import styles from './index.module.scss';

const CartButton = () => {
// TODO : API 요청
type CartButtonProps = {
productId: number;
};

const CartButton = ({ productId }: CartButtonProps) => {
const { isLoggedIn, login, confirmLogin } = useLogin();
const { isLoading, sendRequest } = useAxios<{ cartId: number }>({
url: '/cart',
method: 'post',
data: {
productId,
itemCount: 1,
optionId: null,
optionDetailId: null,
},
});

const handleAddCart = (e: MouseEvent<HTMLButtonElement>) => {
e.preventDefault();
if (isLoggedIn) sendRequest();
else {
const result = confirmLogin();
if (result) login();
}
};

return (
<button type="button" className={styles.btn_cart} onClick={handleAddCart}>
<span className={styles.ico_cart}>선물상자 담기</span>
</button>
<>
{isLoading && <Spinner />}
<button type="button" className={styles.btn_cart} onClick={handleAddCart}>
<span className={styles.ico_cart}>선물상자 담기</span>
</button>
</>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const ColumnProductItem = ({ product, size }: ColumnProductItemProps) => {
</div>
</Link>
<div className={styles.wrapper_util_info}>
<CartButton />
<CartButton productId={product.productId} />
<WishButton
productId={product.productId}
isWished={product.wished}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const FriendWishItem = ({ product }: { product: ProductItem }) => {
<span className={styles.txt_unit}>원</span>
</p>
<div className={styles.wrapper_btn}>
<CartButton />
<CartButton productId={product.productId} />
<WishButton
productId={product.productId}
isWished={product.wished}
Expand Down
30 changes: 21 additions & 9 deletions src/layouts/Product/BuyInfo/ButtonBundles/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { useSelectedFriendsStore } from 'store/useSelectedFriendsStore';

import { useAxios } from 'hooks/useAxios';
import { useKakaoPicker } from 'hooks/useKakaoPicker';
import { useLogin } from 'hooks/useLogin';
import { useModal } from 'hooks/useModal';
Expand Down Expand Up @@ -57,6 +58,20 @@
scrollPos: scrollWishPos,
} = useModal();

// 장바구니 등록 API
const { sendRequest: addItemToCart } = useAxios<{
cartId: number;
}>({
url: '/cart',
method: 'post',
data: {
productId,
itemCount: quantity,
optionId: selectedOption ? options[0].optionsId : null,
optionDetailId: selectedOption ? selectedOption.id : null,
},
});

// 주문 정보
const orderInfos: RequestOrderPreview = [
{
Expand Down Expand Up @@ -85,7 +100,7 @@
// 옵션 선택 여부 확인
const checkOptionBeforeAction = (action: () => void) => {
if (hasOption && !selectedOption) {
alert('옵션을 선택해주세요');

Check warning on line 103 in src/layouts/Product/BuyInfo/ButtonBundles/index.tsx

View workflow job for this annotation

GitHub Actions / build

Unexpected alert
return;
}
action();
Expand Down Expand Up @@ -125,7 +140,7 @@
} else if (selectedFriends.length === 1) {
navigate('/bill/gift', { state: { orderInfos, giftFor: 'friends' } });
} else {
alert('지금은 한 번에 한 명에게만 선물할 수 있어요.');

Check warning on line 143 in src/layouts/Product/BuyInfo/ButtonBundles/index.tsx

View workflow job for this annotation

GitHub Actions / build

Unexpected alert
}
});
};
Expand All @@ -136,9 +151,11 @@
openKakaoPicker();
};

// 선물상자 담기 버튼 핸들러
const handleClickCart = () => {
// console.log('선물상자 담기');
// 장바구니 등록 버튼 핸들러
const handleAddCart = () => {
checkLoginBeforeAction(() => {
checkOptionBeforeAction(addItemToCart);
});
};

return (
Expand Down Expand Up @@ -168,12 +185,7 @@
<span className={styles.ico_funding} />
펀딩 아이템으로 등록하기
</Button>
{/* TODO : 로그인 되었을 때만 보이게 */}
<Button
color="white"
onClick={handleClickCart}
className={styles.btn_cart}
>
<Button color="white" onClick={handleAddCart} className={styles.btn_cart}>
<span className={styles.ico_cart} />
선물상자 담기
</Button>
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/Product/DetailBottom/ProductItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const ProductItem = ({ product }: ProductItemProps) => {
<strong className={styles.txt_prod_name}>{product.name}</strong>
<Price price={product.price} />
<div className={styles.wrapper_util_info}>
<CartButton />
<CartButton productId={product.productId} />
</div>
</article>
);
Expand Down
Loading