From 49acc4b1a4a2d291810124d744d6bf7a2a29a14d Mon Sep 17 00:00:00 2001 From: Toshiaki Maki Date: Thu, 31 Dec 2020 01:03:11 +0900 Subject: [PATCH] Add add-cart functionality gh-18 --- shop-ui/ui/src/App.js | 21 +++++++++++-- shop-ui/ui/src/components/CartSummary.js | 20 ++---------- shop-ui/ui/src/routes/Sock.js | 39 ++++++++++++++++++++++-- 3 files changed, 56 insertions(+), 24 deletions(-) diff --git a/shop-ui/ui/src/App.js b/shop-ui/ui/src/App.js index be4b4156..9c9e3168 100644 --- a/shop-ui/ui/src/App.js +++ b/shop-ui/ui/src/App.js @@ -1,4 +1,4 @@ -import React from 'react'; +import React, {useEffect, useState} from "react"; import {BrowserRouter as Router, Link, Route, Switch} from "react-router-dom"; import {Cart} from "./routes/Cart"; import {Sock} from "./routes/Sock"; @@ -8,18 +8,23 @@ import {CartSummary} from "./components/CartSummary"; import {UserInfo} from "./components/UserInfo"; export default function App() { + const [cart, setCart] = useState({ + items: [] + }); + const refreshCart = () => fetchCart().then(setCart); + useEffect(refreshCart, []); return (

Spring Socks

- + - + @@ -33,3 +38,13 @@ export default function App() { ); } +function fetchCart() { + return fetch('/cart', { + method: 'GET', + headers: { + 'Accept': 'application/json' + }, + }) + .then(res => res.json()); +} + diff --git a/shop-ui/ui/src/components/CartSummary.js b/shop-ui/ui/src/components/CartSummary.js index dde677f6..6356c4dc 100644 --- a/shop-ui/ui/src/components/CartSummary.js +++ b/shop-ui/ui/src/components/CartSummary.js @@ -1,22 +1,6 @@ -import React, {useEffect, useState} from "react"; +import React from "react"; import {Link} from "react-router-dom"; -export function CartSummary() { - const [cart, setCart] = useState({ - items: [] - }); - useEffect(() => { - fetchCart().then(setCart); - }, []); +export function CartSummary({cart}) { return

Cart {cart.itemSize} items : ${cart.total}

; -} - -function fetchCart() { - return fetch('/cart', { - method: 'GET', - headers: { - 'Accept': 'application/json' - }, - }) - .then(res => res.json()); } \ No newline at end of file diff --git a/shop-ui/ui/src/routes/Sock.js b/shop-ui/ui/src/routes/Sock.js index cdfb928a..28d4216c 100644 --- a/shop-ui/ui/src/routes/Sock.js +++ b/shop-ui/ui/src/routes/Sock.js @@ -1,23 +1,41 @@ -import {Link, useParams} from "react-router-dom"; +import {Link, Redirect, useParams} from "react-router-dom"; import React, {useEffect, useState} from "react"; import {Tags} from "../components/Tags"; -export function Sock() { +export function Sock({refreshCart}) { const {id} = useParams(); const [sock, setSock] = useState({}); + const [isSubmitting, setIsSubmitting] = useState(false); const [relatedProducts, setRelatedProducts] = useState([]); - + const [quantity, setQuantity] = useState(1); + const [redirectToCart, setRedirectToCart] = useState(false); useEffect(() => { fetchSock(id).then(body => { setSock(body.sock); setRelatedProducts(body.relatedProducts) }); }, [id]); + const onChangeQuantity = e => setQuantity(e.target.value); + const onClickAddCartItem = e => { + e.preventDefault(); + setIsSubmitting(true); + addCartItem(id, quantity) + .then(refreshCart) + .then(() => setRedirectToCart(true)) + .finally(() => setIsSubmitting(false)); + }; return
+ {redirectToCart && }

{sock.name}

{sock.name}

${sock.price}

{sock.description}

+
+ + +

Related Products

    {relatedProducts.map(sock =>
  • ; } +function addCartItem(id, quantity) { + return fetch(`/cart`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Accept': 'application/json' + }, + body: JSON.stringify({ + id: id, + quantity: quantity + }) + }) + .then(res => res.json()); +} + function fetchSock(id) { return fetch(`/details/${id}`, { method: 'GET',