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) #15

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
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
38 changes: 31 additions & 7 deletions src/api/points-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import ApiService from '../framework/api-service.js';
const Method = {
GET: 'GET',
PUT: 'PUT',
POST: 'POST',
DELETE: 'DELETE',
};

export default class PointsApi extends ApiService {
Expand All @@ -24,19 +26,41 @@ export default class PointsApi extends ApiService {
return parsedResponse;
}

async addPoint(point) {
const response = await this._load({
url: 'points',
method: Method.POST,
body: JSON.stringify(this.#adaptToServer(point)),
headers: new Headers({'Content-Type': 'application/json'}),
});

const parsedResponse = await ApiService.parseResponse(response);

return parsedResponse;
}

async deletePoint(point) {
const response = await this._load({
url: `points/${point.id}`,
method: Method.DELETE,
});

return response;
}

#adaptToServer(point) {
const adaptedTask = {...point,
const adaptedPoint = {...point,
'base_price': point.basePrice,
'date_from': point.dateFrom.toISOString(),
'date_to': point.dateTo.toISOString(),
'is-favorite': point.isFavorite,
'is_favorite': point.isFavorite,
};

delete adaptedTask.basePrice;
delete adaptedTask.dateFrom;
delete adaptedTask.dateTo;
delete adaptedTask.isFavorite;
delete adaptedPoint.basePrice;
delete adaptedPoint.dateFrom;
delete adaptedPoint.dateTo;
delete adaptedPoint.isFavorite;

return adaptedTask;
return adaptedPoint;
}
}
48 changes: 27 additions & 21 deletions src/model/points-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,47 +41,53 @@ export default class PointsModel extends Observable {
];
this._notify(updateType, updatedPoint);
} catch(err) {
throw new Error('Can\'t update task');
throw new Error('Can\'t update point');
}
}

addPoint(updateType, update) {
this.#points = [
update,
...this.#points,
];

this._notify(updateType, update);
async addPoint(updateType, update) {
try {
const response = await this.#pointsApi.addPoint(update);
const newPoint = this.#adaptToClient(response);
this.#points = [newPoint, ...this.#points];
this._notify(updateType, newPoint);
} catch(err) {
throw new Error('Can\'t add point');
}
}

deletePoint(updateType, update) {
async deletePoint(updateType, update) {
const index = this.#points.findIndex((point) => point.id === update.id);

if (index === -1) {
throw new Error('Can\'t delete unexisting point');
}

this.#points = [
...this.#points.slice(0, index),
...this.#points.slice(index + 1),
];

this._notify(updateType);
try {
await this.#pointsApi.deletePoint(update);
this.#points = [
...this.#points.slice(0, index),
...this.#points.slice(index + 1),
];
this._notify(updateType);
} catch(err) {
throw new Error('Can\'t delete point');
}
}

#adaptToClient(point) {
const adaptedTask = {...point,
const adaptedPoint = {...point,
basePrice: point['base_price'],
dateFrom: new Date(point['date_from']),
dateTo: new Date(point['date_to']),
isFavorite: point['is-favorite']
};

delete adaptedTask['base_price'];
delete adaptedTask['date_from'];
delete adaptedTask['date_to'];
delete adaptedTask['is-favorite'];
delete adaptedPoint['base_price'];
delete adaptedPoint['date_from'];
delete adaptedPoint['date_to'];
delete adaptedPoint['is-favorite'];

return adaptedTask;
return adaptedPoint;
}
}
25 changes: 20 additions & 5 deletions src/presenter/new-point-presenter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {remove, render, RenderPosition} from '../framework/render.js';
import EditablePointView from '../view/editable-point-view.js';
import {nanoid} from 'nanoid';
import {UserAction, UpdateType} from '../const.js';
import { isEscapeKey } from '../util.js';

Expand Down Expand Up @@ -53,15 +52,31 @@ export default class NewPointPresenter {
document.removeEventListener('keydown', this.#escKeyDownHandler);
}

setSaving() {
this.#pointEditComponent.updateElement({
isDisabled: true,
isSaving: true,
});
}

setAborting() {
const resetFormState = () => {
this.#pointEditComponent.updateElement({
isDisabled: false,
isSaving: false,
isDeleting: false,
});
};

this.#pointEditComponent.shake(resetFormState);
}

#handleFormSubmit = (point) => {
this.#handleDataChange(
UserAction.ADD_POINT,
UpdateType.MINOR,
// Пока у нас нет сервера, который бы после сохранения
// выдывал честный id задачи, нам нужно позаботиться об этом самим
{id: nanoid(), ...point},
point,
);
this.destroy();
};

#handleDeleteClick = () => {
Expand Down
39 changes: 28 additions & 11 deletions src/presenter/trip-events-presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ export default class TripEventsPresenter {
#currentSortType = SortType.PRICE;

#pointPresenters = new Map();
#newTaskPresenter = null;
#newPointPresenter = null;

#filterType = null;
#isLoading = true;

constructor({tpipEventsContainer, pointsModel, offersModel, destinationsModel, filterModel, onNewTaskDestroy}) {
constructor({tpipEventsContainer, pointsModel, offersModel, destinationsModel, filterModel, onNewPointDestroy}) {
this.#tpipEventsContainer = tpipEventsContainer;
this.#pointsModel = pointsModel;
this.#offersModel = offersModel;
this.#destinationsModel = destinationsModel;
this.#filterModel = filterModel;

this.#newTaskPresenter = new NewPointPresenter({
this.#newPointPresenter = new NewPointPresenter({
pointListContainer: this.#listComponent.element,
onDataChange: this.#handleViewAction,
onDestroy: onNewTaskDestroy,
onDestroy: onNewPointDestroy,
offersModel: this.#offersModel,
destinationsModel: this.#destinationsModel
});
Expand Down Expand Up @@ -75,19 +75,36 @@ export default class TripEventsPresenter {
createPoint() {
this.#currentSortType = SortType.DAY;
this.#filterModel.setFilter(UpdateType.MAJOR, FilterType.EVERYTHING);
this.#newTaskPresenter.init();
this.#newPointPresenter.init();
}

#handleViewAction = (actionType, updateType, update) => {
#handleViewAction = async (actionType, updateType, update) => {
switch (actionType) {
case UserAction.UPDATE_POINT:
this.#pointsModel.updatePoint(updateType, update);
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.#pointsModel.addPoint(updateType, update);
this.#newPointPresenter.setSaving();
try {
await this.#pointsModel.addPoint(updateType, update);
} catch(err) {
this.#pointPresenters.get(update.id).setAborting();
}
break;

case UserAction.DELETE_POINT:
this.#pointsModel.deletePoint(updateType, update);
this.#pointPresenters.get(update.id).setDeleting();
try {
await this.#pointsModel.deletePoint(updateType, update);
} catch(err) {
this.#pointPresenters.get(update.id).setAborting();
}
break;
}
};
Expand Down Expand Up @@ -121,7 +138,7 @@ export default class TripEventsPresenter {
};

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

Expand Down Expand Up @@ -178,7 +195,7 @@ export default class TripEventsPresenter {
}

#clearBoard(resetSortType = false) {
this.#newTaskPresenter.destroy();
this.#newPointPresenter.destroy();
this.#pointPresenters.forEach((presenter) => presenter.remove());
this.#pointPresenters.clear();

Expand Down
36 changes: 35 additions & 1 deletion src/presenter/trip-point-presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,41 @@ export default class TripPointPresenter {
remove(this.#editPointComponent);
};

setSaving() {
if (this.#view === VIEW.EDIT) {
this.#editPointComponent.updateElement({
isDisabled: true,
isSaving: true,
});
}
}

setDeleting() {
if (this.#view === VIEW.EDIT) {
this.#editPointComponent.updateElement({
isDisabled: true,
isDeleting: true,
});
}
}

setAborting() {
if (this.#view === VIEW.DEFAULT) {
this.#pointComponent.shake();
return;
}

const resetFormState = () => {
this.#editPointComponent.updateElement({
isDisabled: false,
isSaving: false,
isDeleting: false,
});
};

this.#editPointComponent.shake(resetFormState);
}

#pointRollupButtonClikHandler = () => {
this.#replacePointToForm();
document.addEventListener('keydown', this.#onFormKeydown);
Expand All @@ -118,7 +153,6 @@ export default class TripPointPresenter {
UpdateType.MINOR,
point
);
this.#replaceFormToPoint();
document.removeEventListener('keydown', this.#onFormKeydown);
};

Expand Down
Loading
Loading