diff --git a/src/const.js b/src/const.js index 05f4b9e..7edc280 100644 --- a/src/const.js +++ b/src/const.js @@ -76,7 +76,8 @@ const SortType = [{ }]; export {POINT_EMPTY, - POINT_TYPE, DESTINATION, + POINT_TYPE, + DESTINATION, DESCRIPTION, OFFERS, DESTINATION_COUNT, diff --git a/src/presenter/point-presenter.js b/src/presenter/point-presenter.js index 8d90326..0fc8be5 100644 --- a/src/presenter/point-presenter.js +++ b/src/presenter/point-presenter.js @@ -83,6 +83,7 @@ export default class PointPresenter { resetView() { if (this.#mode !== Mode.DEFAULT) { + this.#pointEditComponent.reset(this.#point); this.#replaceEditToPoint(); } } diff --git a/src/template/editing-form-template.js b/src/template/editing-form-template.js index 15fc8ad..b65d676 100644 --- a/src/template/editing-form-template.js +++ b/src/template/editing-form-template.js @@ -2,6 +2,24 @@ import { POINT_TYPE, OFFERS } from '../const'; import { formatFullDate } from '../utils/day'; import { getRandomValue } from '../utils/common'; +function createPointType() { + return POINT_TYPE.map((type) => `
+ + +
`).join(''); +} + +function createPointOffer() { + return OFFERS.map((offer) => `
+ + +
`).join(''); +} + function createEditPointTemplate({point}) { return `
  • @@ -16,11 +34,7 @@ function createEditPointTemplate({point}) {
    Event type - - ${POINT_TYPE.map((type) => `
    - - -
    `).join('')} + ${createPointType()}
    @@ -64,14 +78,7 @@ function createEditPointTemplate({point}) {

    Offers

    - ${OFFERS.map((offer) => `
    - - -
    `).join('')} + ${createPointOffer()}
    diff --git a/src/view/editing-form-view.js b/src/view/editing-form-view.js index 87a63c2..33072da 100644 --- a/src/view/editing-form-view.js +++ b/src/view/editing-form-view.js @@ -1,46 +1,124 @@ -import AbstractView from '../framework/view/abstract-view.js'; +import AbstractStatefulView from '../framework/view/abstract-stateful-view.js'; import { createEditPointTemplate } from '../template/editing-form-template.js'; import { POINT_EMPTY } from '../const.js'; -export default class EditPointView extends AbstractView { - #point = null; +export default class EditPointView extends AbstractStatefulView { + //#point = null; #pointDestinations = null; #pointOffers = null; - #onResetClick = null; - #onSubmitClick = null; + #handleResetClick = null; + #handleSubmitClick = null; constructor({point = POINT_EMPTY, pointDestinations, pointOffers, onResetClick, onSubmitClick}) { super(); - this.#point = point; + //this.#point = point; this.#pointDestinations = pointDestinations; this.#pointOffers = pointOffers; - this.#onResetClick = onResetClick; - this.#onSubmitClick = onSubmitClick; + this.#handleResetClick = onResetClick; + this.#handleSubmitClick = onSubmitClick; - this.element - .querySelector('form') - .addEventListener('submit', this.#formSubmitHandler); + this._setState(EditPointView.parsePointToState({point})); - this.element - .querySelector('.event__rollup-btn') - .addEventListener('click', this.#resetButtonClickHandler); + this._restoreHandlers(); } get template() { return createEditPointTemplate({ - point: this.#point, + point: this._state, pointDestinations: this.#pointDestinations, pointOffers: this.#pointOffers }); } + reset({point}) { + this.updateElement( + EditPointView.parsePointToState({point}), + ); + } + + _restoreHandlers = () => { + this.element + .querySelector('form') + .addEventListener('submit', this.#formSubmitHandler); + + this.element + .querySelector('.event__rollup-btn') + .addEventListener('click', this.#resetButtonClickHandler); + + this.element + .querySelector('.event__type-group') + .addEventListener('change', this.#typeChangeHandler); + + this.element + .querySelector('.event__available-offers') + .addEventListener('change', this.#offerChangeHandler); + + this.element + .querySelector('.event__input--price') + .addEventListener('change', this.#priceChangeHandler); + + this.element + .querySelector('.event__input--destination') + .addEventListener('change', this.#destinationChangeHandler); + }; + #formSubmitHandler = (evt) => { evt.preventDefalt(); - this.#onSubmitClick(this.#point); + this.#handleSubmitClick(EditPointView.parseStateToPoint(this._state)); }; #resetButtonClickHandler = (evt) => { evt.preventDefalt(); - this.#onResetClick(this.#point); + this.#handleResetClick(EditPointView.parseStateToPoint(this._state)); + }; + + #typeChangeHandler = (evt) => { + evt.preventDefalt(); + this.updateElement( { + point: { + ...this._state.point, + type: evt.target.value, + offers: [] + } + }); + }; + + #offerChangeHandler = (evt) => { + evt.preventDefalt(); + + const checkedOffers = Array.from(this.element.querySelectorAll('.event__offer-checkbox:checked')); + this._setState({ + point: { + ...this._state.point, + offers: checkedOffers.map((element) => element.dataset.offerId) + } + }); + }; + + #priceChangeHandler = (evt) => { + evt.preventDefalt(); + this._setState({ + point: { + ...this._state.point, + basePrice: evt.target.valueAsNumber + } + }); }; + + #destinationChangeHandler = (evt) => { + evt.preventDefalt(); + const selectedDestination = this.#pointDestinations + .find((pointDestination) => pointDestination.name === evt.target.value); + + const selectedDestinationId = (selectedDestination) ? selectedDestination.id : null; + this.updateElement({ + point: { + ...this._state.point, + destination: selectedDestinationId + } + }); + }; + + static parsePointToState = (point) => ({point}); + static parseStateToPoint = (state) => ({...state}); }