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

Support multiple Pagination components on the same page #2649

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

scottdixon
Copy link
Contributor

@scottdixon scottdixon commented Nov 21, 2024

WHY are these changes introduced?

Enables the use of multiple <Pagination /> components on a single page.

Resolves https://github.com/Shopify/hydrogen-internal/issues/124

WHAT is this pull request doing?

  • Introduces a namespace prop on the <Pagination /> component. If provided, the namespace value prepends the URL parameters - as in <namespace>_cursor and <namespace>_direction.
  • If no namespace is provided, existing cursor and direction params are used.
  • namespaces are stored in the location state so we can:
    • Keep nodes separate.
    • Keep track of namespaces in use.
  • Updates the getPaginationVariables utility to accept an optional namespace.

HOW to test your changes?

  • Fire up the Skeleton template against this branch
  • Replace app/routes/collections.all.tsx with this content
  • Replace app/components/PaginatedResourceSection.tsx with this content
  • Test with and without namespace.
  • Test params work and pagination is correct with soft/hard reloads.

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 Nov 21, 2024

Oxygen deployed a preview of your sd-multiple-pagination-components branch. Details:

Storefront Status Preview link Deployment details Last update (UTC)
Skeleton (skeleton.hydrogen.shop) ✅ Successful (Logs) Preview deployment Inspect deployment November 25, 2024 6:49 AM
metaobjects ✅ Successful (Logs) Preview deployment Inspect deployment November 25, 2024 6:49 AM
classic-remix ✅ Successful (Logs) Preview deployment Inspect deployment November 25, 2024 6:49 AM
custom-cart-method ✅ Successful (Logs) Preview deployment Inspect deployment November 25, 2024 6:50 AM
third-party-queries-caching ✅ Successful (Logs) Preview deployment Inspect deployment November 25, 2024 6:50 AM

Learn more about Hydrogen's GitHub integration.

@scottdixon scottdixon marked this pull request as ready for review November 22, 2024 05:54
Comment on lines 228 to 243
// Handle non-namespaced params (empty string namespace)
if (activeNamespaces.includes('')) {
params.delete('cursor');
params.delete('direction');
}

activeNamespaces
.filter((namespace) => namespace !== '')
.forEach((namespace) => {
const cursorParam = `${namespace}_cursor`;
const directionParam = `${namespace}_direction`;

params.delete(cursorParam);
params.delete(directionParam);
});

Copy link
Contributor

Choose a reason for hiding this comment

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

Optionally, you could roll this into one by doing:

      const namespacePrefix = namespace === '' ? '' : `${namespace}_`
      const cursorParam = `${namespacePrefix}cursor`;
      const directionParam = `${namespacePrefix}direction`;

Though tbh it would have been fine to give no special treatment to the empty namespace, and just used _cursor and _direction when working with it (although granted that might look ugly in the URL)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yess much cleaner, thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And yeah I thought _cursor was a bit of an eye sore + would break existing links when folks upgrade.

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