//Data.js에 데이터 추가
const price = [
{
"_id": 0,
"name": "Any",
"array": []
},
{
"_id": 1,
"name": "$0 to $199",
"array": [0, 199]
},
{
"_id": 2,
"name": "$200 to $249",
"array": [200, 249]
},
{
"_id": 3,
"name": "$250 to $279",
"array": [250, 279]
},
{
"_id": 4,
"name": "$280 to $299",
"array": [280, 299]
},
{
"_id": 5,
"name": "More than $300",
"array": [300, 1500000]
}
]
export {
continents,
price
}
//랜딩페이지에 추가하기
//import { continents, price } from "./Sections/Datas";
<Col lg={12} xs={24}>
{/* RadioBox */}
<Radiobox
list={price}
handleFilters={(filters) => handleFilters(filters, "price")}
/>
</Col>
//RadioBox.js
import React, { useState } from "react";
import { Collapse, Radio } from "antd";
const { Panel } = Collapse;
function RadioBox(props) {
const [Value, setValue] = useState(0);
const renderRadioBox = () =>
props.list &&
props.list.map((value) => (
<Radio key={value._id} value={value._id}>
{" "}
{value.name}{" "}
</Radio>
));
const handleChange = (event) => {
setValue(event.target.value);
props.handleFilters(event.target.value);
};
return (
<div>
<Collapse defaultActiveKey={["0"]}>
<Panel header="Price" key="1">
<Radio.Group onChange={handleChange} value={Value}>
{renderRadioBox()}
</Radio.Group>
</Panel>
</Collapse>
</div>
);
}
export default RadioBox;
const handlePrice = (value) => {
const data = price;
let array = [];
for (let key in data) {
if (data[key]._id === parseInt(value, 10)) {
array = data[key].array;
}
}
return array;
};
const handleFilters = (filters, category) => {
const newFilters = { ...Filters };
newFilters[category] = filters;
console.log("filters", filters);
if (category === "price") {
let priceValues = handlePrice(filters);
newFilters[category] = priceValues;
}
showFilteredResults(newFilters);
setFilters(newFilters);
};
//server의 product.js에 추가
for (let key in req.body.filters) {
// 필터에 따라서
if (req.body.filters[key].length > 0) {
if (key === "price") {
findArgs[key] = {
//Greater than equal, 이것보다 크거나 같은
$gte: req.body.filters[key][0],
//Less than equal, 이것보다 작거나 같은
$lte: req.body.filters[key][1],
};
} else {
findArgs[key] = req.body.filters[key];
}
}
범위에 맞게 적용