3
3
import React , { useEffect , useState } from "react" ;
4
4
import { FiSearch } from "react-icons/fi" ;
5
5
import { useAppDispatch , useAppSelector } from "../../store/store" ;
6
- import { Link , useNavigate } from "react-router-dom" ;
6
+ import { Link , useLocation , useNavigate } from "react-router-dom" ;
7
7
import { searchProduct } from "../../store/features/product/productSlice" ;
8
8
interface SearchInputProps {
9
9
className : string ;
10
10
placeholder ?: string ;
11
11
}
12
12
13
+
13
14
function SearchInput ( { className, placeholder } : SearchInputProps ) {
14
15
const dispatch = useAppDispatch ( ) ;
15
- const { isSuccess, isError, isLoading, products } = useAppSelector (
16
- ( state ) => state . products
17
- ) ;
16
+ const { products } = useAppSelector ( ( state ) => state . products ) ;
18
17
const [ search , setSearch ] = useState ( "" ) ;
19
18
const [ isFocused , setIsFocused ] = useState ( false ) ;
20
19
const [ isHovered , setIsHovered ] = useState ( false ) ;
@@ -24,11 +23,8 @@ function SearchInput({ className, placeholder }: SearchInputProps) {
24
23
const handleSearchChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
25
24
const value = e . target . value ;
26
25
setSearch ( value ) ;
27
- if ( value . trim ( ) ) {
28
- dispatch ( searchProduct ( { name : value . trim ( ) } ) ) ;
29
- } else {
30
- setIsFocused ( false ) ;
31
- }
26
+ dispatch ( searchProduct ( { name : value . trim ( ) } ) ) ;
27
+ setIsFocused ( true ) ;
32
28
} ;
33
29
34
30
const handleProductClick = ( name : string ) => {
@@ -38,6 +34,13 @@ function SearchInput({ className, placeholder }: SearchInputProps) {
38
34
navigate ( `/search?name=${ name } ` ) ;
39
35
} ;
40
36
37
+ const handleSubmit = ( e : React . FormEvent < HTMLFormElement > ) => {
38
+ e . preventDefault ( ) ;
39
+ if ( search . trim ( ) ) {
40
+ navigate ( `/search?name=${ search . trim ( ) } ` ) ;
41
+ }
42
+ } ;
43
+
41
44
useEffect ( ( ) => {
42
45
if ( ! search . trim ( ) ) {
43
46
setIsFocused ( false ) ;
@@ -51,15 +54,7 @@ function SearchInput({ className, placeholder }: SearchInputProps) {
51
54
52
55
return (
53
56
< div className = "main-search" >
54
- < form
55
- className = { `search-container ${ className } ` }
56
- onSubmit = { ( e ) => {
57
- e . preventDefault ( ) ;
58
- if ( search . trim ( ) ) {
59
- navigate ( `/search?name=${ search . trim ( ) } ` ) ;
60
- }
61
- } }
62
- >
57
+ < form className = { `search-container ${ className } ` } onSubmit = { handleSubmit } >
63
58
< div className = "search-icon" >
64
59
< FiSearch />
65
60
</ div >
@@ -75,11 +70,7 @@ function SearchInput({ className, placeholder }: SearchInputProps) {
75
70
onChange = { handleSearchChange }
76
71
value = { search }
77
72
/>
78
- < button
79
- className = "search-button"
80
- type = "submit"
81
- onClick = { ( ) => navigate ( `/search?name=${ search } ` ) }
82
- >
73
+ < button className = "search-button" type = "submit" >
83
74
Search
84
75
</ button >
85
76
</ form >
@@ -92,10 +83,7 @@ function SearchInput({ className, placeholder }: SearchInputProps) {
92
83
{ filteredProducts && filteredProducts . length > 0 ? (
93
84
filteredProducts . map ( ( product : any ) => (
94
85
< div key = { product . id } className = "result" >
95
- < div
96
- className = "link"
97
- onClick = { ( ) => handleProductClick ( product . name ) }
98
- >
86
+ < div className = "link" onClick = { ( ) => handleProductClick ( product . name ) } >
99
87
< p > { product . name } </ p >
100
88
</ div >
101
89
</ div >
0 commit comments