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

Lexorank Collision #4

Closed
zakinadhif opened this issue Oct 16, 2023 · 2 comments
Closed

Lexorank Collision #4

zakinadhif opened this issue Oct 16, 2023 · 2 comments

Comments

@zakinadhif
Copy link

zakinadhif commented Oct 16, 2023

Hello!

I've been trying to replicate some of the mechanisms which are used here for my project. I've found which appears to be a strange behavior on the generateOrderRank hook, and I'm not sure if this is the expected output.

Basically, it appears that the hook gets stuck and generates the same lexorank multiple times because it received an input from a wrongly sorted query?

image

  const collectionData = await payload.find({
    collection: 'menu-items',
    sort: 'orderRank',
    limit: 1,
  });

  const lastOrderRank = collectionData.docs[0]?.orderRank ?? null;

  console.log("Last order rank:", lastOrderRank)

  const lastRank =
    lastOrderRank && typeof lastOrderRank === 'string'
      ? LexoRank.parse(lastOrderRank)
      : LexoRank.min()

  console.log("Last rank:", lastRank.toString());

  const nextRank = lastRank.genNext().genNext();
  data.orderRank = nextRank.toString();

  console.log("Next rank:", nextRank.toString());

image

I suspect it might be happening here too, since my code is nearly identical since I copied from here. The problem disappears when I flip the order of the sorting as such:

  sort: '-orderRank'

Thanks for making the plugin! I'll be using this as a reference on making my custom tree collection.

@DennisSnijder
Copy link
Owner

Interesting find! I'll take a look later this week! Thanks for the report!

@DennisSnijder
Copy link
Owner

@zakinadhif Thank you for looking into this! This bug actually resolved into the user being unable to sort their documents in some cases. This resolved that! 🥳

The fix is available in 0.3.2

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

No branches or pull requests

2 participants