Skip to content

Commit b1f57cb

Browse files
Merge pull request #8589 from SakshamMawari/sort-filter
[Resources] Implementing the sort button on resources page Layer5
2 parents cbfe738 + 17feb47 commit b1f57cb

4 files changed

Lines changed: 249 additions & 134 deletions

File tree

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,41 @@
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";
55
import SearchBox from "../../../reusecore/Search";
6+
import SortBox from "./sortbox";
67
import EmptyResources from "../Resources-error/emptyStateTemplate";
78

89
import { ResourcePageWrapper } from "./resourceGrid.style";
910

11+
const sortOptions = [
12+
{ value: "recent", label: "Recent" },
13+
{ value: "az", label: "A-Z" },
14+
{ value: "za", label: "Z-A" },
15+
];
1016
const ResourceGrid = (props) => {
17+
const [sortBy, setSortBy] = useState("recent");
18+
const handleSortByChange = (value) => {
19+
setSortBy(value);
20+
props.setCurrentPage(1);
21+
};
22+
const sortedData = [...props.data].sort((a, b) => {
23+
const titleA = a.frontmatter?.title ?? "";
24+
const titleB = b.frontmatter?.title ?? "";
25+
const dateA = new Date(a.frontmatter?.date).getTime() || 0;
26+
const dateB = new Date(b.frontmatter?.date).getTime() || 0;
27+
if (sortBy === "az") return titleA.localeCompare(titleB);
28+
if (sortBy === "za") return titleB.localeCompare(titleA);
29+
return dateB - dateA;
30+
});
31+
1132
// Get current posts
1233
const indexOfLastPost = props.currentPage * props.postsPerPage;
1334
const indexOfFirstPost = indexOfLastPost - props.postsPerPage;
1435
const searchedResource =
1536
props.postsPerPage > 0
16-
? props.data.slice(indexOfFirstPost, indexOfLastPost)
17-
: props.data;
37+
? sortedData.slice(indexOfFirstPost, indexOfLastPost)
38+
: sortedData;
1839

1940
const paginate = (pageNumber) => {
2041
props.setCurrentPage(pageNumber);
@@ -29,6 +50,14 @@ const ResourceGrid = (props) => {
2950
<ResourcePageWrapper>
3051
<div className="resource-grid-wrapper">
3152
<div className="search">
53+
<div className="sort-by">
54+
<SortBox
55+
id="resource-sort"
56+
value={sortBy}
57+
onChange={handleSortByChange}
58+
options={sortOptions}
59+
/>
60+
</div>
3261
<div className="searchBox">
3362
<SearchBox
3463
searchQuery={props.searchQuery}
Lines changed: 141 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,164 +1,174 @@
11
import styled from "styled-components";
22
export const ResourcePageWrapper = styled.div`
3-
4-
.resource-grid-wrapper{
5-
margin: 0.8rem 0 1.5rem 1.5rem;
3+
.resource-grid-wrapper {
4+
margin: 0.8rem 0 1.5rem 1.5rem;
5+
}
6+
7+
.search {
8+
display: flex;
9+
justify-content: space-around;
10+
align-items: center;
11+
gap: 1rem;
12+
width: 100%;
13+
margin-bottom: 0.8rem;
14+
15+
.sort-by {
16+
flex: 0 0 30%;
17+
min-width: 0;
618
}
7-
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-
}
22-
}
23-
19+
.searchBox {
20+
flex: 0 0 50%;
21+
min-width: 0;
2422
}
25-
.post-content-block{
26-
height: 7rem;
23+
@media only screen and (max-width: 990px) {
24+
flex-direction: column;
25+
align-items: stretch;
26+
27+
.sort-by,
28+
.searchBox {
29+
flex: 0 0 100%;
30+
max-width: 100%;
31+
width: 100%;
32+
}
2733
}
28-
.post-thumb-block{
29-
height: 10.5rem;
34+
}
35+
.post-content-block {
36+
height: 7rem;
37+
}
38+
.post-thumb-block {
39+
height: 10.5rem;
40+
}
41+
.post-meta-block {
42+
p {
43+
margin: 0;
3044
}
31-
.post-meta-block{
32-
p{
33-
margin:0;
34-
}
45+
}
46+
.btn-container {
47+
display: flex;
48+
justify-content: center;
49+
align-items: center;
50+
gap: 1rem;
51+
margin: 3rem auto 4rem;
52+
53+
@media screen and (max-width: 768px) {
54+
flex-direction: column;
55+
gap: 1rem;
56+
margin: 2rem auto 3rem;
3557
}
36-
.btn-container {
37-
display: flex;
38-
justify-content: center;
39-
align-items: center;
40-
gap: 1rem;
41-
margin: 3rem auto 4rem;
42-
43-
@media screen and (max-width: 768px) {
44-
flex-direction: column;
45-
gap: 1rem;
46-
margin: 2rem auto 3rem;
47-
}
48-
}
58+
}
4959
50-
.nav-btn {
51-
min-width: 130px;
52-
padding: 0.75rem 1.5rem;
53-
font-size: 1rem;
54-
transition: all 0.3s ease;
60+
.nav-btn {
61+
min-width: 130px;
62+
padding: 0.75rem 1.5rem;
63+
font-size: 1rem;
64+
transition: all 0.3s ease;
5565
56-
&:disabled {
57-
opacity: 0.5;
58-
cursor: not-allowed;
59-
}
66+
&:disabled {
67+
opacity: 0.5;
68+
cursor: not-allowed;
69+
}
6070
61-
@media screen and (max-width: 768px) {
62-
min-width: 160px;
63-
padding: 0.75rem 1.75rem;
64-
}
65-
}
71+
@media screen and (max-width: 768px) {
72+
min-width: 160px;
73+
padding: 0.75rem 1.75rem;
74+
}
75+
}
6676
67-
@media only screen and (max-width: 575px) {
68-
.resource-grid-wrapper{
69-
margin: 0 auto 5rem;
70-
}
77+
@media only screen and (max-width: 575px) {
78+
.resource-grid-wrapper {
79+
margin: 0 auto 5rem;
7180
}
81+
}
7282
73-
.no-resources-page{
83+
.no-resources-page {
7484
padding: 1rem 1rem 4rem;
7585
margin: 1rem;
76-
86+
7787
box-shadow: 0px 3px 10px 1px rgba(0, 179, 159, 0.5);
7888
7989
.empty-state-row {
80-
flex-wrap: wrap;
90+
flex-wrap: wrap;
8191
}
8292
8393
.errorMessage {
84-
font-size: 2rem;
85-
line-height: 2rem;
86-
overflow-wrap: break-word;
87-
word-break: break-word;
94+
font-size: 2rem;
95+
line-height: 2rem;
96+
overflow-wrap: break-word;
97+
word-break: break-word;
8898
}
89-
.error-text{
90-
margin-top: 5rem;
91-
overflow-wrap: break-word;
92-
word-break: break-word;
99+
.error-text {
100+
margin-top: 5rem;
101+
overflow-wrap: break-word;
102+
word-break: break-word;
93103
}
94-
.errorSubtitle {
95-
font-weight: 400;
96-
font-size: 1.5rem;
97-
color: gray;
98-
font-style: italic;
99-
margin-top: 2.5rem;
100-
overflow-wrap: break-word;
101-
word-break: break-word;
104+
.errorSubtitle {
105+
font-weight: 400;
106+
font-size: 1.5rem;
107+
color: gray;
108+
font-style: italic;
109+
margin-top: 2.5rem;
110+
overflow-wrap: break-word;
111+
word-break: break-word;
112+
}
113+
img {
114+
display: block;
115+
margin: auto;
116+
margin-top: 3.125rem;
117+
width: 14rem;
118+
max-width: 100%;
119+
height: auto;
120+
object-fit: contain;
102121
}
103-
img{
104-
display: block;
105-
margin:auto;
106-
margin-top: 3.125rem;
107-
width: 14rem;
108-
max-width: 100%;
109-
height: auto;
110-
object-fit: contain;
111-
}
112122
113123
@media only screen and (max-width: 992px) {
114-
padding: 2rem 1.5rem 3rem;
124+
padding: 2rem 1.5rem 3rem;
125+
text-align: center;
126+
127+
.empty-state-row {
128+
justify-content: center;
129+
}
130+
131+
img {
132+
margin: 1rem auto 0;
133+
width: 12rem;
134+
max-width: 100%;
135+
height: auto;
136+
}
137+
138+
.error-text {
139+
margin-top: 1.5rem;
115140
text-align: center;
141+
}
116142
117-
.empty-state-row {
118-
justify-content: center;
119-
}
120-
121-
img {
122-
margin: 1rem auto 0;
123-
width: 12rem;
124-
max-width: 100%;
125-
height: auto;
126-
}
127-
128-
.error-text {
129-
margin-top: 1.5rem;
130-
text-align: center;
131-
}
132-
133-
.errorMessage {
134-
font-size: 1.5rem;
135-
line-height: 1.3;
136-
}
137-
138-
.errorSubtitle {
139-
font-size: 1.15rem;
140-
line-height: 1.4;
141-
margin-top: 1rem;
142-
}
143+
.errorMessage {
144+
font-size: 1.5rem;
145+
line-height: 1.3;
146+
}
147+
148+
.errorSubtitle {
149+
font-size: 1.15rem;
150+
line-height: 1.4;
151+
margin-top: 1rem;
152+
}
143153
}
144154
145155
@media only screen and (max-width: 575px) {
146-
padding: 1.5rem 1rem 2.5rem;
147-
margin: 0.5rem;
148-
149-
img {
150-
width: 9rem;
151-
max-width: 100%;
152-
}
153-
154-
.errorMessage {
155-
font-size: 1.25rem;
156-
}
157-
158-
.errorSubtitle {
159-
font-size: 1rem;
160-
margin-top: 0.75rem;
161-
}
162-
}
156+
padding: 1.5rem 1rem 2.5rem;
157+
margin: 0.5rem;
158+
159+
img {
160+
width: 9rem;
161+
max-width: 100%;
162+
}
163+
164+
.errorMessage {
165+
font-size: 1.25rem;
166+
}
167+
168+
.errorSubtitle {
169+
font-size: 1rem;
170+
margin-top: 0.75rem;
171+
}
163172
}
173+
}
164174
`;

0 commit comments

Comments
 (0)