1- import FilterView from '../view/filter -view.js' ;
1+ import SortView from '../view/sort -view.js' ;
22import { render } from '../framework/render.js' ;
33import ListEventsView from '../view/list-events.js' ;
44import EventPresenter from './event-presenter.js' ;
55import { updateItem } from '../utils.js' ;
6+ import { SortType } from '../const.js' ;
7+ import { sortByPrice , sortByTime } from '../utils.js' ;
68
79import LackDataView from '../view/lack-data-view.js' ;
810export default class BoardPresenter {
911 #boardContainer = null ;
1012 #pointsModel = null ;
1113 #boardPoints = [ ] ;
1214 #eventsPresenter = new Map ( ) ;
15+ #currentEventType = SortType . DEFAULT ;
16+ #sourcedBoardEvents = [ ] ;
1317
1418 #listEventsComponent = new ListEventsView ( ) ;
19+ #sortComponent = null ;
1520
1621 constructor ( { boardContainer, pointsModel} ) {
1722 this . #boardContainer = boardContainer ;
@@ -20,10 +25,43 @@ export default class BoardPresenter {
2025
2126 init ( ) {
2227 this . #boardPoints = [ ...this . #pointsModel. getPoint ( ) ] ;
28+ this . #sourcedBoardEvents = [ ...this . #pointsModel. getPoint ( ) ] ;
2329
2430 this . #renderBoard( ) ;
2531 }
2632
33+ #handleSortTypeChange = ( sortType ) => {
34+ if ( this . #currentEventType === sortType ) {
35+ return ;
36+ }
37+ this . #sortEvents( sortType ) ;
38+ this . #clearEventList( ) ;
39+ this . #renderItemsEvent( ) ;
40+ } ;
41+
42+ #renderSort( ) {
43+ this . #sortComponent = new SortView ( {
44+ onSortTypeChange : this . #handleSortTypeChange
45+ } ) ;
46+
47+ render ( this . #sortComponent, this . #boardContainer) ;
48+ }
49+
50+ #sortEvents( sortType ) {
51+ switch ( sortType ) {
52+ case SortType . PRICE :
53+ this . #boardPoints. sort ( sortByPrice ) ;
54+ break ;
55+ case SortType . TIME :
56+ this . #boardPoints. sort ( sortByTime ) ;
57+ break ;
58+ default :
59+ this . #boardPoints = [ ...this . #sourcedBoardEvents] ;
60+ }
61+
62+ this . #currentEventType = sortType ;
63+ }
64+
2765 #renderNoEvent( ) {
2866 render ( new LackDataView ( ) , this . #boardContainer) ;
2967 }
@@ -50,8 +88,14 @@ export default class BoardPresenter {
5088 }
5189 }
5290
91+ #clearEventList( ) {
92+ this . #eventsPresenter. forEach ( ( presenter ) => presenter . destroy ( ) ) ;
93+ this . #eventsPresenter. clear ( ) ;
94+ }
95+
5396 #handleEventChange = ( updatedEvent ) => {
5497 this . #boardPoints = updateItem ( this . #boardPoints, updatedEvent ) ;
98+ this . #sourcedBoardEvents = updateItem ( this . #sourcedBoardEvents, updatedEvent ) ;
5599 this . #eventsPresenter. get ( updatedEvent . id ) . init ( this . #pointsModel, updatedEvent ) ;
56100 } ;
57101
@@ -61,7 +105,7 @@ export default class BoardPresenter {
61105 return ;
62106 }
63107
64- render ( new FilterView ( ) , this . #boardContainer ) ;
108+ this . #renderSort ( ) ;
65109 this . #renderItemsEvent( ) ;
66110 }
67111}
0 commit comments