From 2be9c4405e5c2c730cdf483c25b77f1ea9cfa10a Mon Sep 17 00:00:00 2001 From: KirankumarAmbati Date: Mon, 21 Jan 2019 13:47:05 +0530 Subject: [PATCH 1/3] closes #241 --- src/components/Cart/Cart.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/components/Cart/Cart.js b/src/components/Cart/Cart.js index 3b2d6a93..9c943a70 100644 --- a/src/components/Cart/Cart.js +++ b/src/components/Cart/Cart.js @@ -322,9 +322,10 @@ class Cart extends Component { {({ client, checkout, removeLineItem, updateLineItem, adding }) => { const setCartLoading = bool => this.setState({ isLoading: bool }); - const handleRemove = itemID => event => { + const handleRemove = itemID => async event => { event.preventDefault(); - removeLineItem(client, checkout.id, itemID); + await removeLineItem(client, checkout.id, itemID); + setCartLoading(false); }; const handleQuantityChange = lineItemID => async quantity => { From a0f89cbd0de38a76600dc6b83d1d5da1a6e7313d Mon Sep 17 00:00:00 2001 From: Jason Lengstorf Date: Tue, 22 Jan 2019 07:57:35 -0800 Subject: [PATCH 2/3] fix: add input handling logic --- src/components/Cart/CartListItem.js | 33 ++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/components/Cart/CartListItem.js b/src/components/Cart/CartListItem.js index caaede34..b07b8a1a 100644 --- a/src/components/Cart/CartListItem.js +++ b/src/components/Cart/CartListItem.js @@ -78,7 +78,7 @@ export default ({ }) => { const [quantity, setQuantity] = useState(1); - if (item.quantity !== quantity && !isCartLoading) { + if (item.quantity !== quantity && quantity !== '' && !isCartLoading) { setQuantity(item.quantity); } @@ -87,12 +87,34 @@ export default ({ return; } - const target = event.target; - const value = Number(target.value); + const value = event.target.value; + // Make sure the quantity is always at least 1. + const safeValue = Math.max(Number(value), 0); + + // No need to update if the value hasn’t updated. + if (value === quantity) { + return; + } + + // If the field is empty, update the state but don’t do anything else. + if (value === '') { + setQuantity(value); + return; + } + + // Otherwise, trigger the loading state and set the quantity in state. setCartLoading(true); - setQuantity(value); - updateQuantity(value); + setQuantity(safeValue); + + // If the quantity is set to 0, remove the item. + if (safeValue === 0) { + handleRemove(event); + return; + } + + // If we get here, update the quantity. + updateQuantity(safeValue); }; const handleRemoveItem = event => { @@ -121,6 +143,7 @@ export default ({ min="1" step="1" onChange={event => handleInputChange(event)} + onBlur={() => setQuantity(item.quantity)} value={quantity} /> From 8dd107ba60a41c91b6e911962f9575af90be5499 Mon Sep 17 00:00:00 2001 From: KirankumarAmbati Date: Tue, 22 Jan 2019 22:42:23 +0530 Subject: [PATCH 3/3] chore: replaced *push* with *navigate* and some code refactoring --- src/components/Cart/CartIndicator.js | 2 +- src/components/Cart/FreeBonus.js | 2 +- src/components/Layout/Layout.js | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/components/Cart/CartIndicator.js b/src/components/Cart/CartIndicator.js index 59584ab5..d81c269a 100644 --- a/src/components/Cart/CartIndicator.js +++ b/src/components/Cart/CartIndicator.js @@ -38,7 +38,7 @@ class CartIndicator extends Component { ? `${num} new items have been added to the cart` : `${num} new item has been added to the cart`; - this.setState({ message: message }); + this.setState({ message }); setTimeout( () => this.setState({ visible: false, message: '' }), diff --git a/src/components/Cart/FreeBonus.js b/src/components/Cart/FreeBonus.js index 13abd962..d8ec1df8 100644 --- a/src/components/Cart/FreeBonus.js +++ b/src/components/Cart/FreeBonus.js @@ -41,7 +41,7 @@ const FreeBonus = () => ( your order!

- + Gift Icon ); diff --git a/src/components/Layout/Layout.js b/src/components/Layout/Layout.js index 2689aa4e..0adfd0a3 100644 --- a/src/components/Layout/Layout.js +++ b/src/components/Layout/Layout.js @@ -1,6 +1,6 @@ import React from 'react'; import styled, { injectGlobal } from 'react-emotion'; -import { push } from 'gatsby'; +import { navigate } from 'gatsby'; import { client } from '../../context/ApolloContext'; import StoreContext, { defaultStoreContext } from '../../context/StoreContext'; @@ -108,7 +108,7 @@ export default class Layout extends React.Component { loading: false } }); - logout(() => push('/')); + logout(() => navigate('/')); }, updateContributor: data => { this.setState(state => ({