1
1
import React , { useContext , useEffect , useState } from "react" ;
2
- import { Link } from "react-router-dom" ;
2
+ import { Link , useNavigate } from "react-router-dom" ;
3
3
import { Context } from "../store/appContext" ;
4
4
import { privateUser } from "../apiservices/callToApi" ;
5
5
@@ -8,27 +8,33 @@ export const Navbar = () => {
8
8
const { store } = useContext ( Context ) ;
9
9
const [ isVerified , setIsVerified ] = useState ( null ) ;
10
10
const [ search , setSearch ] = useState ( "" ) ;
11
+ const navigate = useNavigate ( ) ;
11
12
12
- let products = [ ] ;
13
- const phones = store . phones ;
14
- const tvs = store . tvs ;
15
- const laptops = store . laptops ;
16
-
17
- products = phones . concat ( laptops , tvs ) ;
18
-
19
- console . log ( products ) ;
20
-
21
-
22
-
13
+ const searcher = ( e ) => {
14
+ setSearch ( e . target . value )
15
+ } ;
23
16
24
17
const checkout = async ( ) => {
25
18
const verified = await privateUser ( )
26
19
setIsVerified ( verified ) } ;
27
20
21
+
22
+ const verifiedSearch = ( ) => {
23
+ if ( search ) {
24
+ navigate ( '/search-product' )
25
+ } else {
26
+ navigate ( '/' )
27
+ } ;
28
+ } ;
29
+
28
30
useEffect ( ( ) => {
29
31
checkout ( ) ;
30
32
} , [ ] ) ;
31
33
34
+ useEffect ( ( ) => {
35
+ verifiedSearch ( ) ;
36
+ } , [ search ] )
37
+
32
38
const logOut = ( ) => {
33
39
sessionStorage . removeItem ( 'token' ) ;
34
40
sessionStorage . removeItem ( 'idUser' )
@@ -47,7 +53,7 @@ export const Navbar = () => {
47
53
</ button >
48
54
< div className = "d-flex" >
49
55
< form className = "d-flex" role = "search" >
50
- < input className = "form-control me-2" type = "search" placeholder = "Search" aria-label = "Search" > </ input >
56
+ < input className = "form-control me-2" onChange = { ( e ) => searcher ( e ) } type = "search" placeholder = "Search" aria-label = "Search" > </ input >
51
57
</ form >
52
58
< div className = "collapse navbar-collapse" id = "navbarSupportedContent" >
53
59
< ul className = "navbar-nav me-auto mb-2 mb-lg-0" >
0 commit comments