From 80ec62137aff2b7c72e627467ef119bf83f1beb3 Mon Sep 17 00:00:00 2001 From: Gurikov Maxim Date: Tue, 12 Nov 2024 00:27:12 +0500 Subject: [PATCH] done task --- src/Pages/main-page.tsx | 40 ++++---------- src/components/offer/offer-sort-select.tsx | 64 ++++++++++++++++++++++ src/dataTypes/sort-offers.ts | 3 + src/mocks/offers.ts | 8 +-- 4 files changed, 82 insertions(+), 33 deletions(-) create mode 100644 src/components/offer/offer-sort-select.tsx create mode 100644 src/dataTypes/sort-offers.ts diff --git a/src/Pages/main-page.tsx b/src/Pages/main-page.tsx index 75ef3f0..3f7f3c6 100644 --- a/src/Pages/main-page.tsx +++ b/src/Pages/main-page.tsx @@ -1,4 +1,5 @@ -import React, { useState } from 'react'; +/* eslint-disable react-hooks/exhaustive-deps */ +import React, { useEffect, useState } from 'react'; import { Offer } from '../dataTypes/offer.ts'; import { OffersList } from '../components/offer/offers-list.tsx'; import { Layout } from '../components/layout.tsx'; @@ -8,14 +9,18 @@ import { Map } from '../components/map/map.tsx'; import { useAppSelector } from '../store/store.ts'; import { CitiesList } from '../components/cities-list.tsx'; import { pluralizeAndCombine } from '../utils/string-utils.ts'; +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 offers = useAppSelector((state) => state.offers).filter( + const unsortedOffers = useAppSelector((state) => state.offers).filter( (offer) => offer.city.name === city.name, ); - + useEffect(() => { + setOffers(unsortedOffers); + }, [city]); return (
@@ -31,32 +36,9 @@ export function MainPage(): React.JSX.Element { {pluralizeAndCombine('place', offers.length)} to stay in{' '} {city.name} -
- Sort by - - Popular - - - - -
    -
  • - Popular -
  • -
  • - Price: low to high -
  • -
  • - Price: high to low -
  • -
  • - Top rated first -
  • -
-
+ setOffers(sort(offers))} + /> ) => diff --git a/src/components/offer/offer-sort-select.tsx b/src/components/offer/offer-sort-select.tsx new file mode 100644 index 0000000..59872dc --- /dev/null +++ b/src/components/offer/offer-sort-select.tsx @@ -0,0 +1,64 @@ +import React, { useState } from 'react'; +import { Offer } from '../../dataTypes/offer.ts'; +import { SortOffers } from '../../dataTypes/sort-offers.ts'; + +interface OfferSortSelectProps { + onSortChange: (sort: SortOffers) => void; +} + +const sortingOptions: [string, SortOffers][] = [ + ['Popular', (offers: Offer[]) => offers], + [ + 'Price: low to high', + (offers: Offer[]) => offers.sort((a, b) => a.price - b.price), + ], + [ + 'Price: high to low', + (offers: Offer[]) => offers.sort((a, b) => b.price - a.price), + ], + [ + 'Top rated first', + (offers: Offer[]) => offers.sort((a, b) => b.rating - a.rating), + ], +]; + +export function OfferSortSelect({ + onSortChange, +}: OfferSortSelectProps): React.JSX.Element { + const [isOpen, setIsOpen] = useState(false); + const [sortingOption, setSortingOption] = useState('Popular'); + const handleSortChange = (sort: SortOffers, sortingOptionName: string) => { + setSortingOption(sortingOptionName); + onSortChange(sort); + setIsOpen(false); + }; + return ( +
+ Sort by + setIsOpen(!isOpen)} + > + {sortingOption} + + + + + {isOpen && ( +
    + {sortingOptions.map(([name, sort]) => ( +
  • handleSortChange(sort, name)} + > + {name} +
  • + ))} +
+ )} +
+ ); +} diff --git a/src/dataTypes/sort-offers.ts b/src/dataTypes/sort-offers.ts new file mode 100644 index 0000000..4aac267 --- /dev/null +++ b/src/dataTypes/sort-offers.ts @@ -0,0 +1,3 @@ +import { Offer } from './offer.ts'; + +export type SortOffers = (offers: Offer[]) => Offer[]; diff --git a/src/mocks/offers.ts b/src/mocks/offers.ts index b3cd5ca..ed92151 100644 --- a/src/mocks/offers.ts +++ b/src/mocks/offers.ts @@ -1,5 +1,5 @@ -import {Offer} from '../dataTypes/offer.ts'; -import {RoomType} from '../dataTypes/enums/room-type.ts'; +import { Offer } from '../dataTypes/offer.ts'; +import { RoomType } from '../dataTypes/enums/room-type.ts'; export const offerMocks: Offer[] = [ { @@ -45,7 +45,7 @@ export const offerMocks: Offer[] = [ }, isFavorite: false, isPremium: true, - rating: 4, + rating: 3, previewImage: 'apartment-02.jpg', }, { @@ -91,7 +91,7 @@ export const offerMocks: Offer[] = [ }, isFavorite: true, isPremium: true, - rating: 4, + rating: 5, previewImage: 'apartment-02.jpg', }, ];