Skip to content

Conversation

ChristopherChudzicki
Copy link
Contributor

What are the relevant tickets?

Closes https://github.com/mitodl/hq/issues/6278

Description (What does it do?)

Fixes some sorting we did on the frontend to handle numbers properly.

How can this be tested?

  1. Run ./manage.py populate_featured_lists management command (I changed the default to 12, which is better for our carousels in general). Note: This will override your featured lists. If you want to keep them, manually add at least 12 items to one of them.
    • must use at least 12 items (or 11, actually) to test the lexicographical (string vs number) sorting issue.
  2. Visit http://learn.odl.local:8062/c/unit/ocw
    • The featured list carousel items should be in the same order as shown in API response from http://api.learn.odl.local:8065/api/v1/featured/?offered_by=ocw&limit=12 ( curl http://api.learn.odl.local:8065/api/v1/featured/\?offered_by\=ocw\&limit\=12 | jq '.results.[].title')
    • ⚠️ NOTE: The featured list API responses are cached by django for anonymous users, which includes the NextJS backend and your terminal if using curl above. You may need to restart the docker containers or clear the cache some other way.

})
Object.keys(resultsByPosition)
.sort(
(a, b) => Number(a) - Number(b), // Sort positions numerically
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the only functional change vs the old implementation. But I decided to move it to a separate file and have copilot add some tests.

Comment on lines +71 to +72
const obj = { id: "test", position: 5 }
expect(hasPosition(obj)).toBe(true)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

copilot really wanted to add this test......

@ChristopherChudzicki ChristopherChudzicki changed the title Cc/fix featured list sorting Fix featured list channel sorting Oct 1, 2025
resultsByPosition[result?.position ?? ""]?.push(result)
})
Object.keys(resultsByPosition)
.sort()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Explanation: Object keys are strings, and these were stringified numbers, so they were being sorted like 0, 1, 10, 11, 2, 3, 4, ....

This wasn't a visible problem on the homepage because that carusel uses items from every offeror... to get up to 12 items, that carousel only needs positions 0, 1, 2 from all five offerors.

The featured list query is also used on channel pages (We don't query the learning path directly because, at least right now, those are private; only visible to non-staff via /featured endpoint). And on the channel pages, 12 items are shown, so the sorting was getting messed up.

@ChristopherChudzicki ChristopherChudzicki added the Needs Review An open Pull Request that is ready for review label Oct 1, 2025
Copy link
Contributor

@jonkafton jonkafton left a comment

Choose a reason for hiding this comment

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

👍

.mockImplementation(() => faker.number.float({ min: 0, max: 1 }))

describe("randomizeGroups", () => {
it("should group by position and randomize within groups with duplicates", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Technically does not test that items are randomized within their groups (though not very testable!)

Comment on lines +1 to +3
const hasPosition = <T extends { position: number | null }>(
r: T,
): r is T & { position: number } => r.position !== null
Copy link
Contributor

Choose a reason for hiding this comment

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

Type guard+narrowing... nice!

@ChristopherChudzicki ChristopherChudzicki merged commit 00a104d into main Oct 2, 2025
13 checks passed
@ChristopherChudzicki ChristopherChudzicki deleted the cc/fix-featured-list-sorting branch October 2, 2025 15:31
@odlbot odlbot mentioned this pull request Oct 2, 2025
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Review An open Pull Request that is ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants