-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
주요 변경 사항 - 장바구니 담기 API를 추가했습니다. - useBook hook을 수정하여 장바구니 담기 기능과 상태 관리를 추가했습니다. - AddToCart 컴포넌트를 생성하여 장바구니 담기 폼 구현했습니다. - 도서 상세 페이지에 AddToCart 컴포넌트를 추가하여 장바구니 담기 기능을 제공했습니다. 관련 변경 사항 - cart.api.ts 파일 생성 - AddToCart.tsx 파일 생성 - useBook.ts 파일 수정 - BookDetail.tsx 파일 수정
- Loading branch information
Showing
4 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { httpClient } from "./http"; | ||
|
||
interface AddCartParams { | ||
book_id: number; | ||
quantity: number; | ||
} | ||
|
||
const addCart = async (params: AddCartParams) => { | ||
const response = await httpClient.post('/cart', params) | ||
|
||
return response.data | ||
}; | ||
|
||
export default addCart; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { useState } from "react"; | ||
import { Link } from "react-router-dom"; | ||
import styled from "styled-components"; | ||
import { useBook } from "../../hooks/useBook"; | ||
import { BookDetail } from "../../models/book.model"; | ||
import Button from "../common/Button"; | ||
import { InputText } from "../common/InputText"; | ||
|
||
interface Props { | ||
book: BookDetail; | ||
} | ||
|
||
const AddToCart = ({ book }: Props) => { | ||
const [quantity, setQuantity] = useState<number>(1) | ||
const { addToCart, cartAdded } = useBook(book.id.toString()); | ||
|
||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setQuantity(Number(e.target.value)) | ||
} | ||
|
||
const handleIncrease = () => { | ||
setQuantity(quantity + 1) | ||
} | ||
|
||
const handleDecrease = () => { | ||
if (quantity === 1) return | ||
setQuantity(quantity - 1) | ||
} | ||
|
||
return ( | ||
<AddToCartStyle $added={cartAdded}> | ||
<div> | ||
<InputText inputType="number" value={quantity} onChange={handleChange}/> | ||
<Button size="medium" scheme="normal" onClick={handleIncrease}> | ||
+ | ||
</Button> | ||
<Button size="medium" scheme="normal" onClick={handleDecrease}> | ||
- | ||
</Button> | ||
</div> | ||
<Button size="medium" scheme="primary" onClick={() => addToCart(quantity)}> | ||
장바구니 담기 | ||
</Button> | ||
<div className="added"> | ||
<p>장바구니에 추가되었습니다.</p> | ||
<Link to="/cart">장바구니 이동</Link> | ||
</div> | ||
</AddToCartStyle> | ||
); | ||
} | ||
|
||
interface AddToCartStyleProps { | ||
$added: boolean; | ||
} | ||
|
||
const AddToCartStyle = styled.div<AddToCartStyleProps>` | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
position: relative; | ||
.added { | ||
position: absolute; | ||
right: 0; | ||
bottom: -90px; | ||
background: ${({ theme }) => theme.color.background}; | ||
border-radius: ${({ theme }) => theme.borderRadius.default}; | ||
padding: 8px 12px; | ||
opacity: ${({ $added }) => $added ? "1" : "0"}; | ||
transition: all 0.5s ease; | ||
} | ||
p { | ||
padding: 0 0 8px 0; | ||
margin: 0; | ||
} | ||
`; | ||
|
||
export default AddToCart; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters