Skip to content
Snippets Groups Projects
Commit 68298908 authored by YU SUN's avatar YU SUN
Browse files

finish

parent 5ff3aa63
No related branches found
No related tags found
No related merge requests found
Source diff could not be displayed: it is too large. Options to address this: view the blob.
/**
* @license React
* react-dom.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* @license React
* react.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* @license React
* scheduler.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
public/assets/images/Mindy_Mouse_cat_toy.jpg

87.4 KiB

public/assets/images/cat-house.jpg

54.8 KiB

public/assets/images/cat-litter.jpg

647 KiB

public/assets/images/laser-pointer.jpg

1.43 MiB

public/assets/images/yarn.jpg

208 KiB

{
"products":[
{
"id": 1001,
"title": "고양이 사료, 25파운드",
"description": "당신의 고양이를 위한 <em>거부할 수 없는</em>, 유기농 25파운드 사료입니다.",
"price": 2000,
"image": "assets/images/product-fullsize.png",
"availableInventory": 10,
"rating": 1
},
{
"id": 1002,
"title": "실뭉치",
"description": "실뭉치로 당신의 고양이에게 <strong>오랜</strong> 놀이 시간을 주세요!",
"price": 299,
"image": "assets/images/yarn.jpg",
"availableInventory": 7,
"rating": 1
},
{
"id": 1003,
"title": "고양이 화장실",
"description": "당신의 고양이를 위한 최고급 화장실입니다.",
"price": 1100,
"image": "assets/images/cat-litter.jpg",
"availableInventory": 99,
"rating": 4
},
{
"id": 1004,
"title": "고양이 집",
"description": "고양이가 놀 수 있는 장소!",
"price": 799,
"image": "assets/images/cat-house.jpg",
"availableInventory": 11,
"rating": 5
},
{
"id": 1005,
"title": "레이저 포인터",
"description": "이 <em>놀라운</em> 상품으로 고양이와 놀아주세요.",
"price": 4999,
"image": "assets/images/laser-pointer.jpg",
"availableInventory": 25,
"rating": 1
}
]
}
import React, { useState } from 'react'; import React, { useState, useEffect } from 'react';
import './assets/css/app.css'; import './assets/css/app.css';
const App = (props) => { const App = (props) => {
let [cartItems, setCartItems] = useState([]); let [cartItems, setCartItems] = useState({});
let [showProduct, setShowProduct] = useState(true); let [showProduct, setShowProduct] = useState(true);
let [order, setOrder] = useState({ firstName: '', lastName: '', state: 'CA', address: '' }); let [order, setOrder] = useState({ firstName: '', lastName: '', state: 'CA', address: '' });
let [products, setProducts] = useState([]);
let states = ['AL', 'AR', 'CA', 'NV', 'NY', 'FL']; let states = ['AL', 'AR', 'CA', 'NV', 'NY', 'FL'];
let product = {
id: 1001,
title: "고양이 사료, 25파운드",
description: "당신의 고양이를 위한 거부할 수 없는, 유기농 25파운드 사료입니다.",
price: 2000,
image: "./assets/images/product-fullsize.png", const nameOrder = (p, q) => {
availableInventory : 5, let [pLow, qLow] = [p.title.toLowerCase(), q.title.toLowerCase()]
}; if (pLow > qLow) {
return 1;
}
else if (pLow == qLow) {
return 0;
}
else {
return -1;
}
}
const formatPrice = (price) => { const formatPrice = (price) => {
if (!parseInt(price)) { return ""; } if (!parseInt(price)) { return ""; }
...@@ -33,12 +44,38 @@ const App = (props) => { ...@@ -33,12 +44,38 @@ const App = (props) => {
} }
} }
const addToShoppingCart = () => { useEffect(() => {
setCartItems([...cartItems, product]); fetch('./products.json')
.then((r) => r.json())
.then(data => {
setProducts([...data.products]);
// console.log(products);
})
}, [])
// useEffect(() => {
// console.log(products);
// }, [products]);
const addToShoppingCart = (product) => {
const updatedCartItems = { ...cartItems };
if (updatedCartItems[product.id]) {
updatedCartItems[product.id] += 1;
} else {
updatedCartItems[product.id] = 1;
}
setCartItems(updatedCartItems);
} }
const canAddToCart = () => { const canAddToCart = (product) => {
return product.availableInventory > cartItems.length; // console.log("canAddToCart:" + product.availableInventory)
let produtNum=0;
if (cartItems[product.id]) {
produtNum= cartItems[product.id];
}
return product.availableInventory > produtNum
} }
const updateOrder = (event) => { const updateOrder = (event) => {
...@@ -49,6 +86,19 @@ const App = (props) => { ...@@ -49,6 +86,19 @@ const App = (props) => {
} }
setOrder({ ...order, [control.name]: control.value }); setOrder({ ...order, [control.name]: control.value });
} }
const checkRating = (n, product) => {
return product.rating - n >= 0;
}
const allProductsNum = ()=>{
let sum=0
Object.keys(cartItems).forEach((key) => {
sum+=cartItems[key]
});
return sum;
}
return ( return (
...@@ -64,7 +114,7 @@ const App = (props) => { ...@@ -64,7 +114,7 @@ const App = (props) => {
<button type="button" className="btn btn-default btn-lg" <button type="button" className="btn btn-default btn-lg"
onClick={() => setShowProduct(!showProduct)} > onClick={() => setShowProduct(!showProduct)} >
<span className="glyphicon glyphicon-shopping-cart"> <span className="glyphicon glyphicon-shopping-cart">
{cartItems.length || ''} {allProductsNum() || ''}
</span> </span>
체크아웃 체크아웃
</button> </button>
...@@ -73,25 +123,51 @@ const App = (props) => { ...@@ -73,25 +123,51 @@ const App = (props) => {
</header> </header>
<main> <main>
<div className="row product"> <div className="row product">
{showProduct && {showProduct && products &&
<> products.sort(nameOrder).map(product => {
<div className="col-md-2 col-md-offset-1">
let numProductsInCart = cartItems[product.id];
if (!numProductsInCart)numProductsInCart=0
return (
<div className="row product">
<div className="col-md-4 col-md-offset-0">
<figure> <figure>
<img src={product.image} /> <img className="product" src={product.image} />
</figure> </figure>
</div> </div>
<div className="col-md-6 col-md-offset-2 description"> <div className="col-md-6 col-md-offset-2 description">
<h1> {product.title} </h1> <h1> {product.title} </h1>
<p> {product.description} </p> <p> {product.description} </p>
<p className="price"> {formatPrice(product.price)} </p> <p className="price"> {formatPrice(product.price)} </p>
{canAddToCart() && {canAddToCart(product) &&
<button className="btn btn-primary btn-lg" <button className="btn btn-primary btn-lg"
onClick={addToShoppingCart}> 장바구니 담기 onClick={() => addToShoppingCart(product)}> 장바구니 담기
</button> </button>
} }
<span className="inventory-message">
{/* {(product.availableInventory == numProductsInCart && `품절!(Sold out)`) ||
(numProductsInCart == 0 && `지금 구매하세요.`) ||
(product.availableInventory > numProductsInCart && numProductsInCart > 0 &&
`${product.availableInventory - numProductsInCart} 남았습니다.`)} */}
{product.availableInventory == numProductsInCart && `품절!(Sold out)`}
{numProductsInCart == 0 && `지금 구매하세요.`}
{product.availableInventory > numProductsInCart && numProductsInCart > 0 &&
`${product.availableInventory - numProductsInCart} 남았습니다.`}
</span>
<div className="rating">
{[1, 2, 3, 4, 5].map((i) => {
return <span className={checkRating(i, product) ? "rating-active" : ""}></span>
})}
</div> </div>
</> </div>
}
</div>
)
})}
{!showProduct && /* Checkout 페이지 들어갈 자리 */ {!showProduct && /* Checkout 페이지 들어갈 자리 */
<div> <div>
<div className="col-md-6"> <div className="col-md-6">
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment