|
@@ -0,0 +1,127 @@
|
|
1
|
+import { useState, useEffect } from 'react';
|
|
2
|
+import Grid from '@mui/material/Grid2';
|
|
3
|
+import { Box, Button, TextField, InputAdornment, Typography, Card, CardContent, CardMedia } from '@mui/material';
|
|
4
|
+import { useSelector } from 'react-redux';
|
|
5
|
+
|
|
6
|
+const SearchProduct = ({onClose}) => {
|
|
7
|
+
|
|
8
|
+ const products = useSelector((state) => state.products.products.data)
|
|
9
|
+ const [searchedProducts, setSearchedProducts] = useState([]);
|
|
10
|
+ const [searchQuery, setSearchQuery] = useState('');
|
|
11
|
+
|
|
12
|
+ const filterProduct = function () {
|
|
13
|
+
|
|
14
|
+ let filteredProducts = products.filter((product) => {
|
|
15
|
+
|
|
16
|
+ let title = product?.title?.toLowerCase();
|
|
17
|
+ let search = searchQuery.toLowerCase();
|
|
18
|
+ return title?.includes(search)
|
|
19
|
+ })
|
|
20
|
+
|
|
21
|
+ // MAX show result
|
|
22
|
+ filteredProducts = filteredProducts?.slice(0, 6);
|
|
23
|
+
|
|
24
|
+ setSearchedProducts(filteredProducts)
|
|
25
|
+
|
|
26
|
+ }
|
|
27
|
+
|
|
28
|
+ return (
|
|
29
|
+ <Box
|
|
30
|
+ className="animate__animated animate__fadeIn"
|
|
31
|
+ sx={{
|
|
32
|
+ position: 'fixed',
|
|
33
|
+ width: '100%',
|
|
34
|
+ height: '100%',
|
|
35
|
+ backgroundColor: 'rgba(255,255,255,0.8)',
|
|
36
|
+ top: 0,
|
|
37
|
+ left: 0,
|
|
38
|
+ py: 2,
|
|
39
|
+ zIndex: 999,
|
|
40
|
+ backdropFilter: 'blur(10px)',
|
|
41
|
+ display: 'flex',
|
|
42
|
+ flexDirection: 'column',
|
|
43
|
+ alignItems: 'center',
|
|
44
|
+ }}
|
|
45
|
+ >
|
|
46
|
+ {/* Search Box */}
|
|
47
|
+ <Box
|
|
48
|
+ sx={{
|
|
49
|
+ backgroundColor: 'rgba(0,0,0,0)',
|
|
50
|
+ borderRadius: 2,
|
|
51
|
+ padding: 2,
|
|
52
|
+ width: '90%',
|
|
53
|
+ marginBottom: 4,
|
|
54
|
+ }}
|
|
55
|
+ >
|
|
56
|
+ <Grid container spacing={2}>
|
|
57
|
+ <Grid size={12}>
|
|
58
|
+ <TextField
|
|
59
|
+ fullWidth
|
|
60
|
+ placeholder="Search for a product..."
|
|
61
|
+ variant="outlined"
|
|
62
|
+ value={searchQuery}
|
|
63
|
+ onChange={(e) => {
|
|
64
|
+ setSearchQuery(e.target.value)
|
|
65
|
+ if ((!searchQuery || searchQuery.trim() === "")) {
|
|
66
|
+ setSearchedProducts([])
|
|
67
|
+ } else {
|
|
68
|
+ filterProduct()
|
|
69
|
+ }
|
|
70
|
+ }}
|
|
71
|
+ InputProps={{
|
|
72
|
+ endAdornment: (
|
|
73
|
+ <InputAdornment position="end">
|
|
74
|
+ <Button
|
|
75
|
+ variant="contained"
|
|
76
|
+ color="primary"
|
|
77
|
+ sx={{ height: '100%' }}
|
|
78
|
+ onClick={onClose}
|
|
79
|
+ >
|
|
80
|
+ Cancel
|
|
81
|
+ </Button>
|
|
82
|
+ </InputAdornment>
|
|
83
|
+ ),
|
|
84
|
+ }}
|
|
85
|
+ />
|
|
86
|
+ </Grid>
|
|
87
|
+ </Grid>
|
|
88
|
+ </Box>
|
|
89
|
+
|
|
90
|
+ {/* Product List */}
|
|
91
|
+ <Grid
|
|
92
|
+ container
|
|
93
|
+ columns={12}
|
|
94
|
+ >
|
|
95
|
+ {searchedProducts.length > 0 ? (
|
|
96
|
+ searchedProducts.map((product) => (
|
|
97
|
+ <Grid className="animate__animated animate__fadeIn" item size={{ xs: 12, sm: 12, md: 6 }} sx={{p:1}}>
|
|
98
|
+ <Card
|
|
99
|
+ key={product.id}
|
|
100
|
+ sx={{ display: 'flex', alignItems: 'center', p: 2, cursor:"pointer" }}
|
|
101
|
+ onClick={() => {
|
|
102
|
+ window.location.href = `/product/${product?.handle}`
|
|
103
|
+ }}
|
|
104
|
+ >
|
|
105
|
+ <CardMedia
|
|
106
|
+ component="img"
|
|
107
|
+ sx={{ width: 100, height: 100, borderRadius: 1, marginRight: 2 }}
|
|
108
|
+ image={product?.images[0]?.url}
|
|
109
|
+ alt={product?.title}
|
|
110
|
+ />
|
|
111
|
+ <CardContent sx={{ flex: '1 0 auto' }}>
|
|
112
|
+ <Typography variant="h5">{product?.title}</Typography>
|
|
113
|
+ </CardContent>
|
|
114
|
+ </Card>
|
|
115
|
+ </Grid>
|
|
116
|
+ ))
|
|
117
|
+ ) : (
|
|
118
|
+ <Typography variant="body1" color="text.secondary">
|
|
119
|
+ No products found. Please search
|
|
120
|
+ </Typography>
|
|
121
|
+ )}
|
|
122
|
+ </Grid>
|
|
123
|
+ </Box>
|
|
124
|
+ );
|
|
125
|
+};
|
|
126
|
+
|
|
127
|
+export default SearchProduct;
|