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

Merged
merged 21 commits into from
Dec 10, 2024

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 December 9, 2024 6:05 PM
metaobjects ✅ Successful (Logs) Preview deployment Inspect deployment December 9, 2024 6:05 PM
classic-remix ✅ Successful (Logs) Preview deployment Inspect deployment December 9, 2024 6:05 PM
custom-cart-method ✅ Successful (Logs) Preview deployment Inspect deployment December 9, 2024 6:05 PM
third-party-queries-caching ✅ Successful (Logs) Preview deployment Inspect deployment December 9, 2024 6:05 PM

Learn more about Hydrogen's GitHub integration.

@scottdixon scottdixon requested a review from blittle November 22, 2024 05:45
@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.

Copy link
Contributor

@blittle blittle left a comment

Choose a reason for hiding this comment

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

I tried it out using the tophat examples, and it doesn't seem to behave correctly. Also I'm getting a warning/error in the console about shared namespaces. But the namespace is different passed to each.

See the video: https://screenshot.click/02-49-qtwoz-9ozrw.mp4

Copy link
Contributor

@blittle blittle left a comment

Choose a reason for hiding this comment

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

Looks good! Great work!

@wizardlyhel
Copy link
Contributor

Found a bug with the load previous button. For some reason, click on one of the previous load button will make the other list to load all the previous pages.

Expected: Clicking a load previous button should only affect its own list

Screen.Recording.2024-12-05.at.1.06.26.PM.mov

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.

Found a bug ^

Comment on lines 122 to 126
navigate(window.location.toString(), {
replace: true,
state: {nodes: undefined, pageInfo: undefined},
});
}, []);
Copy link
Contributor

Choose a reason for hiding this comment

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

This completely breaks. It ends up triggering a duplicate URL nav:

image

Copy link
Contributor

Choose a reason for hiding this comment

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

Additionally, I don't think it will work properly if we fix the URL navigation, because it will erase the navigation state even for soft transitions. This means the paginated state will disappear when going back/forth, breaking scroll position.

In my opinion the issue mentioned by Helen is not that big of a deal. It only happens if you hard refresh on a page, then have click next or previous on one of the elements. And even then, it's not bad data getting loaded, just data from both paginated components loading at the same time. Jarring yes, but not rendering bad data at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

wow thank you for catching this!

@wizardlyhel
Copy link
Contributor

@scottdixon - @blittle and I chatted and agree that we're fine with the load previous bug for now. There doesn't seem to be an easy way to revert window history state to that exact state we want.

I have committed an revert on that useEffect

@scottdixon scottdixon merged commit 8f64915 into main Dec 10, 2024
12 checks passed
@scottdixon scottdixon deleted the sd-multiple-pagination-components branch December 10, 2024 00:10
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.

4 participants