Skip to content

Commit

Permalink
Merge pull request #9 from Borisod/module6-task2
Browse files Browse the repository at this point in the history
  • Loading branch information
keksobot authored Jan 26, 2025
2 parents 7df3266 + b8d0758 commit 4cf266b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"dependencies": {
"dayjs": "1.11.13",
"flatpickr": "4.6.13",
"style-loader": "^3.3.2"
}
}
43 changes: 43 additions & 0 deletions src/view/editing-form-view.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { humanizeDueTime, capitalize } from '../utils.js';
import AbstractStatefulView from '../framework/view/abstract-stateful-view.js';
import flatpickr from 'flatpickr';
import 'flatpickr/dist/flatpickr.min.css';

function createEventTypeTemplate(eventTypes, eventId, type) {
return eventTypes.map((eventType) => (
Expand Down Expand Up @@ -127,6 +129,8 @@ export default class EditingFormView extends AbstractStatefulView {
#offers = [];
#onSubmit = null;
#onClick = null;
#flatpickrStart = null;
#flatpickrEnd = null;

constructor({ event, destinations, offers, onSubmit, onClick, events }) {
super();
Expand Down Expand Up @@ -154,6 +158,7 @@ export default class EditingFormView extends AbstractStatefulView {
input.addEventListener('change', this.#onEventTypeChange)
);
this.element.querySelector('.event__input--destination')?.addEventListener('change', this.#onDestinationChange);
this.#setFlatpickr();
}

#onSubmitClick = (evt) => {
Expand Down Expand Up @@ -193,5 +198,43 @@ export default class EditingFormView extends AbstractStatefulView {
...state
};
}

#setFlatpickr() {
const { dateFrom, dateTo } = this._state;

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

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

this.#flatpickrStart = flatpickr(
this.element.querySelector(`#event-start-time-${this._state.id}`),
{
enableTime: true,
dateFormat: 'd/m/y H:i',
defaultDate: dateFrom,
onChange: ([selectedDate]) => {
this.updateElement({ dateFrom: selectedDate });
}
}
);

this.#flatpickrEnd = flatpickr(
this.element.querySelector(`#event-end-time-${this._state.id}`),
{
enableTime: true,
dateFormat: 'd/m/y H:i',
defaultDate: dateTo,
onChange: ([selectedDate]) => {
this.updateElement({ dateTo: selectedDate });
}
}
);
}
}

0 comments on commit 4cf266b

Please sign in to comment.