-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
303 additions
and
38 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
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
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,11 @@ | ||
import { CartType } from '../../graphql/cart' | ||
|
||
const ItemData = ({ imageUrl, price, title }: Pick<CartType, 'imageUrl' | 'price' | 'title'>) => ( | ||
<> | ||
<img className="cart-item__image" src={imageUrl} /> | ||
<p className="cart-item__price">{price}</p> | ||
<p className="cart-item__title">{title}</p> | ||
</> | ||
) | ||
|
||
export default ItemData |
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,49 @@ | ||
import { useState } from 'react' | ||
import { useMutation } from 'react-query' | ||
import { useNavigate } from 'react-router-dom' | ||
import { useRecoilState, useSetRecoilState } from 'recoil' | ||
import { EXECUTE_PAY } from '../../graphql/payment' | ||
import { graphqlFetcher } from '../../queryClient' | ||
import { checkedCartState } from '../../recoils/cart' | ||
import WillPay from '../willPay' | ||
import PaymentModal from './modal' | ||
|
||
type PayInfo = { | ||
id: string | ||
amount: number | ||
} | ||
type PaymentInfos = PayInfo[] | ||
|
||
const Payment = () => { | ||
const navigate = useNavigate() | ||
const [checkedCartData, setCheckedCartData] = useRecoilState(checkedCartState) | ||
const [modalShown, toggleModal] = useState(false) | ||
const { mutate: executePay } = useMutation((payInfos: PaymentInfos) => | ||
graphqlFetcher(EXECUTE_PAY, payInfos), | ||
) | ||
|
||
const showModal = () => { | ||
toggleModal(true) | ||
} | ||
|
||
const proceed = () => { | ||
const payInfos = checkedCartData.map(({ id, amount }) => ({ id, amount })) | ||
executePay(payInfos) | ||
setCheckedCartData([]) | ||
|
||
// navigate('/products', { replace: true }) | ||
} | ||
|
||
const cancel = () => { | ||
toggleModal(false) | ||
} | ||
|
||
return ( | ||
<div> | ||
<WillPay submitTitle="결제하기" handleSubmit={showModal} /> | ||
<PaymentModal show={modalShown} proceed={proceed} cancel={cancel} /> | ||
</div> | ||
) | ||
} | ||
|
||
export default Payment |
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,32 @@ | ||
import { ReactChild } from 'react' | ||
import { createPortal } from 'react-dom' | ||
|
||
const ModalPortal = ({ children }: { children: ReactChild }) => { | ||
return createPortal(children, document.getElementById('modal')!) | ||
} | ||
|
||
const PaymentModal = ({ | ||
show, | ||
proceed, | ||
cancel, | ||
}: { | ||
show: boolean | ||
proceed: () => void | ||
cancel: () => void | ||
}) => { | ||
return show ? ( | ||
<ModalPortal> | ||
<div className={`modal ${show ? 'show' : ''}`}> | ||
<div className="modal__inner"> | ||
<p>정말 결제할까요?</p> | ||
<div> | ||
<button onClick={proceed}>예</button> | ||
<button onClick={cancel}>아니오</button> | ||
</div> | ||
</div> | ||
</div> | ||
</ModalPortal> | ||
) : null | ||
} | ||
|
||
export default PaymentModal |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Product } from '../../graphql/products' | ||
import ProductItem from './item' | ||
|
||
const ProductList = ({ list }: { list: Product[] }) => ( | ||
<ul className="products"> | ||
{list.map(product => ( | ||
<ProductItem {...product} key={product.id} /> | ||
))} | ||
</ul> | ||
) | ||
|
||
export default ProductList |
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,37 @@ | ||
import { SyntheticEvent } from 'react' | ||
import { useNavigate } from 'react-router-dom' | ||
import { useRecoilValue } from 'recoil' | ||
import { checkedCartState } from '../../recoils/cart' | ||
import ItemData from '../cart/itemData' | ||
|
||
const WillPay = ({ | ||
submitTitle, | ||
handleSubmit, | ||
}: { | ||
submitTitle: string | ||
handleSubmit: (e: SyntheticEvent) => void | ||
}) => { | ||
const checkedItems = useRecoilValue(checkedCartState) | ||
const totalPrice = checkedItems.reduce((res, { price, amount }) => { | ||
res += price * amount | ||
return res | ||
}, 0) | ||
|
||
return ( | ||
<div className="cart-willpay"> | ||
<ul> | ||
{checkedItems.map(({ imageUrl, price, title, amount, id }) => ( | ||
<li key={id}> | ||
<ItemData imageUrl={imageUrl} price={price} title={title} /> | ||
<p>수량: {amount}</p> | ||
<p>금액: {price * amount}</p> | ||
</li> | ||
))} | ||
</ul> | ||
<p>총예상결제액: {totalPrice}</p> | ||
<button onClick={handleSubmit}>{submitTitle}</button> | ||
</div> | ||
) | ||
} | ||
|
||
export default WillPay |
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,12 @@ | ||
import { gql } from 'graphql-request' | ||
|
||
export const EXECUTE_PAY = gql` | ||
type PayInfo { | ||
id: String! | ||
amount: Int! | ||
} | ||
mutation EXECUTE_PAY($info: [PayInfo]) { | ||
payInfo(info: $info) | ||
} | ||
` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
const Payment = () => <div /> | ||
import Payment from '../../components/payment' | ||
|
||
export default Payment | ||
const PaymentPage = () => <Payment /> | ||
|
||
export default PaymentPage |
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 |
---|---|---|
@@ -1,21 +1,17 @@ | ||
import { useQuery } from 'react-query' | ||
import ProductItem from '../../components/product/item' | ||
import ProductList from '../../components/product/list' | ||
import GET_PRODUCTS, { Products } from '../../graphql/products' | ||
import { graphqlFetcher, QueryKeys } from '../../queryClient' | ||
|
||
const ProductList = () => { | ||
const ProductListPage = () => { | ||
const { data } = useQuery<Products>(QueryKeys.PRODUCTS, () => graphqlFetcher(GET_PRODUCTS)) | ||
|
||
return ( | ||
<div> | ||
<h2>상품목록</h2> | ||
<ul className="products"> | ||
{data?.products?.map(product => ( | ||
<ProductItem {...product} key={product.id} /> | ||
))} | ||
</ul> | ||
<ProductList list={data?.products || []} /> | ||
</div> | ||
) | ||
} | ||
|
||
export default ProductList | ||
export default ProductListPage |
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
Oops, something went wrong.