Skip to content

Commit

Permalink
add inputFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolaus-B committed Mar 23, 2024
1 parent 54c8949 commit e33194e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/components/CarsList/CarsList.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { useSelector } from "react-redux";
import { selectCars } from "../../redux/cars/carsSelectors";
import { selectfilteredCars } from "../../redux/cars/carsSelectors";
import { Car } from "../Car/Car";
import { CarList, ListContainer, LoadMore } from "./CarsList.styled";

export const CarsList = () => {
const cars = useSelector(selectCars);
// const cars = useSelector(selectCars);
const filteredCars = useSelector(selectfilteredCars);

return (
<ListContainer>
<CarList>
{cars.map((car) => {
{filteredCars.map((car) => {
return <Car key={car.id} car={car.car} />;
})}
</CarList>
Expand Down
4 changes: 2 additions & 2 deletions src/components/FilterButton/FilterButton.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useState } from "react";
// import { useState } from "react";
import { Icon } from "../Icon/Icon";
import {
ButtonContentContainer,
FilterButtonStyled,
} from "./FilterButton.styled";

export const FilterButton = ({ id, iconType, text }) => {
const [isSelected, setIsSelected] = useState(false);
// const [isSelected, setIsSelected] = useState(false);

return (
<FilterButtonStyled onClick={() => console.log(text)}>
Expand Down
11 changes: 10 additions & 1 deletion src/components/Filters/Filters.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useDispatch } from "react-redux";
import { FilterButton } from "../FilterButton/FilterButton";
import {
CategoriesLine,
Expand All @@ -8,13 +9,21 @@ import {
Location,
SearchButton,
} from "./Filters.styled";
import { changeInputFilter } from "../../redux/filter/filterSlice";

export const Filters = () => {
const dispatch = useDispatch();

return (
<div>
<div>
<Location>Location</Location>
<FilterInput name="location" type="text" placeholder="Kyiv, Ukraine" />
<FilterInput
onChange={(e) => dispatch(changeInputFilter(e.target.value))}
name="location"
type="text"
placeholder="Kyiv, Ukraine"
/>
</div>
<FilterText>Filters</FilterText>
<div>
Expand Down
20 changes: 20 additions & 0 deletions src/redux/cars/carsSelectors.js
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
import { createSelector } from "@reduxjs/toolkit";
import { selectInputValue } from "../filter/filterSelectors";

export const selectCars = (state) => state.cars.cars;

export const selectfilteredCars = createSelector(
[selectCars, selectInputValue],
(cars, inputValue) => {
return cars.filter((el) => {
const { car } = el;
// console.log(car);
const carName = car.name.toLowerCase();
const carLocation = car.location.toLowerCase();

return (
carName.includes(inputValue.toLowerCase()) ||
carLocation.includes(inputValue.toLowerCase())
);
});
}
);
1 change: 1 addition & 0 deletions src/redux/filter/filterSelectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const selectInputValue = (state) => state.filter.inputValue;
7 changes: 6 additions & 1 deletion src/redux/filter/filterSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ const filterSlice = createSlice({
equipment: [],
type: [],
},
reducers: {},
reducers: {
changeInputFilter: (state, action) => {
state.inputValue = action.payload;
},
},
});

export const { changeInputFilter } = filterSlice.actions;
export const filterReducer = filterSlice.reducer;

0 comments on commit e33194e

Please sign in to comment.