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

Improve the types for useOptimisticCart() #2269

Merged
merged 2 commits into from
Jul 9, 2024

Conversation

blittle
Copy link
Contributor

@blittle blittle commented Jun 21, 2024

WHY are these changes introduced?

Fix issues in #2132 that also exposed some improvements to be made to the types in useOptimisticCart().

WHAT is this pull request doing?

HOW to test your changes?

Post-merge steps

Checklist

  • I've read the Contributing Guidelines
  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've added a changeset if this PR contains user-facing or noteworthy changes
  • I've added tests to cover my changes
  • I've added or updated the documentation

Copy link
Contributor

shopify bot commented Jun 21, 2024

Oxygen deployed a preview of your bl-fix-optimistic-cart-types branch. Details:

Storefront Status Preview link Deployment details Last update (UTC)
optimistic-cart-ui ✅ Successful (Logs) Preview deployment Inspect deployment July 9, 2024 7:47 PM
custom-cart-method ✅ Successful (Logs) Preview deployment Inspect deployment July 9, 2024 7:46 PM
classic-remix ✅ Successful (Logs) Preview deployment Inspect deployment July 9, 2024 7:46 PM
third-party-queries-caching ✅ Successful (Logs) Preview deployment Inspect deployment July 9, 2024 7:46 PM
metaobjects ✅ Successful (Logs) Preview deployment Inspect deployment July 9, 2024 7:46 PM
Skeleton (skeleton.hydrogen.shop) ✅ Successful (Logs) Preview deployment Inspect deployment July 9, 2024 7:46 PM

Learn more about Hydrogen's GitHub integration.

examples/multipass/app/components/Cart.tsx Outdated Show resolved Hide resolved
@@ -26,7 +26,7 @@ export function CartMain({layout, cart: originalCart}: CartMainProps) {
cart &&
Boolean(cart?.discountCodes?.filter((code) => code.applicable)?.length);
const className = `cart-main ${withDiscount ? 'with-discount' : ''}`;
const cartHasItems = !!cart && cart.totalQuantity > 0;
const cartHasItems = cart?.totalQuantity && cart.totalQuantity > 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe rely on the gt operator to narrow the type to boolean instead of boolean | number | undefined?

Suggested change
const cartHasItems = cart?.totalQuantity && cart.totalQuantity > 0;
const cartHasItems = cart?.totalQuantity > 0;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Typescript doesn't like undefined > 0. So it has to be cart?.totalQuantity! > 0

@blittle blittle force-pushed the bl-fix-optimistic-cart-types branch from 2bf295e to 0fdf276 Compare July 9, 2024 19:44
@michenly michenly merged commit 66236ca into main Jul 9, 2024
13 checks passed
@michenly michenly deleted the bl-fix-optimistic-cart-types branch July 9, 2024 20:33
michenly pushed a commit that referenced this pull request Jul 11, 2024
* Improve the types for `useOptimisticCart()`

* Fixes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants