Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Большие перемены (часть 2) #11

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 127 additions & 8 deletions src/view/modpoint.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import AbstractView from '../framework/view/abstract-view.js';
import { POINT_TYPES, CITIES, POINT_EMPTY } from '../const.js';
import flatpickr from 'flatpickr';
import 'flatpickr/dist/flatpickr.min.css';
import { firstLetterToUpperCase, firstLetterToLowerCase,formatStringToDateTime } from '../utils.js';

function createPointTypesListElement(currentType) {
Expand Down Expand Up @@ -103,6 +105,9 @@ export default class EditablePointView extends AbstractView {
#pointOffers = null;
#onEditSubmit = null;
#onRollupClick = null;
#datepickerFrom = null;
#datepickerTo = null;

constructor({point = POINT_EMPTY, pointDestination, pointOffers, onEditSubmit, onResetClick}) {
super();
this.#point = point;
Expand All @@ -111,10 +116,8 @@ export default class EditablePointView extends AbstractView {
this.#onEditSubmit = onEditSubmit;
this.#onRollupClick = onResetClick;

this.element.querySelector('.event--edit')
.addEventListener('submit', this.#editSubmitHandler);
this.element.querySelector('.event__reset-btn')
.addEventListener('click', this.#resetClickHandler);
this._setState(EditablePointView.parseEventToState(point));
this._restoreHandlers();
}

get template() {
Expand All @@ -125,13 +128,129 @@ export default class EditablePointView extends AbstractView {
});
}

removeElement() {
super.removeElement();

if (this.#datepickerFrom) {
this.#datepickerFrom.destroy();
this.#datepickerFrom = null;
}

if (this.#datepickerTo) {
this.#datepickerTo.destroy();
this.#datepickerTo = null;
}
}
reset(event) {
this.updateElement(
EditablePointView.parseEventToState(event),
);
}

_restoreHandlers() {
this.element.querySelector('.event--edit')
.addEventListener('submit', this.#editSubmitHandler);
this.element.querySelector('.event__rollup-btn')
.addEventListener('click', this.#rollupClickHandler);
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--destination')
.addEventListener('change', this.#destinationChangeHandler);
this.element.querySelector('.event__input--price')
.addEventListener('change', this.#priceChangeHandler);

this.#setDatepickers();
}

#dateFromCloseHandler = ([userDate]) => {
this._setState({
...this._state,
dateFrom: userDate
});

this.#datepickerTo.set('minDate', this._state.dateFrom);
};

#dateToCloseHandler = ([userDate]) => {
this._setState({
...this._state,
dateTo: userDate
});

this.#datepickerFrom.set('maxDate', this._state.dateTo);
};

#setDatepickers() {
const [dateFromElement, dateToElement] = this.element.querySelectorAll('.event__input--time');
const commonConfig = {
dateFormat: 'd/m/y H:i',
enableTime: true,
locale: {
firstDayOfWeek: 1,
},
'time_24hr': true
};

this.#datepickerFrom = flatpickr(
dateFromElement,
{
...commonConfig,
defaultDate: this._state.dateFrom,
onClose: this.#dateFromCloseHandler,
maxDate: this._state.dateTo,
},
);

this.#datepickerTo = flatpickr(
dateToElement,
{
...commonConfig,
defaultDate: this._state.dateTo,
onClose: this.#dateToCloseHandler,
minDate: this._state.dateFrom,
},
);
}

#editSubmitHandler = (evt) => {
evt.preventDefault();
this.#onEditSubmit(this.#point);
this.#onEditSubmit(EventEditView.parseStateToEvent(this._state));
};

#resetClickHandler = (evt) => {
#rollupClickHandler = (evt) => {
evt.preventDefault();
this.#onRollupClick();
};
}
#typeChangeHandler = (evt) => {
this.updateElement({
type: evt.target.value,
offers: [],
});
};
#offerChangeHandler = () => {
const checkedBoxes = Array.from(this.element.querySelectorAll('.event__offer-checkbox:checked'));
this._setState({
...this._state,
offers: checkedBoxes.map((element) => element.id),
});
};
#destinationChangeHandler = (evt) => {
const selectedDestination = this.#pointDestination.find((destination) => destination.name === evt.target.value);
this.updateElement({
destination: (selectedDestination) ? selectedDestination.id : null,
});
};
#priceChangeHandler = (evt) => {
this._setState({
...this._state,
price: evt.target.value,
});
};
static parseEventToState(event) {
return {...event};
}
static parseStateToEvent(state) {
return {...state};
}
}
Loading