From e256574ec0f10b78c2760f0a1f937ac03e1406c4 Mon Sep 17 00:00:00 2001 From: Gurikov Maxim Date: Tue, 12 Nov 2024 14:36:50 +0500 Subject: [PATCH] change solution to use store --- src/Pages/main-page.tsx | 12 +++--------- src/components/offer/offer-sort-select.tsx | 20 +++++++++----------- src/store/actions.ts | 2 ++ src/store/store.ts | 6 +++++- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/Pages/main-page.tsx b/src/Pages/main-page.tsx index 3f7f3c6..bce7e19 100644 --- a/src/Pages/main-page.tsx +++ b/src/Pages/main-page.tsx @@ -1,5 +1,5 @@ /* eslint-disable react-hooks/exhaustive-deps */ -import React, { useEffect, useState } from 'react'; +import React, { useState } from 'react'; import { Offer } from '../dataTypes/offer.ts'; import { OffersList } from '../components/offer/offers-list.tsx'; import { Layout } from '../components/layout.tsx'; @@ -13,14 +13,10 @@ import { OfferSortSelect } from '../components/offer/offer-sort-select.tsx'; export function MainPage(): React.JSX.Element { const [activeOffer, setActiveOffer] = useState>(null); - const [offers, setOffers] = useState([]); const city = useAppSelector((state) => state.city); - const unsortedOffers = useAppSelector((state) => state.offers).filter( + const offers = useAppSelector((state) => state.sortedOffers).filter( (offer) => offer.city.name === city.name, ); - useEffect(() => { - setOffers(unsortedOffers); - }, [city]); return (
@@ -36,9 +32,7 @@ export function MainPage(): React.JSX.Element { {pluralizeAndCombine('place', offers.length)} to stay in{' '} {city.name} - setOffers(sort(offers))} - /> + ) => diff --git a/src/components/offer/offer-sort-select.tsx b/src/components/offer/offer-sort-select.tsx index 59872dc..2052f1b 100644 --- a/src/components/offer/offer-sort-select.tsx +++ b/src/components/offer/offer-sort-select.tsx @@ -1,35 +1,33 @@ import React, { useState } from 'react'; import { Offer } from '../../dataTypes/offer.ts'; import { SortOffers } from '../../dataTypes/sort-offers.ts'; - -interface OfferSortSelectProps { - onSortChange: (sort: SortOffers) => void; -} +import { useAppDispatch, useAppSelector } from '../../store/store.ts'; +import { setSortedOffers } from '../../store/actions.ts'; const sortingOptions: [string, SortOffers][] = [ ['Popular', (offers: Offer[]) => offers], [ 'Price: low to high', - (offers: Offer[]) => offers.sort((a, b) => a.price - b.price), + (offers: Offer[]) => offers.toSorted((a, b) => a.price - b.price), ], [ 'Price: high to low', - (offers: Offer[]) => offers.sort((a, b) => b.price - a.price), + (offers: Offer[]) => offers.toSorted((a, b) => b.price - a.price), ], [ 'Top rated first', - (offers: Offer[]) => offers.sort((a, b) => b.rating - a.rating), + (offers: Offer[]) => offers.toSorted((a, b) => b.rating - a.rating), ], ]; -export function OfferSortSelect({ - onSortChange, -}: OfferSortSelectProps): React.JSX.Element { +export function OfferSortSelect(): React.JSX.Element { const [isOpen, setIsOpen] = useState(false); const [sortingOption, setSortingOption] = useState('Popular'); + const offers = useAppSelector((state) => state.offers); + const dispatch = useAppDispatch(); const handleSortChange = (sort: SortOffers, sortingOptionName: string) => { setSortingOption(sortingOptionName); - onSortChange(sort); + dispatch(setSortedOffers(sort(offers))); setIsOpen(false); }; return ( diff --git a/src/store/actions.ts b/src/store/actions.ts index 9eb1142..728508b 100644 --- a/src/store/actions.ts +++ b/src/store/actions.ts @@ -5,3 +5,5 @@ import { Offer } from '../dataTypes/offer.ts'; export const changeCity = createAction('mainPage/changeCity'); export const fillOffers = createAction('mainPage/fillOffers'); + +export const setSortedOffers = createAction('mainPage/setSortedOffers'); diff --git a/src/store/store.ts b/src/store/store.ts index e797120..5028f89 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -1,5 +1,5 @@ import { configureStore, createReducer } from '@reduxjs/toolkit'; -import { changeCity, fillOffers } from './actions.ts'; +import { changeCity, fillOffers, setSortedOffers } from './actions.ts'; import { offerMocks } from '../mocks/offers.ts'; import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux'; import { AppDispatch, State } from '../dataTypes/store-types.ts'; @@ -8,6 +8,7 @@ import { PARIS } from '../consts/cities.ts'; const initialState = { city: PARIS, offers: offerMocks, + sortedOffers: offerMocks, }; const reducer = createReducer(initialState, (builder) => { @@ -17,6 +18,9 @@ const reducer = createReducer(initialState, (builder) => { }) .addCase(fillOffers, (state, action) => { state.offers = action.payload; + }) + .addCase(setSortedOffers, (state, action) => { + state.sortedOffers = action.payload; }); });