-
Notifications
You must be signed in to change notification settings - Fork 220
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙊