Skip to content

Commit

Permalink
fixFilterStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
OlenaIa committed Dec 1, 2023
1 parent 8c4147a commit 5dfe190
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 39 deletions.
42 changes: 19 additions & 23 deletions src/components/CarsList/CarsList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,48 @@ import { Container, Section } from "pages/Page.styled";
import { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { LIMIT, getAllCarsThunk, getCarsThunk, getFilterCarsThunk } from "redux/cars/fetchCar";
import { selectAllCars, selectBrand, selectCars, selectIsLoading, selectPage } from "redux/selectors";
import { selectAllCars, selectBrand, selectCars, selectIsLoading } from "redux/selectors";
import { CarsListStyle, LoadMore } from "./CarsList.styled";
import { pageSet } from "redux/filter/filterSlice";

export const CarsList = () => {
const dispatch = useDispatch();
const isLoading = useSelector(selectIsLoading);
const cars = useSelector(selectCars);
const allCars = useSelector(selectAllCars);
const filterBrand = useSelector(selectBrand);
const page = useSelector(selectPage);
// const [page, setPage] = useState(0)
// const page = useSelector(selectPage);
const [page, setPage] = useState(1)
const [isLoadMore, setIsLoadMore] = useState(false);

useEffect(() => {
dispatch(getAllCarsThunk())
}, [dispatch]);

// useEffect(() => {
// if (page === 0) {
// setPage(page + 1);
// return;
// };

// dispatch(getCarsThunk(page))
// }, [dispatch, page]);

useEffect(() => {
// if (filterBrand === 'all') {
// setIsLoadMore(false);
// setPage(1);
// };
if (page === 1) {
return;
};

dispatch(getCarsThunk(page))
}, [dispatch, page]);

dispatch(getFilterCarsThunk({filterBrand, page}))
setIsLoadMore(true);
useEffect(() => {
if (filterBrand.value === 'all') {
setIsLoadMore(false);
setPage(1);
} else {setIsLoadMore(true)};

}, [dispatch, filterBrand, page]);
dispatch(getFilterCarsThunk(filterBrand))
}, [dispatch, filterBrand]);

const totalPage = allCars.length / LIMIT;

const onClickLoadMore = () => {
if (page === totalPage - 1) {
setIsLoadMore(true);
};
pageSet();
setPage(page + 1)

console.log('page', page);
};

Expand All @@ -63,7 +59,7 @@ export const CarsList = () => {
<CarItem car={car} key={car.id} index={index} />
)}
</CarsListStyle>
<LoadMore onClick={onClickLoadMore} display={false ? 'none' : 'block'}>Load more</LoadMore>
<LoadMore onClick={onClickLoadMore} display={isLoadMore ? 'none' : 'block'}>Load more</LoadMore>
</>
)}
</Container>
Expand Down
8 changes: 3 additions & 5 deletions src/components/Filters/Filters.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ console.log('mileageFrom');
value={selectedPrice}
/>
</Label>
<form
// onSubmit={onSubmitFilter}
>
<InputWrap>
<Label>Сar mileage / km
<Input type="number"
Expand All @@ -99,8 +96,9 @@ value={mileageTo}
step='10'
$radius='0px 14px 14px 0px' />
</InputWrap>
<ButtonSearch type="submit">Search</ButtonSearch>
</form>
<ButtonSearch type="submit"
// onSubmit={onSubmitFilter}
>Search</ButtonSearch>
</Form>
</>
)
Expand Down
2 changes: 2 additions & 0 deletions src/components/Filters/Filters.styled.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export const Form = styled.form`
`;

export const Label = styled.label`
display: flex;
flex-direction: column;
color: var(--color-text-label);
font-size: 14px;
font-weight: 500;
Expand Down
7 changes: 4 additions & 3 deletions src/redux/cars/fetchCar.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const getAllCars = async (_, thunkAPI) => {

const getCars = async (page, thunkAPI) => {
try {
console.log('page in getCars', page);
const response = await axios.get(`/adverts?page=${page}&limit=${LIMIT}`);
return response.data;
}
Expand All @@ -32,12 +33,12 @@ const getCars = async (page, thunkAPI) => {
}
};

const getFilterCars = async ({filterBrand, page}, thunkAPI) => {
const getFilterCars = async (filterBrand, thunkAPI) => {
try {

const endPoint = (filterBrand.value === 'all') ? `page=${page}&limit=${LIMIT}` : `make=${filterBrand.value}`;
const endPoint = (filterBrand.value === 'all') ? `page=1&limit=${LIMIT}` : `make=${filterBrand.value}`;
console.log('filterBrand in getFilterCars', filterBrand);
console.log('page in getFilterCars', page);
// console.log('page in getFilterCars', page);
console.log('endPoint', endPoint);
const response = await axios.get(`/adverts?${endPoint}`);
return response.data;
Expand Down
7 changes: 1 addition & 6 deletions src/redux/filter/filterSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { createSlice } from '@reduxjs/toolkit';

const filterInitialState = {
brand: { value: 'all', label: 'Enter the text' },
page: 1,
};

export const filterSlice = createSlice({
Expand All @@ -12,13 +11,9 @@ export const filterSlice = createSlice({
brandSet(state, {payload}) {
state.brand = payload;
},
pageSet(state, {payload}) {
console.log('payload', payload);
state.page = 2;
},
},
});

export const filterReducer = filterSlice.reducer;

export const { brandSet, pageSet } = filterSlice.actions;
export const { brandSet } = filterSlice.actions;
3 changes: 1 addition & 2 deletions src/redux/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ export const selectFavoriteCars = state => state.favoriteCars.favorites;

// --------------- FILTER -----------------

export const selectBrand = state => state.filter.brand;
export const selectPage = state => state.filter.page;
export const selectBrand = state => state.filter.brand;

0 comments on commit 5dfe190

Please sign in to comment.