Skip to content

Commit acd3fd3

Browse files
[resources] Add sort by date/relevance dropdown to Resources page
Signed-off-by: Akshat Singhai <akshatsinghai6682@gmail.com>
1 parent 23a0088 commit acd3fd3

3 files changed

Lines changed: 190 additions & 114 deletions

File tree

‎src/sections/Resources/Resources-grid/DataWrapper.js‎

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,48 @@ import LitePlaceholder from "../../../templates/lite-placeholder";
66

77
const DataWrapper = (WrappedComponent) => {
88
return (props) => {
9-
const data = useStaticQuery(
10-
graphql`query allResourcesAndAllResources {
11-
allMdx(
12-
sort: {frontmatter: {date: DESC}}
13-
filter: {fields: {collection: {in: ["blog", "resources", "news", "events"]}}, frontmatter: {published: {eq: true}, resource: {eq: true}}}
14-
) {
15-
nodes {
16-
id
17-
frontmatter {
18-
title
19-
type
20-
technology
21-
product
22-
mesh
23-
thumbnail {
24-
childImageSharp {
25-
gatsbyImageData(width: 480, layout: CONSTRAINED)
9+
const data = useStaticQuery(graphql`
10+
query allResourcesAndAllResources {
11+
allMdx(
12+
sort: { frontmatter: { date: DESC } }
13+
filter: {
14+
fields: {
15+
collection: { in: ["blog", "resources", "news", "events"] }
16+
}
17+
frontmatter: { published: { eq: true }, resource: { eq: true } }
2618
}
27-
extension
28-
publicURL
29-
}
30-
darkthumbnail {
31-
childImageSharp {
32-
gatsbyImageData(width: 480, layout: CONSTRAINED)
19+
) {
20+
nodes {
21+
id
22+
frontmatter {
23+
title
24+
date
25+
type
26+
technology
27+
product
28+
mesh
29+
thumbnail {
30+
childImageSharp {
31+
gatsbyImageData(width: 480, layout: CONSTRAINED)
32+
}
33+
extension
34+
publicURL
35+
}
36+
darkthumbnail {
37+
childImageSharp {
38+
gatsbyImageData(width: 480, layout: CONSTRAINED)
39+
}
40+
extension
41+
publicURL
42+
}
43+
}
44+
fields {
45+
slug
46+
}
3347
}
34-
extension
35-
publicURL
3648
}
3749
}
38-
fields {
39-
slug
40-
}
41-
}
42-
}
43-
}`
44-
);
50+
`);
4551

4652
if (data.allMdx.nodes.length === 0) {
4753
return (

‎src/sections/Resources/Resources-grid/index.js‎

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from "react";
1+
import React, { useState } from "react";
22
import Card from "../../../components/Card";
33
import { Row, Col } from "../../../reusecore/Layout";
44
import Pagination from "./paginate";
@@ -7,14 +7,46 @@ import EmptyResources from "../Resources-error/emptyStateTemplate";
77

88
import { ResourcePageWrapper } from "./resourceGrid.style";
99

10+
const toTimestamp = (raw) => {
11+
if (!raw) return 0;
12+
const str = String(raw).trim();
13+
const iso = str.replace(
14+
/^(\d{4}-\d{2}-\d{2})[ T](\d{2}:\d{2}:\d{2})\s*([+-])(\d{2}):?(\d{2})$/,
15+
"$1T$2$3$4:$5",
16+
);
17+
let t = new Date(iso).getTime();
18+
if (Number.isNaN(t)) {
19+
t = new Date(str.replace(/(\d)(st|nd|rd|th)\b/g, "$1")).getTime();
20+
}
21+
return Number.isNaN(t) ? 0 : t;
22+
};
23+
1024
const ResourceGrid = (props) => {
25+
const hasQuery = Boolean(props.searchQuery);
26+
const [sortOption, setSortOption] = useState(null);
27+
const effectiveSort = sortOption ?? (hasQuery ? "relevance" : "latest");
28+
29+
const sortResources = (nodes) => {
30+
if (effectiveSort === "relevance" && hasQuery) return nodes;
31+
32+
const direction = effectiveSort === "oldest" ? 1 : -1;
33+
return nodes
34+
.slice()
35+
.sort(
36+
(a, b) =>
37+
direction *
38+
(toTimestamp(a.frontmatter.date) - toTimestamp(b.frontmatter.date)),
39+
);
40+
};
41+
1142
// Get current posts
1243
const indexOfLastPost = props.currentPage * props.postsPerPage;
1344
const indexOfFirstPost = indexOfLastPost - props.postsPerPage;
45+
const sortedData = sortResources(props.data);
1446
const searchedResource =
1547
props.postsPerPage > 0
16-
? props.data.slice(indexOfFirstPost, indexOfLastPost)
17-
: props.data;
48+
? sortedData.slice(indexOfFirstPost, indexOfLastPost)
49+
: sortedData;
1850

1951
const paginate = (pageNumber) => {
2052
props.setCurrentPage(pageNumber);
@@ -29,6 +61,21 @@ const ResourceGrid = (props) => {
2961
<ResourcePageWrapper>
3062
<div className="resource-grid-wrapper">
3163
<div className="search">
64+
<div className="sortBox">
65+
<select
66+
className="sortDropdown"
67+
aria-label="Sort by"
68+
value={effectiveSort}
69+
onChange={(e) => {
70+
setSortOption(e.target.value);
71+
props.setCurrentPage(1);
72+
}}
73+
>
74+
<option value="latest">Latest</option>
75+
<option value="oldest">Oldest</option>
76+
<option value="relevance">Relevance</option>
77+
</select>
78+
</div>
3279
<div className="searchBox">
3380
<SearchBox
3481
searchQuery={props.searchQuery}
@@ -78,5 +125,4 @@ const ResourceGrid = (props) => {
78125
</ResourcePageWrapper>
79126
);
80127
};
81-
82128
export default ResourceGrid;
Lines changed: 102 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,126 @@
11
import styled from "styled-components";
22
export const ResourcePageWrapper = styled.div`
3+
.resource-grid-wrapper {
4+
margin: 0.8rem 0 1.5rem 1.5rem;
5+
}
36
4-
.resource-grid-wrapper{
5-
margin: 0.8rem 0 1.5rem 1.5rem;
7+
.search {
8+
display: flex;
9+
justify-content: flex-end;
10+
width: 100%;
11+
12+
margin-bottom: 0.8rem;
13+
.searchBox {
14+
flex: 0 0 50%;
15+
@media only screen and (max-width: 990px) {
16+
flex: 0 0 100%;
17+
max-width: 100%;
18+
}
619
}
20+
.sortBox {
21+
flex: 0 0 auto;
22+
margin-right: 1rem;
23+
.sortDropdown {
24+
padding: 15px 40px 15px 15px;
25+
border-radius: 5px;
26+
border: 1px solid ${(props) => props.theme.headingColor};
27+
background-color: ${(props) => props.theme.shadowDarkColor};
28+
color: ${(props) => props.theme.text};
29+
font-size: 0.9rem;
30+
cursor: pointer;
31+
appearance: none;
32+
-webkit-appearance: none;
33+
-moz-appearance: none;
34+
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");
35+
background-repeat: no-repeat;
36+
background-position: right 12px center;
37+
background-size: 16px;
738
8-
.search{
9-
display:flex;
10-
justify-content:flex-end;
11-
width:100%;
12-
13-
margin-bottom: 0.8rem;
14-
.searchBox{
15-
flex:0 0 50%;
16-
@media only screen and (max-width:990px){
17-
18-
flex:0 0 100%;
19-
max-width:100%;
20-
21-
}
39+
&:focus {
40+
outline: 2px solid #00b39f;
41+
outline-offset: 2px;
2242
}
23-
24-
}
25-
.post-content-block{
26-
height: 7rem;
27-
}
28-
.post-thumb-block{
29-
height: 10.5rem;
43+
}
44+
@media only screen and (max-width: 990px) {
45+
display: none;
46+
}
3047
}
31-
.post-meta-block{
32-
p{
33-
margin:0;
34-
}
48+
}
49+
.post-content-block {
50+
height: 7rem;
51+
}
52+
.post-thumb-block {
53+
height: 10.5rem;
54+
}
55+
.post-meta-block {
56+
p {
57+
margin: 0;
3558
}
36-
.btn-container {
37-
display: flex;
38-
justify-content: center;
39-
align-items: center;
40-
gap: 1rem;
41-
margin: 3rem auto 4rem;
59+
}
60+
.btn-container {
61+
display: flex;
62+
justify-content: center;
63+
align-items: center;
64+
gap: 1rem;
65+
margin: 3rem auto 4rem;
4266
43-
@media screen and (max-width: 768px) {
44-
flex-direction: column;
45-
gap: 1rem;
46-
margin: 2rem auto 3rem;
47-
}
48-
}
67+
@media screen and (max-width: 768px) {
68+
flex-direction: column;
69+
gap: 1rem;
70+
margin: 2rem auto 3rem;
71+
}
72+
}
4973
50-
.nav-btn {
51-
min-width: 130px;
52-
padding: 0.75rem 1.5rem;
53-
font-size: 1rem;
54-
transition: all 0.3s ease;
74+
.nav-btn {
75+
min-width: 130px;
76+
padding: 0.75rem 1.5rem;
77+
font-size: 1rem;
78+
transition: all 0.3s ease;
5579
56-
&:disabled {
57-
opacity: 0.5;
58-
cursor: not-allowed;
59-
}
80+
&:disabled {
81+
opacity: 0.5;
82+
cursor: not-allowed;
83+
}
6084
61-
@media screen and (max-width: 768px) {
62-
min-width: 160px;
63-
padding: 0.75rem 1.75rem;
64-
}
65-
}
85+
@media screen and (max-width: 768px) {
86+
min-width: 160px;
87+
padding: 0.75rem 1.75rem;
88+
}
89+
}
6690
67-
@media only screen and (max-width: 575px) {
68-
.resource-grid-wrapper{
69-
margin: 0 auto 5rem;
70-
}
91+
@media only screen and (max-width: 575px) {
92+
.resource-grid-wrapper {
93+
margin: 0 auto 5rem;
7194
}
95+
}
7296
73-
.no-resources-page{
97+
.no-resources-page {
7498
padding: 1rem 1rem 4rem;
7599
margin: 1rem;
76-
100+
77101
box-shadow: 0px 3px 10px 1px rgba(0, 179, 159, 0.5);
78102
.errorMessage {
79-
font-size: 2rem;
80-
line-height: 2rem;
103+
font-size: 2rem;
104+
line-height: 2rem;
81105
}
82-
.error-text{
83-
margin-top: 5rem;
106+
.error-text {
107+
margin-top: 5rem;
84108
}
85-
.errorSubtitle {
86-
font-weight: 400;
87-
font-size: 1.5rem;
88-
color: gray;
89-
font-style: italic;
90-
margin-top: 2.5rem;
109+
.errorSubtitle {
110+
font-weight: 400;
111+
font-size: 1.5rem;
112+
color: gray;
113+
font-style: italic;
114+
margin-top: 2.5rem;
91115
}
92-
img{
93-
display: block;
94-
margin:auto;
95-
margin-top: 3.125rem;
96-
width: 14rem;
97-
@media only screen and (max-width:700px){
98-
max-width: 100%;
99-
}
100-
}
116+
img {
117+
display: block;
118+
margin: auto;
119+
margin-top: 3.125rem;
120+
width: 14rem;
121+
@media only screen and (max-width: 700px) {
122+
max-width: 100%;
123+
}
101124
}
125+
}
102126
`;

0 commit comments

Comments
 (0)