Skip to content

Commit 401e824

Browse files
committed
add search function
1 parent a806d01 commit 401e824

File tree

3 files changed

+90
-18
lines changed

3 files changed

+90
-18
lines changed

src/App.jsx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import React, { useEffect, useState } from 'react';
22
// import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
33
import './App.css';
4+
import './index.css';
45
import {
56
BrowserRouter as Router,
67
Routes,
78
Route,
89
Navigate,
910
useLocation
1011
} from 'react-router-dom';
11-
12+
import SearchResults from './components/search';
1213
import Login from './components/login';
1314
import SignUp from './components/register';
1415
import Home from './components/home';
@@ -31,17 +32,17 @@ function App() {
3132

3233
return () => unsubscribe(); // Clean up on unmount
3334
}, []);
34-
// Handle redirects from GitHub Pages 404 fallback
35-
// function RedirectWithState() {
36-
// const location = useLocation();
37-
// useEffect(() => {
38-
// const redirectPath = new URLSearchParams(location.search).get('redirect');
39-
// if (redirectPath) {
40-
// window.history.replaceState({}, '', redirectPath);
41-
// }
42-
// }, [location]);
43-
// return null;
44-
// }
35+
// Handle redirects from GitHub Pages 404 fallback
36+
// function RedirectWithState() {
37+
// const location = useLocation();
38+
// useEffect(() => {
39+
// const redirectPath = new URLSearchParams(location.search).get('redirect');
40+
// if (redirectPath) {
41+
// window.history.replaceState({}, '', redirectPath);
42+
// }
43+
// }, [location]);
44+
// return null;
45+
// }
4546

4647
return (
4748
<Router basename={import.meta.env.BASE_URL}>
@@ -66,6 +67,8 @@ function App() {
6667
/>
6768
<Route path="/courses/:courseName" element={<CourseDetail />} />
6869
<Route path="/" element={<Dashboard courses={courses} />} />
70+
<Route path="/search" element={<SearchResults />} />
71+
6972
</Routes>
7073
<ToastContainer />
7174
</div>

src/components/Navbar.jsx

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
1+
import React, { useState } from 'react';
12
import logo from '../assets/images/logo.png';
2-
import { Link } from 'react-router-dom'; // Import Link from react-router-dom
3+
import { Link, useNavigate } from 'react-router-dom'; // Import Link and useNavigate from react-router-dom
34

45
function Navbar({ loggedin }) {
6+
const [searchTerm, setSearchTerm] = useState(''); // State for search term
7+
const navigate = useNavigate(); // useNavigate for navigation
8+
9+
// Handle search input change
10+
const handleSearchChange = (event) => {
11+
setSearchTerm(event.target.value);
12+
};
13+
14+
// Handle search submission
15+
const handleSearchSubmit = (event) => {
16+
event.preventDefault(); // Prevent page refresh
17+
if (searchTerm) {
18+
// Redirect to the search results page with the search term
19+
navigate(`/search?query=${encodeURIComponent(searchTerm)}`);
20+
setSearchTerm(''); // Clear the input after submission
21+
}
22+
};
23+
524
return (
625
<nav className="navbar navbar-expand-lg">
726
<div className="container-fluid">
@@ -16,12 +35,11 @@ function Navbar({ loggedin }) {
1635
<li className="nav-item">
1736
<Link className="nav-link active" aria-current="page" to="/home">Home</Link>
1837
</li>
19-
) : (
20-
<li className="nav-item">
38+
) : (
39+
<li className="nav-item">
2140
<Link className="nav-link active" aria-current="page" to="/">Home</Link>
2241
</li>
2342
)}
24-
2543
<li className="nav-item">
2644
<Link className="nav-link" aria-current="page" to="/events">Events</Link>
2745
</li>
@@ -36,8 +54,14 @@ function Navbar({ loggedin }) {
3654
</li>
3755
</ul>
3856
<div className="d-flex align-items-center flex-column flex-lg-row">
39-
<form className="me-2 mb-2 mb-lg-0">
40-
<input type="text" className="form-control form-control-sm" placeholder="Search" />
57+
<form className="me-2 mb-2 mb-lg-0" onSubmit={handleSearchSubmit}>
58+
<input
59+
type="text"
60+
className="form-control form-control-sm"
61+
placeholder="Search"
62+
value={searchTerm}
63+
onChange={handleSearchChange}
64+
/>
4165
</form>
4266
{loggedin === 'true' ? (
4367
<Link className="btn btn-primary clk" to="/profile">Profile </Link>

src/components/search.jsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import React from 'react';
2+
import { useLocation } from 'react-router-dom';
3+
import courses from './courseData'; // Ensure the path is correct
4+
import { Link } from 'react-router-dom';
5+
const useQuery = () => {
6+
return new URLSearchParams(useLocation().search);
7+
};
8+
9+
function SearchResults() {
10+
const query = useQuery();
11+
const searchTerm = query.get('query')?.toLowerCase() || '';
12+
13+
// Filter courses based on the search term
14+
const filteredCourses = Object.values(courses).filter(course =>
15+
course.title.toLowerCase().includes(searchTerm) ||
16+
course.description.toLowerCase().includes(searchTerm)
17+
);
18+
19+
return (
20+
<div className="container mt-5">
21+
<h2>Search Results for "{searchTerm}"</h2>
22+
{filteredCourses.length > 0 ? (
23+
<div className="row row-cols-1 row-cols-md-3 g-3">
24+
{filteredCourses.map((course, index) => (
25+
<div className="col" key={index}>
26+
<div className="card h-100">
27+
<Link to={`/courses/${course.title.replace(/\s+/g, '-').toLowerCase()}`} className='lnk'>
28+
<img src={course.image} className="card-img-top" alt={course.title} />
29+
<div className="card-body">
30+
<h5 className="card-title">{course.title}</h5>
31+
<p className="card-text">{course.description}</p>
32+
</div>
33+
</Link>
34+
</div>
35+
</div>
36+
))}
37+
</div>
38+
) : (
39+
<h4 className="text-center">No results found</h4>
40+
)}
41+
</div>
42+
);
43+
}
44+
45+
export default SearchResults;

0 commit comments

Comments
 (0)