Skip to content

Commit

Permalink
Merge branch 'master' into module8-task1
Browse files Browse the repository at this point in the history
  • Loading branch information
n3wstar authored Jun 10, 2024
2 parents 6d217ae + 28215f7 commit e42e968
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/const.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isPointFuture, isPointPresent, isPointPast } from './utils.js';

const DEFAULT_TYPE = 'flight';
const DEFAULT_TYPE = 'Flight';

const EMPTY_POINT = {
basePrice: 0,
Expand Down
21 changes: 20 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import FilterPresenter from './presenter/filter-presenter.js';
import FiltersModel from './model/filter-model.js';
import PointsApiService from './service/points-api-service.js';


const bodyElement = document.querySelector('body');
const headerElement = bodyElement.querySelector('.page-header');
const tripInfoElement = headerElement.querySelector('.trip-main');
Expand Down Expand Up @@ -36,7 +37,8 @@ const boardPresenter = new BoardPresenter({
destinationsModel,
offersModel,
pointsModel,
filtersModel
filtersModel,
onNewPointDestroy: newPointFormCloseHandler
});

const filterPresenter = new FilterPresenter({
Expand All @@ -45,8 +47,25 @@ const filterPresenter = new FilterPresenter({
pointModel: pointsModel
});

const newPointComponent = new NewPointButtonView({

Check failure on line 50 in src/main.js

View workflow job for this annotation

GitHub Actions / Check

'NewPointButtonView' is not defined
onClick: newPointClickHandler
});

function newPointFormCloseHandler() {
newPointComponent.element.disabled = false;
}

function newPointClickHandler() {
boardPresenter.createPoint();
newPointComponent.element.disabled = true;
}

render(newPointComponent, tripInfoElement, RenderPosition.BEFOREEND);


render(new TripInfoView(), tripInfoElement, RenderPosition.AFTERBEGIN);


filterPresenter.init();
boardPresenter.init();
pointsModel.init();
Expand Down
19 changes: 18 additions & 1 deletion src/presenter/board-presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { UpdateType, UserAction } from '../const.js';
import { FilterOptions } from '../const.js';
import { RenderPosition } from '../render.js';


export default class BoardPresenter{
#sortComponent = null;
#eventListComponent = new EventListView();
Expand All @@ -19,19 +20,28 @@ export default class BoardPresenter{
#offersModel = null;
#pointsModel = null;
#filtersModel = null;
#newPointPresenter = null;
#pointPresenters = new Map();
#currentSortType = SortType.DAY;
#filterType = FilterOptions.EVERYTHING;

#isLoading = true;
#isLoadingError = false;

constructor({container, destinationsModel, offersModel, pointsModel, filtersModel}){

constructor({container, destinationsModel, offersModel, pointsModel, filtersModel, onNewPointDestroy}){
this.#container = container;
this.#destinationModel = destinationsModel;
this.#offersModel = offersModel;
this.#pointsModel = pointsModel;
this.#filtersModel = filtersModel;
this.#newPointPresenter = new NewPointPresenter({

Check failure on line 38 in src/presenter/board-presenter.js

View workflow job for this annotation

GitHub Actions / Check

'NewPointPresenter' is not defined
container: this.#eventListComponent,
destinationsModel: this.#destinationModel,
offersModel: this.#offersModel,
onDataChange: this.#userActionHandler,
onDestroy: onNewPointDestroy
});

this.#pointsModel.addObserver(this.#modelEventHandler);
this.#filtersModel.addObserver(this.#modelEventHandler);
Expand All @@ -56,6 +66,12 @@ export default class BoardPresenter{
return sortedPoints;
}

createPoint() {
this.#currentSortType = SortType.DAY;
this.#filtersModel.setFilter(UpdateType.MAJOR, FilterTypes.EVERYTHING);

Check failure on line 71 in src/presenter/board-presenter.js

View workflow job for this annotation

GitHub Actions / Check

'FilterTypes' is not defined
this.#newPointPresenter.init();
}

init(){
this.#renderBoard();
}
Expand Down Expand Up @@ -106,6 +122,7 @@ export default class BoardPresenter{
};

#clearPoints = ({resetSortType = false} = {}) => {
this.#newPointPresenter.destroy();
this.#pointPresenters.forEach((presenter) => presenter.destroy());
this.#pointPresenters.clear();

Expand Down
65 changes: 65 additions & 0 deletions src/presenter/new-point-presenter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {remove, render, RenderPosition } from '../framework/render';
import { UpdateType, UserAction } from '../const';
import FormEditView from '../view/form-edit-view';
export default class NewPointPresenter {
#container = null;
#destinationsModel = null;
#offersModel = null;
#editFormComponent = null;
#handleDataChange = null;
#handleDestroy = null;

constructor({ container, destinationsModel, offersModel, onDataChange, onDestroy }) {
this.#container = container;
this.#destinationsModel = destinationsModel;
this.#offersModel = offersModel;
this.#handleDataChange = onDataChange;
this.#handleDestroy = onDestroy;
}

init() {
if (this.#editFormComponent !== null) {
return;
}
this.#editFormComponent = new FormEditView({
pointDestination: this.#destinationsModel.getDestination(),
pointOffers: this.#offersModel.getOffers(),
isCreating: true,
onRollUpClick: this.#cancelClickHandler,
onSubmitClick: this.#formSubmitHandler,
onResetClick: this.#cancelClickHandler
});
render(this.#editFormComponent, this.#container.element, RenderPosition.AFTERBEGIN);
document.addEventListener('keydown', this.#escKeyDownHandler);
}

destroy() {
if (this.#editFormComponent === null) {
return;
}
remove(this.#editFormComponent);
this.#editFormComponent = null;
document.removeEventListener('keydown', this.#escKeyDownHandler);
this.#handleDestroy();
}


#formSubmitHandler = (point) => {
this.#handleDataChange(
UserAction.CREATE_POINT,
UpdateType.MINOR,
point
);
};

#cancelClickHandler = () => {
this.destroy();
};

#escKeyDownHandler = (evt) => {
if (evt.key === 'Escape' || evt.key === 'Esc') {
evt.preventDefault();
this.destroy();
}
};
}
7 changes: 4 additions & 3 deletions src/template/form-edit-markup.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import { TYPES } from '../const.js';
import { formatStringToDateTime } from '../utils.js';

Expand All @@ -13,11 +14,11 @@ function createPointDestinationListElement(destinations) {
return ( `${destinations.map((destination) => `<option value="${destination.name}"></option>`).join('')} `);
}

function createOffersTemplate(offers, selectedOffers) {
function createOffersTemplate(offers, selectedOffers, isDisabled) {
const offerItems = offers.offers.map((offer) => {
const offerName = offer.title.replace(' ', '').toLowerCase();
return (`<div class="event__offer-selector">
<input class="event__offer-checkbox visually-hidden" id="${offer.id}" type="checkbox" name="event-offer-${offerName}" ${selectedOffers?.offers?.map((of) => of.id).includes(offer.id) ? 'checked' : ''}>
<input class="event__offer-checkbox visually-hidden" id="${offer.id}" type="checkbox" name="event-offer-${offerName}" ${selectedOffers?.offers?.map((of) => of.id).includes(offer.id) ? 'checked' : ''} ${isDisabled ? '' : 'disabled'}>
<label class="event__offer-label" for="${offer.id}">
<span class="event__offer-title">${offer.title}</span>
&plus;&euro;&nbsp;
Expand Down Expand Up @@ -107,7 +108,7 @@ function CreateFormEditMarkup({state, pointDestination, pointOffers}){
<section class="event__section event__section--offers">
<h3 class="event__section-title event__section-title--offers">Offers</h3>
<div class="event__available-offers">
${createOffersTemplate(currentOffers, offers)}
${createOffersTemplate(currentOffers, offers, isDisabled)}
</div>
</section>
Expand Down

0 comments on commit e42e968

Please sign in to comment.