-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from fed1v/module6-task2
- Loading branch information
Showing
7 changed files
with
132 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {Sorting} from '../../consts.ts'; | ||
|
||
type SortOptionsItemProps = { | ||
option: Sorting; | ||
isActive: boolean; | ||
onItemClick: (option: Sorting) => void; | ||
} | ||
|
||
export function SortOptionsItem({option, isActive, onItemClick}: SortOptionsItemProps) { | ||
const className = isActive | ||
? 'places__option places__option--active' | ||
: 'places__option'; | ||
|
||
return ( | ||
<li | ||
className={className} | ||
tabIndex={0} | ||
onClick={() => onItemClick(option)} | ||
> | ||
{option} | ||
</li> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import {useState} from 'react'; | ||
import {Sorting} from '../../consts.ts'; | ||
import {SortOptionsItem} from './sort-options-item.tsx'; | ||
import {useAppSelector} from '../../hooks'; | ||
|
||
type SortOptionsProps = { | ||
options: Sorting[]; | ||
onSortingOptionChange: (sortingOption: Sorting) => void; | ||
} | ||
|
||
export function SortOptions({options, onSortingOptionChange}: SortOptionsProps) { | ||
const [isVisible, setIsVisible] = useState(false); | ||
|
||
const sorting = useAppSelector((state) => state.sorting); | ||
|
||
function handleSortingExpand() { | ||
setIsVisible((value) => !value); | ||
} | ||
|
||
function handleSortingOptionChange(option: Sorting) { | ||
onSortingOptionChange(option); | ||
setIsVisible(false); | ||
} | ||
|
||
return ( | ||
<form className="places__sorting" action="#" method="get"> | ||
<span className="places__sorting-caption">Sort by</span> | ||
<span | ||
className="places__sorting-type" | ||
tabIndex={0} | ||
onClick={() => handleSortingExpand()} | ||
> | ||
{sorting} | ||
<svg className="places__sorting-arrow" width="7" height="4"> | ||
<use xlinkHref="#icon-arrow-select"></use> | ||
</svg> | ||
</span> | ||
|
||
{isVisible && | ||
<ul className="places__options places__options--custom places__options--opened"> | ||
{ | ||
options.map((option) => ( | ||
<SortOptionsItem | ||
key={option} | ||
option={option} | ||
isActive={option === sorting} | ||
onItemClick={handleSortingOptionChange} | ||
/> | ||
)) | ||
} | ||
</ul>} | ||
|
||
</form> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import {createAction} from '@reduxjs/toolkit'; | ||
import {Offer} from '../types/offer.ts'; | ||
import {Sorting} from '../consts.ts'; | ||
|
||
export const selectCity = createAction<{ city: string }>('selectCity'); | ||
|
||
export const fillOffersList = createAction<{ offers: Offer[] }>('fillOffersList'); | ||
|
||
export const applySorting = createAction<{ sorting: Sorting }>('applySorting'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {Sorting} from '../consts.ts'; | ||
import {Offer} from '../types/offer.ts'; | ||
|
||
export function applySortingToOffersList(sorting: Sorting, offers: Offer[]) { | ||
switch (sorting) { | ||
case Sorting.POPULAR: { | ||
return offers; | ||
} | ||
case Sorting.PRICE_HIGH_TO_LOW: { | ||
return offers.toSorted((a, b) => b.price - a.price); | ||
} | ||
case Sorting.PRICE_LOW_TO_HIGH: { | ||
return offers.toSorted((a, b) => a.price - b.price); | ||
} | ||
case Sorting.TOP_RATED_FIRST: { | ||
return offers.toSorted((a, b) => a.rating - b.rating); | ||
} | ||
} | ||
} |