Skip to content

Commit

Permalink
Merge pull request #17 from jfhdgkjfh/module6-task2
Browse files Browse the repository at this point in the history
  • Loading branch information
keksobot authored May 25, 2024
2 parents fa17886 + 7e4ba12 commit b4cdb91
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
11 changes: 11 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": {
"css-loader": "^7.0.0",
"dayjs": "1.11.7",
"flatpickr": "^4.6.13",
"nanoid": "^5.0.7",
"style-loader": "^3.3.4"
}
Expand Down
35 changes: 34 additions & 1 deletion src/view/task-edit-view.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import AbstractStatefulView from '../framework/view/abstract-stateful-view.js';
import {COLORS} from '../const.js';
import {humanizeTaskDueDate, isTaskRepeating} from '../utils/task.js';
import flatpickr from 'flatpickr';

import 'flatpickr/dist/flatpickr.min.css';

const BLANK_TASK = {
color: COLORS[0],
Expand Down Expand Up @@ -90,7 +93,7 @@ function createTaskEditTemplate(data) {
const repeatingTemplate = createTaskEditRepeatingTemplate(repeating, isRepeating);

const colorsTemplate = createTaskEditColorsTemplate(color);
const isSubmitDisabled = isRepeating && !isTaskRepeating(repeating);
const isSubmitDisabled = (isDueDate && dueDate === null) || (isRepeating && !isTaskRepeating(repeating));


return (
Expand Down Expand Up @@ -138,6 +141,7 @@ function createTaskEditTemplate(data) {

export default class TaskEditView extends AbstractStatefulView {
#handleFormSubmit = null;
#datepicker = null;

constructor({task = BLANK_TASK, onFormSubmit}) {
super();
Expand All @@ -151,6 +155,15 @@ export default class TaskEditView extends AbstractStatefulView {
return createTaskEditTemplate(this._state);
}

removeElement() {
super.removeElement();

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

reset(task) {
this.updateElement(
TaskEditView.parseTaskToState(task),
Expand All @@ -173,6 +186,8 @@ export default class TaskEditView extends AbstractStatefulView {
this.element.querySelector('.card__repeat-days-inner')
.addEventListener('change', this.#repeatingChangeHandler);
}

this.#setDatepicker();
}

#colorChangeHandler = (evt) => {
Expand All @@ -189,6 +204,12 @@ export default class TaskEditView extends AbstractStatefulView {
});
};

#dueDateChangeHandler = ([userDate]) => {
this.updateElement({
dueDate: userDate,
});
};

#dueDateToggleHandler = (evt) => {
evt.preventDefault();
this.updateElement({
Expand Down Expand Up @@ -217,6 +238,18 @@ export default class TaskEditView extends AbstractStatefulView {
});
};

#setDatepicker() {
if (this._state.isDueDate) {
this.#datepicker = flatpickr(
this.element.querySelector('.card__date'),
{
dateFormat: 'j F',
defaultDate: this._state.dueDate,
onChange: this.#dueDateChangeHandler,
},
);
}
}

static parseTaskToState(task) {
return {...task,
Expand Down

0 comments on commit b4cdb91

Please sign in to comment.