Skip to content

Commit f309a1c

Browse files
author
Shariq
committed
Added Search and Filter Features
1 parent e57909a commit f309a1c

File tree

1 file changed

+46
-7
lines changed

1 file changed

+46
-7
lines changed

src/app/(pages)/whitepapers/page.jsx

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ import React, { useState, useEffect } from 'react';
33

44
const WhitepapersPage = () => {
55
const [whitepapers, setWhitepapers] = useState([]);
6+
const [searchQuery, setSearchQuery] = useState('');
7+
const [filters, setFilters] = useState({
8+
featured: false,
9+
ai: false,
10+
cloudBasics: false,
11+
});
612

13+
// Fetch whitepapers data
714
useEffect(() => {
815
const fetchWhitepapers = async () => {
916
try {
@@ -17,6 +24,33 @@ const WhitepapersPage = () => {
1724

1825
fetchWhitepapers();
1926
}, []);
27+
28+
// Filter whitepapers based on search query and selected filters
29+
const filteredWhitepapers = whitepapers
30+
.map(section => ({
31+
...section,
32+
items: section.items.filter(item => {
33+
const matchesSearch = item.title.toLowerCase().includes(searchQuery.toLowerCase()) ||
34+
item.description.toLowerCase().includes(searchQuery.toLowerCase());
35+
36+
const matchesFilters = (
37+
(filters.featured && section.category === 'Featured Whitepapers') ||
38+
(filters.ai && section.category === 'AI and ML') ||
39+
(filters.cloudBasics && section.category === 'Cloud Basics') ||
40+
!Object.values(filters).includes(true)
41+
);
42+
43+
return matchesSearch && matchesFilters;
44+
})
45+
}))
46+
.filter(section => section.items.length > 0); // Remove sections with no matching items
47+
48+
// Handle filter change
49+
const handleFilterChange = (e) => {
50+
const { id, checked } = e.target;
51+
setFilters(prev => ({ ...prev, [id]: checked }));
52+
};
53+
2054
return (
2155
<div className="bg-gray-50 min-h-screen p-6">
2256
{/* Header Section */}
@@ -41,29 +75,34 @@ const WhitepapersPage = () => {
4175
<h2 className="text-lg font-semibold mt-6 mb-4">Filter by</h2>
4276
<div className="space-y-2">
4377
<div className="flex items-center space-x-2">
44-
<input type="checkbox" id="featured" className="w-4 h-4" />
78+
<input type="checkbox" id="featured" checked={filters.featured} onChange={handleFilterChange} className="w-4 h-4" />
4579
<label htmlFor="featured" className="text-gray-700">Featured Whitepapers</label>
4680
</div>
4781
<div className="flex items-center space-x-2">
48-
<input type="checkbox" id="ai" className="w-4 h-4" />
82+
<input type="checkbox" id="ai" checked={filters.ai} onChange={handleFilterChange} className="w-4 h-4" />
4983
<label htmlFor="ai" className="text-gray-700">AI and ML</label>
5084
</div>
5185
<div className="flex items-center space-x-2">
52-
<input type="checkbox" id="cloud-basics" className="w-4 h-4" />
53-
<label htmlFor="cloud-basics" className="text-gray-700">Cloud Basics</label>
86+
<input type="checkbox" id="cloudBasics" checked={filters.cloudBasics} onChange={handleFilterChange} className="w-4 h-4" />
87+
<label htmlFor="cloudBasics" className="text-gray-700">Cloud Basics</label>
5488
</div>
55-
{/* Add more filters as needed */}
5689
</div>
5790
</aside>
5891

5992
{/* Main Content */}
6093
<section className="w-full lg:w-3/4">
6194
<div className="flex items-center space-x-4 mb-6">
62-
<input type="text" placeholder="Search" className="w-full px-4 py-2 border rounded-lg shadow-sm focus:outline-none" />
95+
<input
96+
type="text"
97+
placeholder="Search"
98+
value={searchQuery}
99+
onChange={(e) => setSearchQuery(e.target.value)}
100+
className="w-full px-4 py-2 border rounded-lg shadow-sm focus:outline-none"
101+
/>
63102
<button className="bg-blue-600 text-white px-4 py-2 rounded-md font-semibold">Search</button>
64103
</div>
65104

66-
{whitepapers.map((section, index) => (
105+
{filteredWhitepapers.map((section, index) => (
67106
<div key={index} className="mb-8">
68107
<h2 className="text-2xl font-bold mb-4">{section.category}</h2>
69108
<div className="grid md:grid-cols-2 lg:grid-cols-3 gap-6">

0 commit comments

Comments
 (0)