-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
85 lines (70 loc) · 2.04 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import Navigation from "./Navigation/Nav";
import Products from "./Products/Products";
import Recommended from "./Recommended/Recommended";
import Sidebar from "./Sidebar/Sidebar";
//getting the db
import products from "./db/data";
import Cards from "./Components/Cards";
import { useState } from "react";
function App() {
const [selectedCategory, setSelectedCategory]= useState(null);
const [query,setQuery]= useState("");
//input filter
const handleInputChange= event=>{
setQuery(event.target.value)
}
const filteredItems= products.filter(
(product) => product.title.toLowerCase().indexOf(query.toLowerCase()) !== -1
);
//radio filter
const handleChange= event=>{
setSelectedCategory(event.target.value)
};
//buttons filter
const handleClick= event=>{
setSelectedCategory(event.target.value)
};
//final filtering----
function filteredData(query, selected, products){
let filteredProducts= products;
//for input
if(query){
filteredProducts= filteredItems
};
//for checkmarks
if(selected){
filteredProducts= filteredProducts.filter(
({company, color, category, newPrice, title}) =>
category === selected ||
color === selected ||
company === selected ||
newPrice === selected ||
title === selected
);
}
return filteredProducts.map(
({img, title, prevPrice, newPrice, star, reviews})=>(
<Cards
key={Math.random()}
img={img}
title={title}
prevPrice={prevPrice}
newPrice={newPrice}
star={star}
reviews={reviews}
/>
)
);
}
//now result of filter
const result= filteredData(products,selectedCategory,query);
//if we wrote query or selected category at first it will show null error as they are pre defined
return (<>
<Sidebar handleChange={handleChange}/>
<Navigation query={query} handleInputChange={handleInputChange}/>
<Recommended handleClick={handleClick}/>
<Products result={result}/>
</>
);
}
export default App;