Skip to content

Commit

Permalink
7.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jfhdgkjfh committed May 25, 2024
1 parent b4cdb91 commit 4b5cda7
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 15 deletions.
5 changes: 2 additions & 3 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 @@ -34,6 +34,7 @@
"css-loader": "^7.0.0",
"dayjs": "1.11.7",
"flatpickr": "^4.6.13",
"he": "^1.2.0",
"nanoid": "^5.0.7",
"style-loader": "^3.3.4"
}
Expand Down
12 changes: 0 additions & 12 deletions src/mock/filter.js

This file was deleted.

Empty file added src/model/filter-model.js
Empty file.
Empty file.
Empty file.
128 changes: 128 additions & 0 deletions src/presenter/task-presenter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import {render, replace, remove} from '../framework/render.js';
import TaskView from '../view/task-view.js';
import TaskEditView from '../view/task-edit-view.js';
import {UserAction, UpdateType} from '../const.js';
import {isTaskRepeating, isDatesEqual} from '../utils/task.js';

const Mode = {
DEFAULT: 'DEFAULT',
EDITING: 'EDITING',
};
export default class TaskPresenter {
#taskListContainer = null;
#handleDataChange = null;
#handleModeChange = null;
#taskComponent = null;
#taskEditComponent = null;
#task = null;
#mode = Mode.DEFAULT;
constructor({taskListContainer, onDataChange, onModeChange}) {
this.#taskListContainer = taskListContainer;
this.#handleDataChange = onDataChange;
this.#handleModeChange = onModeChange;
}

init(task) {
this.#task = task;
const prevTaskComponent = this.#taskComponent;
const prevTaskEditComponent = this.#taskEditComponent;
this.#taskComponent = new TaskView({
task: this.#task,
onEditClick: this.#handleEditClick,
onFavoriteClick: this.#handleFavoriteClick,
onArchiveClick: this.#handleArchiveClick
});
this.#taskEditComponent = new TaskEditView({
task: this.#task,
onFormSubmit: this.#handleFormSubmit,
onDeleteClick: this.#handleDeleteClick
});

if (prevTaskComponent === null || prevTaskEditComponent === null) {
render(this.#taskComponent, this.#taskListContainer);
return;
}
if (this.#mode === Mode.DEFAULT) {
replace(this.#taskComponent, prevTaskComponent);
}
if (this.#mode === Mode.EDITING) {
replace(this.#taskEditComponent, prevTaskEditComponent);
}
remove(prevTaskComponent);
remove(prevTaskEditComponent);
}

destroy() {
remove(this.#taskComponent);
remove(this.#taskEditComponent);
}

resetView() {
if (this.#mode !== Mode.DEFAULT) {
this.#taskEditComponent.reset(this.#task);
this.#replaceFormToCard();
}
}

#replaceCardToForm() {
replace(this.#taskEditComponent, this.#taskComponent);
document.addEventListener('keydown', this.#escKeyDownHandler);
this.#handleModeChange();
this.#mode = Mode.EDITING;
}

#replaceFormToCard() {
replace(this.#taskComponent, this.#taskEditComponent);
document.removeEventListener('keydown', this.#escKeyDownHandler);
this.#mode = Mode.DEFAULT;
}

#escKeyDownHandler = (evt) => {
if (evt.key === 'Escape') {
evt.preventDefault();
this.#taskEditComponent.reset(this.#task);
this.#replaceFormToCard();
}
};

#handleEditClick = () => {
this.#replaceCardToForm();
};

#handleFavoriteClick = () => {
this.#handleDataChange(
UserAction.UPDATE_TASK,
UpdateType.MINOR,
{...this.#task, isFavorite: !this.#task.isFavorite},
);
};

#handleArchiveClick = () => {
this.#handleDataChange(
UserAction.UPDATE_TASK,
UpdateType.MINOR,
{...this.#task, isArchive: !this.#task.isArchive},
);
};

#handleFormSubmit = (update) => {
const isMinorUpdate =
!isDatesEqual(this.#task.dueDate, update.dueDate) ||
isTaskRepeating(this.#task.repeating) !== isTaskRepeating(update.repeating);

this.#handleDataChange(
UserAction.UPDATE_TASK,
isMinorUpdate ? UpdateType.MINOR : UpdateType.PATCH,
update,
);
this.#replaceFormToCard();
};

#handleDeleteClick = (task) => {
this.#handleDataChange(
UserAction.DELETE_TASK,
UpdateType.MINOR,
task,
);
};
}

0 comments on commit 4b5cda7

Please sign in to comment.