diff --git a/src/Pages/main-page.tsx b/src/Pages/main-page.tsx index bce7e19..907666f 100644 --- a/src/Pages/main-page.tsx +++ b/src/Pages/main-page.tsx @@ -14,9 +14,11 @@ import { OfferSortSelect } from '../components/offer/offer-sort-select.tsx'; export function MainPage(): React.JSX.Element { const [activeOffer, setActiveOffer] = useState>(null); const city = useAppSelector((state) => state.city); - const offers = useAppSelector((state) => state.sortedOffers).filter( + const unsortedOffers = useAppSelector((state) => state.offers).filter( (offer) => offer.city.name === city.name, ); + const sort = useAppSelector((state) => state.sorting); + const offers = sort(unsortedOffers); return (
diff --git a/src/components/offer/offer-sort-select.tsx b/src/components/offer/offer-sort-select.tsx index 2052f1b..9648616 100644 --- a/src/components/offer/offer-sort-select.tsx +++ b/src/components/offer/offer-sort-select.tsx @@ -1,8 +1,8 @@ import React, { useState } from 'react'; import { Offer } from '../../dataTypes/offer.ts'; import { SortOffers } from '../../dataTypes/sort-offers.ts'; -import { useAppDispatch, useAppSelector } from '../../store/store.ts'; -import { setSortedOffers } from '../../store/actions.ts'; +import { useAppDispatch } from '../../store/store.ts'; +import { setSorting } from '../../store/actions.ts'; const sortingOptions: [string, SortOffers][] = [ ['Popular', (offers: Offer[]) => offers], @@ -23,11 +23,10 @@ const sortingOptions: [string, SortOffers][] = [ 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); - dispatch(setSortedOffers(sort(offers))); + dispatch(setSorting(sort)); setIsOpen(false); }; return ( diff --git a/src/mocks/offers.ts b/src/mocks/offers.ts index ed92151..87a63c7 100644 --- a/src/mocks/offers.ts +++ b/src/mocks/offers.ts @@ -94,4 +94,27 @@ export const offerMocks: Offer[] = [ rating: 5, previewImage: 'apartment-02.jpg', }, + { + id: '6af6f711-c28d-4121-82cd-e0b462a27f44', + title: 'amogus', + type: RoomType.Apartment, + price: 88, + city: { + name: 'Paris', + location: { + latitude: 52.35514938496378, + longitude: 4.673877537499948, + zoom: 8, + }, + }, + location: { + latitude: 48.9, + longitude: 2.4, + zoom: 8, + }, + isFavorite: true, + isPremium: true, + rating: 5, + previewImage: 'apartment-02.jpg', + }, ]; diff --git a/src/store/actions.ts b/src/store/actions.ts index 728508b..156369e 100644 --- a/src/store/actions.ts +++ b/src/store/actions.ts @@ -1,9 +1,10 @@ import { createAction } from '@reduxjs/toolkit'; import { City } from '../dataTypes/city.ts'; import { Offer } from '../dataTypes/offer.ts'; +import { SortOffers } from '../dataTypes/sort-offers.ts'; export const changeCity = createAction('mainPage/changeCity'); export const fillOffers = createAction('mainPage/fillOffers'); -export const setSortedOffers = createAction('mainPage/setSortedOffers'); +export const setSorting = createAction('mainPage/setSorting'); diff --git a/src/store/store.ts b/src/store/store.ts index 5028f89..c9c756a 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -1,14 +1,15 @@ import { configureStore, createReducer } from '@reduxjs/toolkit'; -import { changeCity, fillOffers, setSortedOffers } from './actions.ts'; +import {changeCity, fillOffers, setSorting} from './actions.ts'; import { offerMocks } from '../mocks/offers.ts'; import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux'; import { AppDispatch, State } from '../dataTypes/store-types.ts'; import { PARIS } from '../consts/cities.ts'; +import {Offer} from '../dataTypes/offer.ts'; const initialState = { city: PARIS, offers: offerMocks, - sortedOffers: offerMocks, + sorting: (offers: Offer[]) => offers, }; const reducer = createReducer(initialState, (builder) => { @@ -19,8 +20,8 @@ const reducer = createReducer(initialState, (builder) => { .addCase(fillOffers, (state, action) => { state.offers = action.payload; }) - .addCase(setSortedOffers, (state, action) => { - state.sortedOffers = action.payload; + .addCase(setSorting, (state, action) => { + state.sorting = action.payload; }); });