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

[Bug]: @adminjs/relations default sorting not working in oneToManyHandler #1664

Open
Pekket opened this issue May 6, 2024 · 0 comments
Open
Labels
bug Something isn't working

Comments

@Pekket
Copy link

Pekket commented May 6, 2024

Contact Details

No response

What happened?

Im trying to sort my oneToMany table on configured sort options.

export const requestLogsResource = (prisma: PrismaClient) => ({
  resource: {
    model: getModelByName('request_logs'),
    client: prisma,
  },
  features: [targetRelationSettingsFeature()],
  options: {
    id: 'requestLogs',
    navigation: false,
    sort: {
      direction: 'desc',
      sortBy: 'timestamp',
    },
    listProperties: ['timestamp', 'question'],
... 
image

Sorting is only working when clicking on the header table of timestamp. This because query parameters are added to the url.
When investigating this problem i found that oneToManyHandler (@adminjs/relations/lib/actions/one-to-many/one-to-many.handler.js) is defaulting its query parameter direction to asc.

export const oneToManyHandler = async (a, b, c = {}, d) => {
  const {
    relation: e,
    targetResource: f,
  } = b, {
    sortBy: g,
    direction: h = 'asc',
    filters: i = {},
    perPage: j = d._admin.options.settings?.defaultPerPage || 10,
    page: k = 1,
  } = c;

This causes the sortSetter (adminjs/src/backend/services/sort-setter/sort-setter) to always default back to asc in code.
const computedDirection = direction || options.direction || DEFAULT_DIRECTION

🚀🚀🚀
Proposed solution: remove default of direction in oneToManyHandler function.

Bug prevalence

Visting overview page with relations

AdminJS dependencies version

"@adminjs/express": "^6.1.0",
"@adminjs/prisma": "^5.0.1",
"@adminjs/relations": "^1.1.2",
"@prisma/client": "^5.6.0",
"adminjs": "^7.8.1",

What browsers do you see the problem on?

No response

Relevant log output

No response

Relevant code that's giving you issues

No response

@Pekket Pekket added the bug Something isn't working label May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant