-
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.
Merge pull request #156 from jihwooon/issue-84
장바구니 담기 기능 및 도서 상세 페이지 개선
- 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