diff --git a/src/Redux/reducers/book.js b/src/Redux/reducers/book.js index 194b19f..28a4ca7 100644 --- a/src/Redux/reducers/book.js +++ b/src/Redux/reducers/book.js @@ -20,24 +20,15 @@ export default function book(state = initialState, action) { case "book/delete": return { ...state, loading: false, error: null }; case "book/search": - return { - ...state, - loading: false, - error: null, - searchResult: state.list.filter( - (item) => - item.product[0].name.indexOf(data) !== -1 || - item.product[1].name.indexOf(data) !== -1 || - item.name.indexOf(data) !== -1 - ), - }; + return { ...state }; case "book/filter": const realList = state.searchResult.length > 0 ? state.searchResult : state.list; const level = data.level; const subject = data.subject; const sort = data.sort; - if (level.length > 0 || subject.length > 0 || sort) { + const search = data.search; + if (level.length > 0 || subject.length > 0 || sort || search) { localStorage.setItem("filter", "ON"); } let finalSort; @@ -53,12 +44,16 @@ export default function book(state = initialState, action) { (item) => item.name.indexOf(subject.map((item) => item)) !== -1 ) : byLevelFilter; + console.log(bySubjectFilter); + const bySearchFilter = search + ? bySubjectFilter.filter((item) => item.name.indexOf(search) !== -1) + : bySubjectFilter; if (sort === "گران ترین") { - finalSort = bySubjectFilter.sort((item1, item2) => { + finalSort = bySearchFilter.sort((item1, item2) => { return item1.product[0].price - item2.product[0].price; }); } else { - finalSort = bySubjectFilter; + finalSort = bySearchFilter; } return { ...state, diff --git a/src/views/Products/List/index.js b/src/views/Products/List/index.js index a430ba6..82b264f 100644 --- a/src/views/Products/List/index.js +++ b/src/views/Products/List/index.js @@ -32,24 +32,22 @@ class List extends React.Component { ) : ( <> - {list.length > 0 ? ( -
- {search - ? searchResult.map((item, i) => ( - - )) - : list.map((item, i) => ( - - ))} -
- ) : ( -
- -
- )} +
+ {list.length > 0 ? ( + filterResult.length > 0 || localStorage.getItem("filter") ? ( + filterResult.map((item, i) => ) + ) : ( + list.map((item, i) => ) + ) + ) : ( +
+ +
+ )} +
); } diff --git a/src/views/Products/Select/index.js b/src/views/Products/Select/index.js index 407c77e..83edf16 100644 --- a/src/views/Products/Select/index.js +++ b/src/views/Products/Select/index.js @@ -41,7 +41,6 @@ const useStyles = makeStyles((theme) => ({ export default function CustomizedSelects(props) { const classes = useStyles(); - console.log(props); return (
@@ -49,7 +48,24 @@ export default function CustomizedSelects(props) { id="demo-customized-select-native" value={props.parent.state[props.name]} onChange={(e) => - props.parent.setState({ [props.name]: e.target.value }) + e.target.value !== "نام کتاب" && + e.target.value !== "مرتب سازی" && + e.target.value !== "پایه تحصیلی" + ? props.parent.setState({ + selectedFilters: { + ...props.parent.state.selectedFilters, + [props.name]: + props.name === "sort" + ? e.target.value + : new Array(e.target.value), + }, + }) + : props.parent.setState({ + selectedFilters: { + ...props.parent.state.selectedFilters, + [props.name]: props.name === "sort" ? null : new Array(), + }, + }) } input={} > diff --git a/src/views/Products/index.js b/src/views/Products/index.js index 7a66064..016a068 100644 --- a/src/views/Products/index.js +++ b/src/views/Products/index.js @@ -32,36 +32,45 @@ class Products extends Component { subject: [], sort: null, }, - firstFilters: [ - { id: 0, value: "محبوب ترین" }, - { id: 1, value: "جدیدترین" }, - { id: 2, value: "ارزان ترین" }, + sortFilters: [ + { id: 0, value: "مرتب سازی" }, + { id: 1, value: "محبوب ترین" }, + { id: 2, value: "جدیدترین" }, + { id: 3, value: "ارزان ترین" }, ], - dateFilters: [ - { id: 0, value: "1400" }, - { id: 1, value: "`1399" }, - { id: 2, value: "1398" }, + subjectFilters: [ + { id: 0, value: "نام کتاب" }, + { id: 1, value: "ریاضی" }, + { id: 2, value: "شیمی" }, + { id: 3, value: "علوم" }, ], - degreeFilters: [ - { id: 0, value: "اول" }, - { id: 1, value: "دوم" }, - { id: 2, value: "سوم" }, + levelFilters: [ + { id: 0, value: "پایه تحصیلی" }, + { id: 1, value: "اول" }, + { id: 2, value: "دوم" }, + { id: 3, value: "سوم" }, + { id: 4, value: "چهارم" }, + { id: 5, value: "دهم" }, + { id: 6, value: "هفتم" }, ], - first: "", - date: "", - degree: "", }; handleProductSearch = (val) => { this.setState({ search: val, }); - this.props.searchBook(val); + // this.props.searchBook(val); }; componentDidUpdate(prevProps, prevState) { - if (prevState.selectedFilters !== this.state.selectedFilters) { - this.props.filterBook(this.state.selectedFilters); + if ( + prevState.selectedFilters !== this.state.selectedFilters || + prevState.search !== this.state.search + ) { + this.props.filterBook({ + ...this.state.selectedFilters, + search: this.state.search, + }); } } @@ -70,8 +79,7 @@ class Products extends Component { } render() { - const { dateFilters, degreeFilters, firstFilters } = this.state; - const { loading, list, page } = this.props; + const { levelFilters, subjectFilters, sortFilters } = this.state; return ( <> {window.innerWidth > 1000 ? ( @@ -108,9 +116,9 @@ class Products extends Component {
- - +