Skip to content

Commit

Permalink
Merge pull request #9 from dashuulka/module6-task1
Browse files Browse the repository at this point in the history
  • Loading branch information
keksobot authored May 23, 2024
2 parents 112c0c3 + a6881c1 commit fe8d8b5
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 31 deletions.
3 changes: 2 additions & 1 deletion src/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ const SortType = [{
}];

export {POINT_EMPTY,
POINT_TYPE, DESTINATION,
POINT_TYPE,
DESTINATION,
DESCRIPTION,
OFFERS,
DESTINATION_COUNT,
Expand Down
1 change: 1 addition & 0 deletions src/presenter/point-presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export default class PointPresenter {

resetView() {
if (this.#mode !== Mode.DEFAULT) {
this.#pointEditComponent.reset(this.#point);
this.#replaceEditToPoint();
}
}
Expand Down
33 changes: 20 additions & 13 deletions src/template/editing-form-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => `<div class="event__type-item">
<input id="event-type-${type.toLowerCase()}-1" class="event__type-input visually-hidden" type="radio" name="event-type" value="${type.toLowerCase()}">
<label class="event__type-label event__type-label--${type.toLowerCase()}" for="event-type-${type.toLowerCase()}-1">${type}</label>
</div>`).join('');
}

function createPointOffer() {
return OFFERS.map((offer) => `<div class="event__offer-selector">
<input class="event__offer-checkbox visually-hidden" id="event-offer-${offer.toLowerCase()}-1" type="checkbox" name="event-offer-${offer.toLowerCase()}" checked>
<label class="event__offer-label" for="event-offer-${offer.toLowerCase()}-1">
<span class="event__offer-title">Add ${offer.toLowerCase()}</span>
&plus;&euro;&nbsp;
<span class="event__offer-price">${getRandomValue()}</span>
</label>
</div>`).join('');
}

function createEditPointTemplate({point}) {
return `<li class="trip-events__item">
<form class="event event--edit" action="#" method="post">
Expand All @@ -16,11 +34,7 @@ function createEditPointTemplate({point}) {
<div class="event__type-list">
<fieldset class="event__type-group">
<legend class="visually-hidden">Event type</legend>
${POINT_TYPE.map((type) => `<div class="event__type-item">
<input id="event-type-${type.toLowerCase()}-1" class="event__type-input visually-hidden" type="radio" name="event-type" value="${type.toLowerCase()}">
<label class="event__type-label event__type-label--${type.toLowerCase()}" for="event-type-${type.toLowerCase()}-1">${type}</label>
</div>`).join('')}
${createPointType()}
</fieldset>
</div>
</div>
Expand Down Expand Up @@ -64,14 +78,7 @@ function createEditPointTemplate({point}) {
<h3 class="event__section-title event__section-title--offers">Offers</h3>
<div class="event__available-offers">
${OFFERS.map((offer) => `<div class="event__offer-selector">
<input class="event__offer-checkbox visually-hidden" id="event-offer-${offer.toLowerCase()}-1" type="checkbox" name="event-offer-${offer.toLowerCase()}" checked>
<label class="event__offer-label" for="event-offer-${offer.toLowerCase()}-1">
<span class="event__offer-title">Add ${offer.toLowerCase()}</span>
&plus;&euro;&nbsp;
<span class="event__offer-price">${getRandomValue()}</span>
</label>
</div>`).join('')}
${createPointOffer()}
</div>
</section>
Expand Down
112 changes: 95 additions & 17 deletions src/view/editing-form-view.js
Original file line number Diff line number Diff line change
@@ -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});
}

0 comments on commit fe8d8b5

Please sign in to comment.