Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: keep item quantity in sync #218

Merged
merged 4 commits into from
Jan 10, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"gatsby-plugin-sharp": "^2.0.6",
"gatsby-source-shopify": "^2.0.5",
"gatsby-transformer-sharp": "^2.1.3",
"react": "^16.4.2",
"react": "^16.8.0-alpha.0",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙊

"react-apollo": "^2.2.4",
"react-dom": "^16.4.2",
"react-dom": "^16.8.0-alpha.0",
"react-emotion": "^9.1.3",
"react-helmet": "^5.2.0",
"react-icons": "^3.1.0",
Expand Down
32 changes: 29 additions & 3 deletions src/components/Cart/Cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,31 @@ const CartRoot = styled(`div`)`
&.open {
transform: translateX(0%);
}

&.closed {
transform: translateX(100%);
}

::after {
background-color: ${colors.lightest};
bottom: 0;
content: '';
left: 0;
opacity: 0;
pointer-events: none;
position: absolute;
right: 0;
top: 0;
transition: all 250ms;
}

&.loading {
::after {
opacity: 0.9;
pointer-events: all;
}
}

@media (min-width: ${breakpoints.desktop}px) {
width: ${dimensions.cartWidthDesktop};

Expand Down Expand Up @@ -261,7 +282,8 @@ const BackLink = styled(Button)`

class Cart extends Component {
state = {
className: 'closed'
className: 'closed',
isLoading: false
};

componentDidUpdate(prevProps) {
Expand All @@ -279,7 +301,6 @@ class Cart extends Component {
if (this.props.isDesktopViewport) {
if (imageBrowserStatusChanged) {
if (this.props.productImagesBrowserStatus === 'open') {
console.log('asdfasdfasdfadsfasdfads');
setTimeout(() => {
this.setState(state => ({
className: state.className + ' covered'
Expand Down Expand Up @@ -322,7 +343,11 @@ class Cart extends Component {
);

return (
<CartRoot className={className}>
<CartRoot
className={`${className} ${
this.state.isLoading ? 'loading' : ''
}`}
>
<Heading>
<CartToggle onClick={toggle}>
{status === 'open' ? (
Expand Down Expand Up @@ -352,6 +377,7 @@ class Cart extends Component {
handleRemove={handleRemove}
updateQuantity={handleQuantityChange}
setCartLoading={setCartLoading}
isCartLoading={this.state.isLoading}
/>

<Costs>
Expand Down
9 changes: 8 additions & 1 deletion src/components/Cart/CartList.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ const Headers = styled(`div`)`
}
`;

const CartList = ({ items, handleRemove, updateQuantity, setCartLoading }) => (
const CartList = ({
items,
handleRemove,
updateQuantity,
setCartLoading,
isCartLoading
}) => (
<>
<Headers>
<span>Product</span>
Expand All @@ -45,6 +51,7 @@ const CartList = ({ items, handleRemove, updateQuantity, setCartLoading }) => (
handleRemove={handleRemove(item.id)}
updateQuantity={updateQuantity(item.id)}
setCartLoading={setCartLoading}
isCartLoading={isCartLoading}
/>
))}
</CartListRoot>
Expand Down
130 changes: 51 additions & 79 deletions src/components/Cart/CartListItem.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import React from 'react';
import styled, { css } from 'react-emotion';
import React, { useState } from 'react';
import styled from 'react-emotion';

import { MdClose } from 'react-icons/md';

import CartThumbail from './CartThumbail';
import { Input } from '../shared/FormElements';
import { Button } from '../shared/Buttons';

import {
breakpoints,
colors,
spacing,
radius,
input,
visuallyHidden
} from '../../utils/styles';
import { breakpoints, colors, spacing } from '../../utils/styles';

const CartListItemRoot = styled('li')`
align-items: center;
Expand Down Expand Up @@ -76,80 +69,59 @@ const Remove = styled(Button)`
}
`;

// Add our own debounce utility so we don’t need to load a lib.
const debounce = (delay, fn) => {
let timeout;

return function(...args) {
if (timeout) {
clearTimeout(timeout);
export default ({
item,
setCartLoading,
updateQuantity,
handleRemove,
isCartLoading
}) => {
const [quantity, setQuantity] = useState(item.quantity);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jlengstorf forgive me if I'm just new to hooks, but I don't think the item quantity will stay sync'd to the corresponding cart list item (fix is missing?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well that’s embarrassing.


const handleInputChange = event => {
if (isCartLoading) {
return;
}

timeout = setTimeout(() => {
fn(...args);
timeout = null;
}, delay);
};
};

class CartListItem extends React.Component {
state = {
quantity: this.props.item.quantity || 1
};

inputChangeHandler = event => {
const target = event.target;
const value = target.value;
const value = Number(target.value);

this.setState({ quantity: value });
this.props.setCartLoading(true);
this.debouncedUpdateQuantity(value);
setCartLoading(true);
setQuantity(value);
updateQuantity(value);
};

debouncedUpdateQuantity = debounce(500, quantity =>
this.props.updateQuantity(quantity)
);

removeHandler = event => {
this.props.setCartLoading(true);
this.props.handleRemove(event);
const handleRemoveItem = event => {
setCartLoading(true);
handleRemove(event);
};

componentWillUnmount() {
this.props.setCartLoading(false);
}

render() {
const { item } = this.props;
return (
<CartListItemRoot>
<Thumbail
id={item.variant.image.id}
fallback={item.variant.image.src}
alt={item.variant.image.altText}
/>
<Info>
<Name>{item.title}</Name>
<Meta>
{item.variant.title}, ${item.variant.price}
</Meta>
</Info>
<Quantity
aria-label="Quantity"
id={`quantiQuantityty_${item.id.substring(58, 64)}`}
type="number"
name="quantity"
min="1"
step="1"
onChange={event => this.inputChangeHandler(event)}
value={this.state.quantity}
/>
<Remove onClick={this.removeHandler}>
<MdClose />
</Remove>
</CartListItemRoot>
);
}
}

export default CartListItem;
return (
<CartListItemRoot>
<Thumbail
id={item.variant.image.id}
fallback={item.variant.image.src}
alt={item.variant.image.altText}
/>
<Info>
<Name>{item.title}</Name>
<Meta>
{item.variant.title}, ${item.variant.price}
</Meta>
</Info>
<Quantity
aria-label="Quantity"
id={`quantity_${item.id.substring(58, 64)}`}
type="number"
name="quantity"
min="1"
step="1"
onChange={event => handleInputChange(event)}
value={quantity}
/>
<Remove onClick={handleRemoveItem}>
<MdClose />
</Remove>
</CartListItemRoot>
);
};
28 changes: 14 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9897,15 +9897,15 @@ react-dev-utils@^4.2.1:
strip-ansi "3.0.1"
text-table "0.2.0"

react-dom@^16.4.2:
version "16.7.0"
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.7.0.tgz#a17b2a7ca89ee7390bc1ed5eb81783c7461748b8"
integrity sha512-D0Ufv1ExCAmF38P2Uh1lwpminZFRXEINJe53zRAbm4KPwSyd6DY/uDoS0Blj9jvPpn1+wivKpZYc8aAAN/nAkg==
react-dom@^16.8.0-alpha.0:
version "16.8.0-alpha.0"
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.8.0-alpha.0.tgz#47bbcf42593eab5468b577d3c3f3c27f251a65ef"
integrity sha512-OGfLUXGxtMgqLB1BqnB9flVCvc8VvAnhQYeFr+EUzwiODcD34dPrxtfa7ccK4dwzcPi8HRLSyBcaQi2OvVoUww==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"
prop-types "^15.6.2"
scheduler "^0.12.0"
scheduler "^0.13.0-alpha.0"

react-emotion@^9.1.3:
version "9.2.12"
Expand Down Expand Up @@ -9998,15 +9998,15 @@ react-side-effect@^1.1.0:
exenv "^1.2.1"
shallowequal "^1.0.1"

react@^16.4.2:
version "16.7.0"
resolved "https://registry.yarnpkg.com/react/-/react-16.7.0.tgz#b674ec396b0a5715873b350446f7ea0802ab6381"
integrity sha512-StCz3QY8lxTb5cl2HJxjwLFOXPIFQp+p+hxQfc8WE0QiLfCtIlKj8/+5tjjKm8uSTlAW+fCPaavGFS06V9Ar3A==
react@^16.8.0-alpha.0:
version "16.8.0-alpha.0"
resolved "https://registry.yarnpkg.com/react/-/react-16.8.0-alpha.0.tgz#8545eb79534cce5ba99ed40085f7f955e8857a19"
integrity sha512-B/nJJkNqV4JAJQ1M0Q38ZvhZ6BoYsjyZq29hbsIIPYayn/0DcYhu7tExpfzdtTC0SnUcA+RnDBTlXTEJ5b9PEg==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"
prop-types "^15.6.2"
scheduler "^0.12.0"
scheduler "^0.13.0-alpha.0"

read-chunk@^1.0.1:
version "1.0.1"
Expand Down Expand Up @@ -10583,10 +10583,10 @@ sax@>=0.6.0, sax@^1.2.4, sax@~1.2.1, sax@~1.2.4:
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==

scheduler@^0.12.0:
version "0.12.0"
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.12.0.tgz#8ab17699939c0aedc5a196a657743c496538647b"
integrity sha512-t7MBR28Akcp4Jm+QoR63XgAi9YgCUmgvDHqf5otgAj4QvdoBE4ImCX0ffehefePPG+aitiYHp0g/mW6s4Tp+dw==
scheduler@^0.13.0-alpha.0:
version "0.13.0-alpha.0"
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.13.0-alpha.0.tgz#8fc1f01617b59e4543376735b2f954867d49110c"
integrity sha512-+DbRwO53hlLso4gzrSIqcfNe8eRMMaV4gd4eySGisbyLBrp5unTpAMR0UAbrqlQLTGhnOFNoQAfaZTz4iPj1RQ==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"
Expand Down