Skip to content

Commit

Permalink
Защита
Browse files Browse the repository at this point in the history
  • Loading branch information
egorarud committed Jun 9, 2024
1 parent dc8d636 commit 1b0ab5a
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 225 deletions.
24 changes: 4 additions & 20 deletions src/const.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
const POINT_TYPE = ['taxi', 'bus', 'train', 'ship', 'drive', 'flight', 'check-in', 'sightseeing', 'restaurant'];
const CITIES = ['Amsterdam', 'Geneva', 'Chamonix', 'Moscow', 'Paris'];
const DESCRIPTIONS = [
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
'Cras aliquet varius magna, non porta ligula feugiat eget.',
'Fusce tristique felis at fermentum pharetra.',
'Aliquam id orci ut lectus varius viverra.',
'Nullam nunc ex, convallis sed finibus eget, sollicitudin eget ante.',
'Phasellus eros mauris, condimentum sed nibh vitae, sodales efficitur ipsum.'
];
const POINT_TYPES = ['taxi', 'bus', 'train', 'ship', 'drive', 'flight', 'check-in', 'sightseeing', 'restaurant'];

const FilterType = {
EVERYTHING: 'everything',
Expand All @@ -16,7 +7,7 @@ const FilterType = {
FUTUTRE: 'future'
};

const LIST_EMPTY_TEXT = {
const ListEmptyText = {
[FilterType.EVERYTHING] : 'Click New Event to create your first point',
[FilterType.PAST] : 'There are no past events now',
[FilterType.PRESENT] : 'There are no present events now',
Expand Down Expand Up @@ -44,16 +35,9 @@ const SortType = {
OFFERS: 'offers'
};

const MIN_PRICE = 100;
const MAX_PRICE = 5000;

export {
POINT_TYPE,
CITIES,
DESCRIPTIONS,
MAX_PRICE,
MIN_PRICE,
LIST_EMPTY_TEXT,
POINT_TYPES,
ListEmptyText,
UserAction,
UpdateType,
FilterType,
Expand Down
136 changes: 68 additions & 68 deletions src/presenter/trip-events-presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,90 +83,32 @@ export default class TripEventsPresenter {
this.#newPointPresenter.init();
}

#handleViewAction = async (actionType, updateType, update) => {
switch (actionType) {
case UserAction.UPDATE_POINT:
this.#pointPresenters.get(update.id).setSaving();
try {
await this.#pointsModel.updatePoint(updateType, update);
} catch(err) {
this.#pointPresenters.get(update.id).setAborting();
}
break;

case UserAction.ADD_POINT:
this.#newPointPresenter.setSaving();
try {
await this.#pointsModel.addPoint(updateType, update);
} catch(err) {
this.#pointPresenters.get(update.id).setAborting();
}
break;

case UserAction.DELETE_POINT:
this.#pointPresenters.get(update.id).setDeleting();
try {
await this.#pointsModel.deletePoint(updateType, update);
} catch(err) {
this.#pointPresenters.get(update.id).setAborting();
}
break;
}
};

#handleModelEvent = (updateType, data) => {
switch (updateType) {
case UpdateType.PATCH:
this.#pointPresenters.get(data.id).init(data);
break;
case UpdateType.MINOR:
this.#clearBoard();
this.#renderBoard();
break;
case UpdateType.MAJOR:
this.#clearBoard(true);
this.#renderBoard();
break;
case UpdateType.INIT:
this.#isLoading = false;
remove(this.#loadingComponent);
this.#clearBoard(true);
this.#renderBoard();
break;
}
};

#changeSortType = (type) => {
this.#currentSortType = type;
this.#clearBoard();
this.#renderBoard();
};

#changeViewHandler = () => {
this.#newPointPresenter.destroy();
this.#pointPresenters.forEach((presenter) => presenter.resetView());
};

#renderSort = () => {
#renderSort() {
this.#sortComponent = new SortView(this.#changeSortType, this.#currentSortType);
render(this.#sortComponent, this.#tpipEventsContainer);
};
}

#renderList = () => {
#renderList() {
render(this.#listComponent, this.#tpipEventsContainer);
};
}

#renderLoading() {
render(this.#loadingComponent, this.#tpipEventsContainer);
}

#renderListEmpty = () => {
#renderListEmpty() {
this.#listEmptyComponent = new ListEmptyView(this.#filterType);
render(this.#listEmptyComponent, this.#tpipEventsContainer);
remove(this.#loadingComponent);
};
}

#renderPoints = (points) => {
#renderPoints(points) {
this.#clearListEmptyText();

if (points.length === 0) {
Expand All @@ -186,12 +128,12 @@ export default class TripEventsPresenter {
pointPresenter.init(point);
this.#pointPresenters.set(point.id, pointPresenter);
}
};
}

#renderTripInfo = () => {
#renderTripInfo() {
this.#tripInfoComponent = new TripInfoView(this.#pointsModel, this.#destinationsModel, this.#offersModel);
render(this.#tripInfoComponent, this.#mainContainer, RenderPosition.AFTERBEGIN);
};
}

#renderBoard() {
if (this.#isLoading) {
Expand Down Expand Up @@ -224,4 +166,62 @@ export default class TripEventsPresenter {
remove(this.#listEmptyComponent);
}
}

#changeViewHandler = () => {
this.#newPointPresenter.destroy();
this.#pointPresenters.forEach((presenter) => presenter.resetView());
};

#handleViewAction = async (actionType, updateType, update) => {
switch (actionType) {
case UserAction.UPDATE_POINT:
this.#pointPresenters.get(update.id).setSaving();
try {
await this.#pointsModel.updatePoint(updateType, update);
} catch(err) {
this.#pointPresenters.get(update.id).setAborting();
}
break;

case UserAction.ADD_POINT:
this.#newPointPresenter.setSaving();
try {
await this.#pointsModel.addPoint(updateType, update);
} catch(err) {
this.#pointPresenters.get(update.id).setAborting();
}
break;

case UserAction.DELETE_POINT:
this.#pointPresenters.get(update.id).setDeleting();
try {
await this.#pointsModel.deletePoint(updateType, update);
} catch(err) {
this.#pointPresenters.get(update.id).setAborting();
}
break;
}
};

#handleModelEvent = (updateType, data) => {
switch (updateType) {
case UpdateType.PATCH:
this.#pointPresenters.get(data.id).init(data);
break;
case UpdateType.MINOR:
this.#clearBoard();
this.#renderBoard();
break;
case UpdateType.MAJOR:
this.#clearBoard(true);
this.#renderBoard();
break;
case UpdateType.INIT:
this.#isLoading = false;
remove(this.#loadingComponent);
this.#clearBoard(true);
this.#renderBoard();
break;
}
};
}
46 changes: 23 additions & 23 deletions src/presenter/trip-point-presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,36 +70,17 @@ export default class TripPointPresenter {
}
}

#favoriteButtonClickHandler = () => {
this.#onDataChange(
UserAction.UPDATE_POINT,
UpdateType.MINOR,
{...this.#point, isFavorite: !this.#point.isFavorite}
);
};

#replacePointToForm() {
this.#onViewChange();
replace(this.#editPointComponent, this.#pointComponent);
this.#view = VIEW.EDIT;
}

#replaceFormToPoint() {
replace(this.#pointComponent, this.#editPointComponent);
this.#view = VIEW.DEFAULT;
}

resetView = () => {
resetView() {
if (this.#view === VIEW.EDIT){
this.#editPointComponent.reset(this.#point, this.#destination, this.#offer);
this.#replaceFormToPoint();
}
};
}

remove = () => {
remove() {
remove(this.#pointComponent);
remove(this.#editPointComponent);
};
}

setSaving() {
if (this.#view === VIEW.EDIT) {
Expand Down Expand Up @@ -136,6 +117,17 @@ export default class TripPointPresenter {
this.#editPointComponent.shake(resetFormState);
}

#replacePointToForm() {
this.#onViewChange();
replace(this.#editPointComponent, this.#pointComponent);
this.#view = VIEW.EDIT;
}

#replaceFormToPoint() {
replace(this.#pointComponent, this.#editPointComponent);
this.#view = VIEW.DEFAULT;
}

#pointRollupButtonClikHandler = () => {
this.#replacePointToForm();
document.addEventListener('keydown', this.#onFormKeydown);
Expand Down Expand Up @@ -173,4 +165,12 @@ export default class TripPointPresenter {
this.#replaceFormToPoint();
}
};

#favoriteButtonClickHandler = () => {
this.#onDataChange(
UserAction.UPDATE_POINT,
UpdateType.MINOR,
{...this.#point, isFavorite: !this.#point.isFavorite}
);
};
}
19 changes: 0 additions & 19 deletions src/render.js

This file was deleted.

36 changes: 9 additions & 27 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,17 @@ function formatEventDate(dueDate, dateFormat) {
}

function formatEventDuration(diff) {
const hours = Math.floor(diff / 60);
const days = Math.floor(diff / 1440);
const hours = Math.floor((diff % 1440) / 60);
const minutes = diff % 60;

return hours === 0 ? `${minutes}M` : `${hours}H ${minutes}M`;
}

function createId() {
let lastGeneratedId = 0;
return function () {
lastGeneratedId += 1;
return lastGeneratedId;
};
}

function updateItem(items, update) {
return items.map((item) => item.id === update.id ? update : item);
}

function getRandomInteger(a, b) {
const lower = Math.ceil(Math.min(a, b));
const upper = Math.floor(Math.max(a, b));
const result = Math.random() * (upper - lower + 1) + lower;
return Math.floor(result);
if (days > 0) {
return `${days}D ${hours}H ${minutes}M`;
} else if (hours > 0) {
return `${hours}H ${minutes}M`;
} else {
return `${minutes}M`;
}
}

function calculateDateDifference(start, end) {
Expand All @@ -40,8 +28,6 @@ function calculateDateDifference(start, end) {

const isElementHas = (element) => element !== null && element !== undefined && element.length > 0;

const getRandomArrayElement = (arr) => arr[getRandomInteger(0, arr.length - 1)];

const isEscapeKey = (evt) => evt.key === 'Escape';

const filter = {
Expand All @@ -52,14 +38,10 @@ const filter = {
};

export {
createId,
getRandomInteger,
getRandomArrayElement,
formatEventDate,
isElementHas,
isEscapeKey,
calculateDateDifference,
formatEventDuration,
updateItem,
filter
};
Loading

0 comments on commit 1b0ab5a

Please sign in to comment.