Skip to content

Commit

Permalink
add filter by location and type
Browse files Browse the repository at this point in the history
  • Loading branch information
Soldizz committed Apr 13, 2024
1 parent 190ffed commit dd72c9b
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 16 deletions.
8 changes: 7 additions & 1 deletion src/features/CardList/CardList.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
display: flex;
flex-direction: column;
gap: 50px;
justify-content: center;
align-items: center;
}

.not_found {
margin-top: 100px;
display: flex;
flex-direction: column;
gap: 24px;
}
34 changes: 23 additions & 11 deletions src/features/CardList/CardList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ import {Loader} from "@/shared/ui/Loader/ui/Loader";
import {Button, ButtonTheme} from "@/shared/ui/Button/Button";
import {useDispatch, useSelector} from "react-redux";
import {campersActions} from "@/features/CardList/model/slice/camperSlice";
import {selectCampers} from "@/features/CardList/model/selectors/getAllCampers";
import {selectCampers, selectFilter} from "@/features/CardList/model/selectors/getAllCampers";
import {filterCampers} from "@/shared/lib/filterCampers/filterCampers";

export const CardList = () => {
const [camper, setCamper] = useState([]);
const [isLoadingServer, setIsLoadingServer] = useState(false);
const [visibleCamperCount, setVisibleCamperCount] = useState(4);
const dispatch = useDispatch();
const campers = useSelector(selectCampers);
const filtered = useSelector(selectFilter);

useEffect(() => {
if(filtered) setCamper(filterCampers(campers, filtered));
}, [filtered]);

Check failure on line 22 in src/features/CardList/CardList.tsx

View workflow job for this annotation

GitHub Actions / pipeline (17.x)

React Hook useEffect has a missing dependency: 'campers'. Either include it or remove the dependency array

useEffect(() => {
setIsLoadingServer(true);
Expand All @@ -22,20 +28,19 @@ export const CardList = () => {
setCamper(response.data);
dispatch(campersActions.addCamper(response.data))
})
.catch(error => {
console.log('error', error);
})
.catch(error => console.log('error', error))
.finally(() => setIsLoadingServer(false));
}, []);

Check failure on line 33 in src/features/CardList/CardList.tsx

View workflow job for this annotation

GitHub Actions / pipeline (17.x)

React Hook useEffect has a missing dependency: 'dispatch'. Either include it or remove the dependency array

useEffect(() => {
if(campers.length > 0) {
setCamper(campers);
}
if(campers.length > 0) setCamper(campers);
}, []);

Check failure on line 37 in src/features/CardList/CardList.tsx

View workflow job for this annotation

GitHub Actions / pipeline (17.x)

React Hook useEffect has a missing dependency: 'campers'. Either include it or remove the dependency array

const loadMore = () => {
setVisibleCamperCount(prevCount => prevCount + 4);
const loadMore = () => setVisibleCamperCount(prevCount => prevCount + 4);

const handleReset = () => {
dispatch(campersActions.clearFilter());
setCamper(campers);
};

return (
Expand All @@ -48,17 +53,24 @@ export const CardList = () => {
) : (
<section className={cls.container_list}>
<ul className={cls.list}>
{camper.length > 0 && camper.slice(0, visibleCamperCount).map((item: any, index: number) => (
{camper.length > 0
? camper.slice(0, visibleCamperCount).map((item: any, index: number) => (
<li key={`${item.name} + ${index}`}>
<Card {...item} />

Check warning on line 59 in src/features/CardList/CardList.tsx

View workflow job for this annotation

GitHub Actions / pipeline (17.x)

Prop spreading is forbidden
</li>
))}
))
: <li className={cls.not_found}>
<h3>Нажаль, нічого не знайдено</h3>
<Button theme={ButtonTheme.SEARCH} onClick={handleReset}>Reset</Button>
</li>
}
</ul>
{camper.length > visibleCamperCount && (
<Button theme={ButtonTheme.LOAD_MORE} onClick={loadMore}>
Load more
</Button>
)}

</section>
)}
</>
Expand Down
4 changes: 3 additions & 1 deletion src/features/CardList/model/selectors/getAllCampers.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@

export const selectCampers = (state) => state.campers.campers
export const selectCampers = (state) => state.campers.campers;

export const selectFilter = (state) => state.campers.filter;
8 changes: 6 additions & 2 deletions src/features/CardList/model/slice/camperSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ import { createSlice } from '@reduxjs/toolkit';

const initialState = {
campers: [],
filter: {}
filter: null
}

export const camperSlice = createSlice({
name: 'campers',
initialState,
reducers: {
addCamper: (state, action) => {
state.campers.push(action.payload)
state.campers = action.payload
},
addFilter: (state, action) => {
state.filter = action.payload
},

clearFilter: (state) => {
state.filter = null
}
},
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.container {
width: 360px;
margin-top: 32px;
display: flex;
flex-direction: column;
Expand Down
18 changes: 18 additions & 0 deletions src/shared/lib/filterCampers/filterCampers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {DetailsItem} from "@/shared/types/card";

export const filterCampers = (original: any, filters: any) => {
const byLocation = original.filter((item: any) => item.location === filters.location);
if(filters.equipment.length === 0 && filters.type.length === 0) {return byLocation}

if(filters.type.length > 0) {
const byType: DetailsItem[] = [];
byLocation.forEach((item: any) => {
if(filters.type.includes(item.form)) {
byType.push(item)
}
})
return byType
}


}
2 changes: 1 addition & 1 deletion src/shared/ui/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const Select = ({getLocation} : SelectProps) => {

useEffect(() => {
if (campers.length > 0) {
const uniqueLocations = [...new Set(campers[0].map((item: any) => item.location))];
const uniqueLocations = [...new Set(campers.map((item: any) => item.location))];
// @ts-ignore
setOptions(uniqueLocations.map((location: string, index: number) => ({ id: index + 1, name: location })));
}
Expand Down

0 comments on commit dd72c9b

Please sign in to comment.