diff --git a/src/mock/destination.js b/src/mock/destination.js index 791a6a6..0849dde 100644 --- a/src/mock/destination.js +++ b/src/mock/destination.js @@ -11,7 +11,7 @@ export const getRandomDestination = () => { description: description, pictures: [ { - 'scr': `https://loremflickr.com/248/152?random=${crypto.randomUUID()}`, + 'src': `https://loremflickr.com/248/152?random=${crypto.randomUUID()}`, 'description' : `${city} description` } ] diff --git a/src/presenter/point-presenter.js b/src/presenter/point-presenter.js index c97d5cd..9cf4ab0 100644 --- a/src/presenter/point-presenter.js +++ b/src/presenter/point-presenter.js @@ -47,8 +47,8 @@ export default class PointPresenter { this.#editPointElement = new PointEditView({ point: this.#point, - pointDestination: this.#destinationsModel.getById(point.destination), - pointOffers: this.#offersModel.getByType(point.type), + destinations: this.#destinationsModel.getAll(), + offers: this.#offersModel.getAll(), onCloseEditPoint: this.#onCloseEditClick, onSubmiClick: this.#onSubmiClick, }); @@ -94,6 +94,7 @@ export default class PointPresenter { resetView = () =>{ if(this.#mode !== Mode.DEFAULT){ this.#replaceToPoint(); + this.#editPointElement.reset(this.#point); } }; diff --git a/src/view/point-edit-view.js b/src/view/point-edit-view.js index 548223c..72731ed 100644 --- a/src/view/point-edit-view.js +++ b/src/view/point-edit-view.js @@ -1,5 +1,5 @@ import { POINT_EMPTY, TYPES } from '../const.js'; -import AbstractView from '../framework/view/abstract-view.js'; +import AbstractStatefulView from '../framework/view/abstract-stateful-view.js'; import {formatToSlashDate} from '../utils.js'; function createTypesElements(typeArray){ @@ -7,8 +7,8 @@ function createTypesElements(typeArray){ let typesElements = ''; typeArray.forEach((type) => { typesElements += `
- - + +
`; }); @@ -16,15 +16,33 @@ function createTypesElements(typeArray){ } -function createOfferSelector(offersArray){ +function createDestinationPhotos(pointDestination){ + let photos = '
'; + pointDestination.pictures.forEach((picture) =>{ + photos += `${picture.description}`; + }); + return `${photos}
`; +} + +function createDestinationList(destinations){ + let dataset = ''; + destinations.forEach((destination) =>{ + dataset += ``; + }); + return `${dataset}`; + +} +function createOfferSelector(pointOffers, offersArray){ let offersElements = `
-

Offers

`; +

Offers

+
`; - offersArray.offers.forEach((offer, i) => { + offersArray.forEach((offer) => { + const checked = pointOffers.some((offerId) => offerId === offer.id) ? 'checked' : ''; offersElements += `
- -
`; }); - offersElements += '
'; + offersElements += ''; return offersElements; } -function createPointEditElement({point, pointDestination, pointOffers}) { +function createPointEditElement({point, destinations, offers}) { + const pointDestination = destinations.find((destination) => destination.id === point.destination); + const pointOffers = offers.find((subOffers) => subOffers.type === point.type).offers; const {basePrice, dateFrom, dateTo, type} = point; @@ -65,11 +85,7 @@ function createPointEditElement({point, pointDestination, pointOffers}) { ${type} - - - - - + ${createDestinationList(destinations)}
@@ -95,49 +111,69 @@ function createPointEditElement({point, pointDestination, pointOffers}) {
- ${createOfferSelector(pointOffers)} + ${createOfferSelector(point.offers, pointOffers)}

Destination

${pointDestination.description}

+
+ ${createDestinationPhotos(pointDestination)} +
`; } -export default class EditPointView extends AbstractView{ +export default class EditPointView extends AbstractStatefulView{ - #pointDestination; - #pointOffers; - #point; + #destinations; + #offers; #onCloseEditPoint; #onSubmiClick; - constructor({point = POINT_EMPTY, pointDestination, pointOffers, onCloseEditPoint, onSubmiClick}) { + constructor({point = POINT_EMPTY, destinations, offers, onCloseEditPoint, onSubmiClick}) { super(); - this.#point = point; - this.#pointDestination = pointDestination; - this.#pointOffers = pointOffers; + this.#destinations = destinations; + this.#offers = offers; this.#onCloseEditPoint = onCloseEditPoint; this.#onSubmiClick = onSubmiClick; - this.#closeEditPoint(); + this._setState(EditPointView.parsePointToState({point})); this.#submitEditPoint(); + this._restoreHandlers(); } + _restoreHandlers = () =>{ + this.element + .querySelector('.event__rollup-btn') + .addEventListener('click', this.#closeEditPointHandler); + + this.element + .querySelector('.event__input--destination') + .addEventListener('change', this.#onDestinationChange); + + this.element + .querySelector('.event__available-offers') + .addEventListener('change', this.#offerChangeHandler); + + this.element + .querySelector('.event__input--price') + .addEventListener('change', this.#priceChangeHandler); + + this.element + .querySelector('.event__type-group') + .addEventListener('change', this.#typeChangeHandler); + }; + get template() { return createPointEditElement({ - point: this.#point, - pointDestination: this.#pointDestination, - pointOffers: this.#pointOffers + point: this._state.point, + destinations: this.#destinations, + offers: this.#offers }); } - #closeEditPoint = () => { - this.element - .querySelector('.event__rollup-btn') - .addEventListener('click', this.#closeEditPointHandler); - }; + reset = (point) => this.updateElement({ point }); #submitEditPoint = () =>{ this.element @@ -145,6 +181,50 @@ export default class EditPointView extends AbstractView{ .addEventListener('submit', this.#submiClickHandler); }; + #onDestinationChange = (evt) => { + const newDestinationName = evt.target.value; + const newDestination = this.#destinations.find((dest) => dest.name === newDestinationName); + this.updateElement({ + point: { + ...this._state.point, + destination: newDestination.id, + } + }); + }; + + #offerChangeHandler = () => { + const selectedOffers = Array.from(this.element.querySelectorAll('.event__offer-checkbox:checked')) + .map(({id}) => id.split('-').slice(3).join('-')); + + this._setState({ + point: { + ...this._state.point, + offers: selectedOffers + } + }); + }; + + #priceChangeHandler = (evt) => { + this._setState({ + point: { + ...this._state.point, + basePrice: evt.target.value, + } + }); + }; + + + #typeChangeHandler = (evt) => { + evt.preventDefault(); + this.updateElement({ + point: { + ...this._state.point, + type: evt.target.value, + offers: [], + }, + }); + }; + #closeEditPointHandler = (evt) => { evt.preventDefault(); this.#onCloseEditPoint(); @@ -154,4 +234,7 @@ export default class EditPointView extends AbstractView{ evt.preventDefault(); this.#onSubmiClick(); }; + + static parsePointToState = ({ point }) => ({ point }); + static parseStateToPoint = (state) => state.point; } diff --git a/src/view/point-view.js b/src/view/point-view.js index b03d459..430bb8e 100644 --- a/src/view/point-view.js +++ b/src/view/point-view.js @@ -3,9 +3,9 @@ import AbstractView from '../framework/view/abstract-view.js'; import{formatStringDateTime, getPointDuration, formatStringTime, formatStringDate} from '../utils.js'; -function createOfferElements(pointOffers) { +function createOfferElements(pointOffers, selectedOffers) { let elements = ''; - pointOffers.offers.forEach((offer) => { + pointOffers.offers.filter((offer) => selectedOffers.includes(offer.id)).forEach((offer) => { elements += `
  • ${offer.title} +€  @@ -23,7 +23,7 @@ function createPointElement({point, pointDestination, pointOffers}) {
    - Event type icon + Event type icon

    ${type} ${pointDestination.name}

    @@ -39,7 +39,7 @@ function createPointElement({point, pointDestination, pointOffers}) {

    Offers:

      - ${createOfferElements(pointOffers)} + ${createOfferElements(pointOffers, point.offers)}