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

feat: add optional fetcher key to CartForm #1792

Merged
merged 3 commits into from
Mar 11, 2024

Conversation

rmiller61
Copy link
Contributor

WHY are these changes introduced?

Improve developers' experience by giving them access to a CartForm's fetcher outside the component where the fetcher is created.

For example, if I have a CartFrom that opens a drawer after a product has been added to the cart, I would like to display a pending UI while the form route is being called. To do this, I need to know the value of a fetcher.state that is scoped outside of the modal.

By passing a key to the CartForm's useFetcher hook, I can access that fetcher anywhere in my app:

// app/components/ProductForm.tsx

<CartForm fetcherKey="MY_FETCHER_KEY" route="/cart" action={CartForm.ACTIONS.LinesAdd}>

     // Render the inputs

     <button type="submit" onClick={ () => openDrawer() }>Add to Cart</button>

</CartForm>
// app/components/CartDrawer.tsx

const fetcher = useFetcher({ key: "MY_FETCHER_KEY" });

if (fetcher.state !== 'idle') {
     // Pending UI
}

WHAT is this pull request doing?

Adds an optional fetcherKey prop to packages/hydrogen/src/cart/CartForm.tsx and passes it to the component's useFetcher hook.

HOW to test your changes?

  • Build @shopify/hydrogen and run a demo app.
  • Since CarForm is an abstraction of an existing API (useFetcher()) and this feature builds on top of that API, it shouldn't affect test coverage

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

@rmiller61
Copy link
Contributor Author

I have signed the CLA!

Copy link
Contributor

@wizardlyhel wizardlyhel left a comment

Choose a reason for hiding this comment

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

Thanks for the contributions

.changeset/blue-garlics-obey.md Outdated Show resolved Hide resolved
@wizardlyhel wizardlyhel merged commit 351b3c1 into Shopify:main Mar 11, 2024
7 checks passed
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.

2 participants