-
Notifications
You must be signed in to change notification settings - Fork 19
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
Cart item quantity basics #748
base: main
Are you sure you want to change the base?
Conversation
and fix a potential reference data loading error
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.
FIRST-LOOK-REVIEW (5f82a27)
return product instanceof DataItem | ||
? { | ||
id: product.id(), | ||
quantity: Number(item.get('quantity')), |
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.
Can you provide a fallback, e.g. to 1
, if no quantity is given or NaN? E.g. someone added something to the cart, never touched it, we upgrade our code, now the quantity
in the cart is undefined
.
return sum( | ||
CartItem.all() | ||
.take() | ||
.map((item) => item.get('quantity')), | ||
) |
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.
Do we really need sum
here? How about:
return CartItem.all()
.take()
.map((item) => item.get('quantity'))
.filter((quantity) => typeof quantity === 'number')
.reduce((acc, quantity) => acc + quantity, 0)
<button | ||
className="btn btn-sm btn-primary my-1" | ||
onClick={async () => { | ||
await subtractFromCart(product) | ||
toast.info(cartRemovedMessage) | ||
}} | ||
> | ||
<i className="bi bi-dash-lg px-0" /> | ||
<span className="d-none" aria-hidden> | ||
- | ||
</span> | ||
</button> |
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.
const item = await load(() => findInCart(product)) | ||
|
||
if (item) { | ||
item.update({ quantity: Number(item.get('quantity')) + 1 }) |
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.
Number(item.get('quantity'))
could be a NaN
if no quantity
exists (e.g. an earlier app version)
export function isInCart(product: ProductInstance): boolean { | ||
if (!isUserLoggedIn()) return false // TODO: remove, once CartItem itself requires a login | ||
export function quantityInCart(product: ProductInstance): number { | ||
return Number(findInCart(product)?.get('quantity')) |
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.
Number(item.get('quantity'))
could be a NaN
if no quantity
exists (e.g. an earlier app version)
Next up: better support for changing the number of items in the cart view. Currently we only have DataFormNumberWidget which has worse UX than the +/- buttons. I used https://app.uxcel.com/courses/common-patterns/shopping-cart-best-practices-071 for inspiration.