Skip to content

Commit

Permalink
6.6. Обновление века (часть 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
egorarud committed May 9, 2024
1 parent d383c53 commit 816bc9b
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/const.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const POINT_TYPE = ['taxi', 'bus', 'train', 'ship', 'drive', 'flight', 'check-in', 'sightseeing', 'restaurant'];
const CITIES = ['Amsterdam', 'Geneva', 'Chamonix', ];
const CITIES = ['Amsterdam', 'Geneva', 'Chamonix', 'Moscow', 'Paris'];
const DESCRIPTIONS = [
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
'Cras aliquet varius magna, non porta ligula feugiat eget.',
Expand Down
21 changes: 20 additions & 1 deletion src/mock/offers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const offers = [
id: 'of1',
title: 'Upgrade',
price: getRandomInteger(MIN_PRICE, MAX_PRICE)
},
{
id: 'of0',
title: 'NoCheked',
price: 0
}]
},
{
Expand All @@ -28,7 +33,21 @@ const offers = [
title: 'First Class',
price: getRandomInteger(MIN_PRICE, MAX_PRICE)
}]
}
},
{
type: 'train',
offers: [
{
id: 'of8765',
title: 'Upgrade',
price: getRandomInteger(MIN_PRICE, MAX_PRICE)
},
{
id: 'of34567654',
title: 'NoCheked',
price: 0
}]
},
];

export {offers};
4 changes: 4 additions & 0 deletions src/model/destinations-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ export default class DestinationsModel {
getDestinationById(id){
return this.#destinations.find((destination) => destination.id === id);
}

getDestinationByName(name){
return this.#destinations.find((destination) => destination.name === name);
}
}
4 changes: 4 additions & 0 deletions src/model/offers-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ export default class OffersModel {
getOfferByType(type){
return this.#offers.find((offer) => offer.type === type);
}

getOfferById(id){
return this.#offers.find((offer) =>offer.id === id);
}
}
16 changes: 6 additions & 10 deletions src/presenter/trip-events-presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ListEmptyView from '../view/list-empty-view.js';
import TripPointPresenter from './trip-point-presenter.js';
import {render} from '../framework/render.js';
import {LIST_EMPTY_TEXT} from '../const.js';
import { calculateDateDifference } from '../util.js';
import { calculateDateDifference, updateItem } from '../util.js';


export default class TripEventsPresenter {
Expand Down Expand Up @@ -47,12 +47,10 @@ export default class TripEventsPresenter {
}
}

#changePointFavorite = (point) => {
this.#points = this.#points.map((element) => element.id === point.id ? point : element);
#handlePointChange = (updatedPoint) => {
this.#points = updateItem(this.#points, updatedPoint);

const destination = this.#destinationsModel.getDestinationById(point.destination);
const offer = this.#offersModel.getOfferByType(point.type);
this.#pointPresenters.get(point.id).init(point, destination, offer);
this.#pointPresenters.get(updatedPoint.id).init(updatedPoint);
};

#changeSortType = (type) => {
Expand Down Expand Up @@ -109,10 +107,8 @@ export default class TripEventsPresenter {

for (let i = 0; i < points.length; i++) {
const point = points[i];
const destination = this.#destinationsModel.getDestinationById(point.destination);
const offer = this.#offersModel.getOfferByType(point.type);
const pointPresenter = new TripPointPresenter(this.#listComponent.element, this.#changePointFavorite, this.#changeViewHandler);
pointPresenter.init(point, destination, offer);
const pointPresenter = new TripPointPresenter(this.#listComponent.element, this.#changeViewHandler, this.#handlePointChange, this.#offersModel, this.#destinationsModel);
pointPresenter.init(point);
this.#pointPresenters.set(point.id, pointPresenter);
}
};
Expand Down
30 changes: 23 additions & 7 deletions src/presenter/trip-point-presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,34 @@ const VIEW = {

export default class TripPointPresenter {
#pointContainer = null;
#onFavoriteChange = null;
#onViewChange = null;
#onDataChange = null;

#pointComponent = null;
#editPointComponent = null;

#offersModel = null;
#destinationsModel = null;

#point = null;

#view = VIEW.DEFAULT;

constructor(pointContainer, onFavoriteChange, onViewChange) {
constructor(pointContainer, onViewChange, onDataChange, offersModel, destinationsModel) {
this.#pointContainer = pointContainer;
this.#onFavoriteChange = onFavoriteChange;
this.#onViewChange = onViewChange;
this.#onDataChange = onDataChange;

this.#offersModel = offersModel;
this.#destinationsModel = destinationsModel;
}

init(point, destination, offer) {
init(point) {
this.#point = point;

const destination = this.#destinationsModel.getDestinationById(this.#point.destination);
const offer = this.#offersModel.getOfferByType(this.#point.type);

const prevPointComponent = this.#pointComponent;

this.#pointComponent = new PointView({
Expand All @@ -46,7 +55,9 @@ export default class TripPointPresenter {
offer,
onDeleteButtonClick: this.#deleteButtonClikHandler,
onSubmitForm: this.#submitFormHandler,
onRollupButtonClick: this.#formRollupButtonClikHandler
onRollupButtonClick: this.#formRollupButtonClikHandler,
getDestinationByName: this.#getDestinationByName,
getOfferByType : this.#getOfferByType
});

if (prevPointComponent !== null && this.#pointContainer.contains(prevPointComponent.element)) {
Expand All @@ -58,7 +69,7 @@ export default class TripPointPresenter {
}

#favoriteButtonClickHandler = () => {
this.#onFavoriteChange({...this.#point, isFavorite: !this.#point.isFavorite});
this.#onDataChange({...this.#point, isFavorite: !this.#point.isFavorite});
};

#replacePointToForm() {
Expand Down Expand Up @@ -93,7 +104,8 @@ export default class TripPointPresenter {
document.removeEventListener('keydown', this.#onFormKeydown);
};

#submitFormHandler = () => {
#submitFormHandler = (point) => {
this.#onDataChange(point);
this.#replaceFormToPoint();
document.removeEventListener('keydown', this.#onFormKeydown);
};
Expand All @@ -109,4 +121,8 @@ export default class TripPointPresenter {
this.#replaceFormToPoint();
}
};

#getOfferByType = (type) => this.#offersModel.getOfferByType(type);

#getDestinationByName = (name) => this.#destinationsModel.getDestinationByName(name);
}
8 changes: 6 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ function createId() {
};
}

function updateItem(items, update) {
return items.map((item) => item.id === update.id ? update : item);
}

function getRandomInteger(a, b) {
const lower = Math.ceil(Math.min(a, b));
const upper = Math.floor(Math.max(a, b));
Expand All @@ -33,10 +37,10 @@ function calculateDateDifference(start, end) {
return end.diff(start, 'minute');
}

const isElementHas = (element) => element.length > 0;
const isElementHas = (element) => element !== null && element !== undefined && element.length > 0;

const getRandomArrayElement = (arr) => arr[getRandomInteger(0, arr.length - 1)];

const isEscapeKey = (evt) => evt.key === 'Escape';

export {createId, getRandomInteger, getRandomArrayElement, formatEventDate, isElementHas, isEscapeKey, calculateDateDifference, formatEventDuration};
export {createId, getRandomInteger, getRandomArrayElement, formatEventDate, isElementHas, isEscapeKey, calculateDateDifference, formatEventDuration, updateItem};
Loading

0 comments on commit 816bc9b

Please sign in to comment.