1
1
import React , { useState } from 'react' ;
2
2
import logo from '../assets/images/logo.png' ;
3
3
import { Link , useNavigate } from 'react-router-dom' ;
4
+ import courses from './courseData' ; // Assuming this is your courses object
4
5
5
6
function Navbar ( { loggedin } ) {
6
- const [ searchTerm , setSearchTerm ] = useState ( '' ) ; // State for search term
7
- const navigate = useNavigate ( ) ; // useNavigate for navigation
7
+ const [ searchTerm , setSearchTerm ] = useState ( '' ) ;
8
+ const [ suggestions , setSuggestions ] = useState ( [ ] ) ;
9
+ const navigate = useNavigate ( ) ;
8
10
9
11
// Handle search input change
10
12
const handleSearchChange = ( event ) => {
11
- setSearchTerm ( event . target . value ) ;
13
+ const searchInput = event . target . value ;
14
+ setSearchTerm ( searchInput ) ;
15
+
16
+ // Convert courses object to an array for filtering
17
+ const coursesArray = Object . values ( courses ) ;
18
+
19
+ if ( searchInput . trim ( ) ) {
20
+ const filteredSuggestions = coursesArray . filter ( ( course ) =>
21
+ course . title . toLowerCase ( ) . includes ( searchInput . toLowerCase ( ) )
22
+ ) ;
23
+ setSuggestions ( filteredSuggestions ) ;
24
+ } else {
25
+ setSuggestions ( [ ] ) ;
26
+ }
12
27
} ;
13
28
14
29
// Handle search submission when "Enter" is pressed
15
30
const handleSearchSubmit = ( event ) => {
16
- event . preventDefault ( ) ; // Prevent form submission default behavior
31
+ event . preventDefault ( ) ;
17
32
if ( searchTerm . trim ( ) ) {
18
- // Navigate to search page with the query
19
33
navigate ( `/search?query=${ encodeURIComponent ( searchTerm ) } ` ) ;
20
- // setSearchTerm('' ); // Optionally reset the search input after navigating
34
+ setSuggestions ( [ ] ) ; // Clear suggestions after search
21
35
}
22
36
} ;
23
37
38
+ // Handle suggestion click
39
+ const handleSuggestionClick = ( suggestion ) => {
40
+ setSearchTerm ( suggestion . title ) ;
41
+ navigate ( `/search?query=${ encodeURIComponent ( suggestion . title ) } ` ) ;
42
+ setSuggestions ( [ ] ) ;
43
+ } ;
44
+
24
45
return (
25
46
< nav className = "navbar navbar-expand-lg" >
26
47
< div className = "container-fluid" >
@@ -53,8 +74,10 @@ function Navbar({ loggedin }) {
53
74
</ ul >
54
75
</ li >
55
76
</ ul >
77
+
78
+ { /* Search Bar */ }
56
79
< div className = "d-flex align-items-center flex-column flex-lg-row" >
57
- < div className = "me-2 mb-2 mb-lg-0" >
80
+ < div className = "me-2 mb-2 mb-lg-0 position-relative " >
58
81
< form className = "input-group" onSubmit = { handleSearchSubmit } >
59
82
< span className = "input-group-text" id = "basic-addon1" >
60
83
< svg xmlns = "http://www.w3.org/2000/svg" width = "16" height = "16" fill = "currentColor" className = "bi bi-search" viewBox = "0 0 16 16" >
@@ -69,9 +92,27 @@ function Navbar({ loggedin }) {
69
92
onChange = { handleSearchChange }
70
93
/>
71
94
</ form >
95
+
96
+ { /* Suggestions Dropdown */ }
97
+ { suggestions . length > 0 && (
98
+ < ul className = "list-group position-absolute w-100 mt-1 z-index-1000" style = { { zIndex : '1000' } } >
99
+ { suggestions . map ( ( suggestion , index ) => (
100
+ < li
101
+ key = { index }
102
+ className = "list-group-item"
103
+ onClick = { ( ) => handleSuggestionClick ( suggestion ) }
104
+ style = { { cursor : 'pointer' } }
105
+ >
106
+ { suggestion . title }
107
+ </ li >
108
+ ) ) }
109
+ </ ul >
110
+ ) }
72
111
</ div >
112
+
113
+ { /* Profile or Sign up Button */ }
73
114
{ loggedin === 'true' ? (
74
- < span > < Link className = "btn btn-primary clk rounded-circle" to = "/profile" > < i className = "fa-regular fa-user" > </ i > </ Link > Profile</ span >
115
+ < span > < Link className = "btn btn-primary clk rounded-circle" to = "/profile" > < i className = "fa-regular fa-user" > </ i > </ Link > Profile</ span >
75
116
) : (
76
117
< Link className = "btn btn-primary clk" to = "/login" > Sign up</ Link >
77
118
) }
0 commit comments