From 8884eecefb90e14e331912825a5031eefac0be05 Mon Sep 17 00:00:00 2001 From: Akshat Singhai Date: Wed, 4 Nov 2026 23:09:29 +0530 Subject: [PATCH 1/3] feat(resources): add sort by date/relevance dropdown to Resources page Signed-off-by: Akshat Singhai --- package.json | 2 +- .../Resources/Resources-grid/DataWrapper.js | 1 + .../Resources/Resources-grid/index.js | 48 +++++++++++--- .../Resources-grid/resourceGrid.style.js | 66 +++++++++++++++++-- src/utils/resource-sort.js | 26 ++++++++ src/utils/resource-sort.test.js | 52 +++++++++++++++ 6 files changed, 181 insertions(+), 14 deletions(-) create mode 100644 src/utils/resource-sort.js create mode 100644 src/utils/resource-sort.test.js diff --git a/package.json b/package.json index 2184bc58a44b96..dcccb80db9447f 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "lint": "eslint --fix .", "checklint": "eslint .", "check:contributing-versions": "node .github/scripts/check-contributing-versions.cjs", - "test:unit": "node --test src/utils/build-collections.test.js src/utils/page-path-guard.test.js", + "test:unit": "node --test src/utils/build-collections.test.js src/utils/page-path-guard.test.js src/utils/resource-sort.test.js", "pretest": "eslint --ignore-path .gitignore .", "deploy": "gatsby build && gh-pages -d public -b master", "noIndex": "gatsby build && echo 'User-agent: *\nDisallow: /' > public/robots.txt", diff --git a/src/sections/Resources/Resources-grid/DataWrapper.js b/src/sections/Resources/Resources-grid/DataWrapper.js index bad95e5053dad3..9941323462d876 100644 --- a/src/sections/Resources/Resources-grid/DataWrapper.js +++ b/src/sections/Resources/Resources-grid/DataWrapper.js @@ -37,6 +37,7 @@ const DataWrapper = (WrappedComponent) => { } fields { slug + dateForSort } } } diff --git a/src/sections/Resources/Resources-grid/index.js b/src/sections/Resources/Resources-grid/index.js index 457372829e6fb4..8c706f05e1e51a 100644 --- a/src/sections/Resources/Resources-grid/index.js +++ b/src/sections/Resources/Resources-grid/index.js @@ -1,20 +1,35 @@ -import React from "react"; +import React, { useState, useEffect } from "react"; import Card from "../../../components/Card"; import { Row, Col } from "../../../reusecore/Layout"; import Pagination from "./paginate"; import SearchBox from "../../../reusecore/Search"; import EmptyResources from "../Resources-error/emptyStateTemplate"; +import { sortResources, paginateResources } from "../../../utils/resource-sort"; import { ResourcePageWrapper } from "./resourceGrid.style"; const ResourceGrid = (props) => { + const query = props.searchQuery || ""; + const hasQuery = Boolean(query); + const [choice, setChoice] = useState(null); + const effectiveSort = + choice && choice.query === query + ? choice.value + : hasQuery + ? "relevance" + : "latest"; + + useEffect(() => { + setChoice(null); + }, [query]); + // Get current posts - const indexOfLastPost = props.currentPage * props.postsPerPage; - const indexOfFirstPost = indexOfLastPost - props.postsPerPage; - const searchedResource = - props.postsPerPage > 0 - ? props.data.slice(indexOfFirstPost, indexOfLastPost) - : props.data; + const sortedData = sortResources(props.data, effectiveSort); + const searchedResource = paginateResources( + sortedData, + props.currentPage, + props.postsPerPage + ); const paginate = (pageNumber) => { props.setCurrentPage(pageNumber); @@ -29,6 +44,23 @@ const ResourceGrid = (props) => {
+
+ +
{ ); }; -export default ResourceGrid; +export default ResourceGrid; \ No newline at end of file diff --git a/src/sections/Resources/Resources-grid/resourceGrid.style.js b/src/sections/Resources/Resources-grid/resourceGrid.style.js index 5ce57d05329451..1b2bf3433c5922 100644 --- a/src/sections/Resources/Resources-grid/resourceGrid.style.js +++ b/src/sections/Resources/Resources-grid/resourceGrid.style.js @@ -8,16 +8,72 @@ export const ResourcePageWrapper = styled.div` .search{ display:flex; justify-content:flex-end; + align-items:center; width:100%; margin-bottom: 0.8rem; .searchBox{ - flex:0 0 50%; + flex: 0 1 400px; + min-width:0; + & > div, + .search-box{ + width: 100%; + max-width: 100%; + } @media only screen and (max-width:990px){ - - flex:0 0 100%; - max-width:100%; - + flex: 1 1 auto; + min-width: 0; + .search-box{ + width: 100%; + } + } + } + .sortBox { + align-self: stretch; + margin-right: 1rem; + @media only screen and (max-width: 600px) { + margin-right: 0.5rem; + } + .sortDropdown { + height: 100%; + padding: 15px 40px 15px 15px; + border-radius: 5px; + border: 1px solid ${(props) => props.theme.headingColor}; + background-color: ${(props) => props.theme.shadowDarkColor}; + color: ${(props) => props.theme.text}; + font-size: 0.9rem; + cursor: pointer; + appearance: none; + -webkit-appearance: none; + -moz-appearance: none; + background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e"); + background-repeat: no-repeat; + background-position: right 12px center; + background-size: 16px; + + &:focus { + outline: 2px solid #00b39f; + outline-offset: 2px; + } + + option { + color: ${(props) => props.theme.text}; + background-color: ${(props) => props.theme.shadowDarkColor}; + } + + @media only screen and (max-width: 600px) { + width: 48px; + height:48px; + padding: 0; + font-size:0; + color: transparent; + background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2' stroke-linecap='round'%3e%3cline x1='4' y1='7' x2='20' y2='7'/%3e%3cline x1='4' y1='12' x2='15' y2='12'/%3e%3cline x1='4' y1='17' x2='10' y2='17'/%3e%3c/svg%3e"); + background-position: center; + background-size: 22px; + option { + font-size: 0.9rem; + } + } } } diff --git a/src/utils/resource-sort.js b/src/utils/resource-sort.js new file mode 100644 index 00000000000000..5e5a6ef168e873 --- /dev/null +++ b/src/utils/resource-sort.js @@ -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 }; \ No newline at end of file diff --git a/src/utils/resource-sort.test.js b/src/utils/resource-sort.test.js new file mode 100644 index 00000000000000..2d941555df015e --- /dev/null +++ b/src/utils/resource-sort.test.js @@ -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); +}); \ No newline at end of file From 3719a237da1edb750ae1a83cd45b984a1c5b2ab4 Mon Sep 17 00:00:00 2001 From: Akshat Singhai Date: Thu, 5 Nov 2026 23:21:45 +0530 Subject: [PATCH 2/3] fix(resources): remove redundant max-width from search box Signed-off-by: Akshat Singhai --- src/sections/Resources/Resources-grid/resourceGrid.style.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/sections/Resources/Resources-grid/resourceGrid.style.js b/src/sections/Resources/Resources-grid/resourceGrid.style.js index 1b2bf3433c5922..de9fedfc204a5f 100644 --- a/src/sections/Resources/Resources-grid/resourceGrid.style.js +++ b/src/sections/Resources/Resources-grid/resourceGrid.style.js @@ -18,7 +18,6 @@ export const ResourcePageWrapper = styled.div` & > div, .search-box{ width: 100%; - max-width: 100%; } @media only screen and (max-width:990px){ flex: 1 1 auto; From bfb3752393db1e4857cd9584cb67c1720324ce1b Mon Sep 17 00:00:00 2001 From: Akshat Singhai Date: Thu, 5 Nov 2026 23:30:15 +0530 Subject: [PATCH 3/3] fix(package): run resources-url tests alongside resource-sort tests Signed-off-by: Akshat Singhai --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index dcccb80db9447f..b218802778a2fc 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "lint": "eslint --fix .", "checklint": "eslint .", "check:contributing-versions": "node .github/scripts/check-contributing-versions.cjs", - "test:unit": "node --test src/utils/build-collections.test.js src/utils/page-path-guard.test.js src/utils/resource-sort.test.js", + "test:unit": "node --test src/utils/build-collections.test.js src/utils/page-path-guard.test.js src/utils/resources-url.test.js src/utils/resource-sort.test.js", "pretest": "eslint --ignore-path .gitignore .", "deploy": "gatsby build && gh-pages -d public -b master", "noIndex": "gatsby build && echo 'User-agent: *\nDisallow: /' > public/robots.txt",