Repository navigation
[resources] Add sort by date/relevance dropdown to Resources page #8073
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
Open
akshatsinghai6682-sketch
wants to merge
11
commits into
layer5io:master
Choose a base branch
from
akshatsinghai6682-sketch:sort-resources-feature
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+180
−14
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8884eec
feat(resources): add sort by date/relevance dropdown to Resources page
akshatsinghai6682-sketch 5096d97
Merge branch 'master' into sort-resources-feature
akshatsinghai6682-sketch 4194ea0
Merge branch 'master' into sort-resources-feature
akshatsinghai6682-sketch 3719a23
fix(resources): remove redundant max-width from search box
akshatsinghai6682-sketch bfb3752
fix(package): run resources-url tests alongside resource-sort tests
akshatsinghai6682-sketch f17b0ee
Merge branch 'master' into sort-resources-feature
akshatsinghai6682-sketch 25a1590
Merge branch 'master' into sort-resources-feature
akshatsinghai6682-sketch a8c1e1d
Merge branch 'master' into sort-resources-feature
akshatsinghai6682-sketch 5778244
Merge branch 'master' into sort-resources-feature
akshatsinghai6682-sketch 3232ce3
Merge branch 'master' into sort-resources-feature
akshatsinghai6682-sketch 179d3b4
Merge branch 'master' into sort-resources-feature
akshatsinghai6682-sketch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,7 @@ const DataWrapper = (WrappedComponent) => { | |
| } | ||
| fields { | ||
| slug | ||
| dateForSort | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| const getTime = (node) => { | ||
| const t = new Date(node.fields?.dateForSort).getTime(); | ||
| return Number.isNaN(t) || t <= 0 ? null : t; | ||
| }; | ||
|
|
||
| const sortResources = (nodes, sortOption) => { | ||
| if (sortOption === "relevance") return nodes; | ||
|
|
||
| const direction = sortOption === "oldest" ? 1 : -1; | ||
| return nodes.slice().sort((a, b) => { | ||
| const ta = getTime(a); | ||
| const tb = getTime(b); | ||
| if (ta === null && tb === null) return 0; | ||
| if (ta === null) return 1; | ||
| if (tb === null) return -1; | ||
| return direction * (ta - tb); | ||
| }); | ||
| }; | ||
|
|
||
| const paginateResources = (items, currentPage, postsPerPage) => { | ||
| if (!(postsPerPage > 0)) return items; | ||
| const last = currentPage * postsPerPage; | ||
| return items.slice(last - postsPerPage, last); | ||
| }; | ||
|
|
||
| module.exports = { getTime, sortResources, paginateResources }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| const test = require("node:test"); | ||
| const assert = require("node:assert/strict"); | ||
| const { sortResources, paginateResources } = require("./resource-sort"); | ||
|
|
||
| const node = (id, dateForSort) => ({ id, fields: { dateForSort } }); | ||
|
|
||
| const mixed = [ | ||
| node("b", "2024-02-01T00:00:00.000Z"), | ||
| node("missing", undefined), | ||
| node("c", "2025-03-01T00:00:00.000Z"), | ||
| node("invalid", "not-a-date"), | ||
| node("a", "2023-01-01T00:00:00.000Z"), | ||
| ]; | ||
|
|
||
| const ids = (list) => list.map((n) => n.id); | ||
|
|
||
| test("latest sorts newest first", () => { | ||
| assert.deepEqual(ids(sortResources(mixed, "latest")).slice(0, 3), ["c", "b", "a"]); | ||
| }); | ||
|
|
||
| test("oldest sorts oldest first", () => { | ||
| assert.deepEqual(ids(sortResources(mixed, "oldest")).slice(0, 3), ["a", "b", "c"]); | ||
| }); | ||
|
|
||
| test("missing or invalid dates are always last", () => { | ||
| for (const option of ["latest", "oldest"]) { | ||
| const tail = ids(sortResources(mixed, option)).slice(3); | ||
| assert.deepEqual(tail.sort(), ["invalid", "missing"]); | ||
| } | ||
| }); | ||
|
|
||
| test("relevance keeps the incoming order", () => { | ||
| assert.deepEqual(ids(sortResources(mixed, "relevance")), ids(mixed)); | ||
| }); | ||
|
|
||
| test("sorting does not mutate the input", () => { | ||
| const before = ids(mixed); | ||
| sortResources(mixed, "latest"); | ||
| assert.deepEqual(ids(mixed), before); | ||
| }); | ||
|
|
||
| const items = [1, 2, 3, 4, 5, 6, 7]; | ||
|
|
||
| test("pagination returns the right slice for each page", () => { | ||
| assert.deepEqual(paginateResources(items, 1, 3), [1, 2, 3]); | ||
| assert.deepEqual(paginateResources(items, 2, 3), [4, 5, 6]); | ||
| assert.deepEqual(paginateResources(items, 3, 3), [7]); | ||
| }); | ||
|
|
||
| test("pagination returns everything when postsPerPage is 0", () => { | ||
| assert.deepEqual(paginateResources(items, 1, 0), items); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.