Skip to content

Commit

Permalink
Merge pull request #23 from lunianka/module8-task2
Browse files Browse the repository at this point in the history
  • Loading branch information
keksobot authored May 28, 2024
2 parents f5fb2c0 + b94c1d5 commit 56763c6
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 57 deletions.
10 changes: 9 additions & 1 deletion src/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ const EditType = {
const Method = {
GET: 'GET',
PUT: 'PUT',
POST: 'POST',
DELETE: 'DELETE'
};

const TimeLimit = {
LOWER_LIMIT: 350,
UPPER_LIMIT: 1000,
};

export {
Expand All @@ -130,5 +137,6 @@ export {
UserAction,
UpdateType,
EditType,
Method
Method,
TimeLimit
}

Check failure on line 142 in src/const.js

View workflow job for this annotation

GitHub Actions / Check

Missing semicolon
26 changes: 17 additions & 9 deletions src/model/points-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export default class PointsModel extends Observable {

async update(updateType, point) {
try {
console.log('ф', this.#service.updatePoint(adaptToServer(point)))
const updatedPoint = await this.#service.updatePoint(adaptToServer(point));
const adaptedPoint = adaptToClient(updatedPoint);
this.#points = updateItem(this.#points, adaptedPoint);
Expand All @@ -52,15 +51,24 @@ export default class PointsModel extends Observable {
}
}

add(updateType, point) {
const addedPoint = this.#service.addPoint(point);
this.#points.push(addedPoint);
this._notify(updateType, addedPoint);
async add(updateType, point) {
try {
const addedPoint = await this.#service.addPoint(adaptToServer(point));
const adaptedPoint = adaptToClient(addedPoint);
this.#points.push(adaptedPoint);
this._notify(updateType, adaptedPoint);
} catch {
throw new Error('Can\'t add point');
}
}

delete(updateType, point) {
this.#service.deletePoint(point);
this.#points = this.#points.filter((pointItem) => pointItem.id !== point.id);
this._notify(updateType);
async delete(updateType, point) {
try {
await this.#service.deletePoint(point);
this.#points = this.#points.filter((pointItem) => pointItem.id !== point.id);
this._notify(updateType);
} catch {
throw new Error('Can\'t delete point');
}
}
}
21 changes: 19 additions & 2 deletions src/presenter/new-point-presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,31 @@ export default class NewPointPresenter {
this.#handleDestroy({ isCanceled });
};

setSaving = () => {
this.#pointNewComponent.updateElement({
isDisabled: true,
isSaving: true,
});
}

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

this.#pointNewComponent.shake(resetFormState);
}

#formSubmitHandler = (point) => {
this.#handleDataChange(
UserAction.ADD_POINT,
UpdateType.MINOR,
point
);

this.destroy({ isCanceled: false });
};

#resetButtonClickHandler = () => {
Expand Down
37 changes: 36 additions & 1 deletion src/presenter/point-presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,42 @@ export default class PointPresenter {
destroy = () => {
remove(this.#pointComponent);
remove(this.#editPointComponent);
}
};

setSaving = () => {
if (this.#mode === Mode.EDITING) {
this.#editPointComponent.updateElement({
isDisabled: true,
isSaving: true
});
}
};

setDeleting = () => {
this.#editPointComponent.updateElement({
isDisabled: true,
isDeleting: true
});
};

setAborting = () => {
if (this.#mode === Mode.DEFAULT) {
this.#pointComponent.shake();
return;
}

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

this.#editPointComponent.shake(resetFormState);
}
};

#replacePointToForm = () => {
replace(this.#editPointComponent, this.#pointComponent);
Expand Down
39 changes: 32 additions & 7 deletions src/presenter/trip-presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import LoadingView from '../view/loading-view.js';
import PointPresenter from './point-presenter.js';
import NewPointPresenter from './new-point-presenter.js';
import { render, replace, remove, RenderPosition } from '../framework/render.js';
import { SortType, EnabledSortType, UserAction, UpdateType, FilterType } from '../const.js';
import { SortType, EnabledSortType, UserAction, UpdateType, FilterType, TimeLimit } from '../const.js';
import { sort } from '../utils/sort.js';
import { filter } from '../utils/filter.js';
import UiBlocker from '../framework/ui-blocker/ui-blocker.js';

export default class TripPresenter {
#tripContainer = null;
Expand All @@ -16,6 +17,10 @@ export default class TripPresenter {
#sortComponent = null;
#messageComponent = null;
#loadingComponent = new LoadingView();
#uiBlocker = new UiBlocker({
lowerLimit: TimeLimit.LOWER_LIMIT,
upperLimit: TimeLimit.UPPER_LIMIT
});

#destinationsModel = null;
#offersModel = null;
Expand Down Expand Up @@ -43,7 +48,7 @@ export default class TripPresenter {
container: this.#pointListComponent.element,
destinationsModel: this.#destinationsModel,
offersModel: this.#offersModel,
onDataChange: this.#pointChangeHandler,
onDataChange: this.#viewActionHandler,
onDestroy: this.#newPointDestroyHandler
});

Expand Down Expand Up @@ -75,7 +80,7 @@ export default class TripPresenter {
container: this.#pointListComponent.element,
destinationsModel: this.#destinationsModel,
offersModel: this.#offersModel,
onDataChange: this.#pointChangeHandler,
onDataChange: this.#viewActionHandler,
onModeChange: this.#modeChangeHandler
});

Expand Down Expand Up @@ -138,9 +143,12 @@ export default class TripPresenter {
#renderBoard = () => {
if (this.#isLoading) {
this.#renderLoading();
this.#newPointButtonPresenter.disableButton();
return;
}

this.#newPointButtonPresenter.enableButton();

if (this.#isLoadingError) {
this.#clearBoard({ resetSortType: true });
remove(this.#sortComponent);
Expand Down Expand Up @@ -170,18 +178,35 @@ export default class TripPresenter {
}
}

#pointChangeHandler = (actionType, updateType, update) => {
#viewActionHandler = async (actionType, updateType, update) => {
this.#uiBlocker.block();
switch (actionType) {
case UserAction.UPDATE_POINT:
this.#pointsModel.update(updateType, update);
this.#pointPresenters.get(update.id).setSaving();
try {
await this.#pointsModel.update(updateType, update);
} catch {
this.#pointPresenters.get(update.id).setAborting();
}
break;
case UserAction.DELETE_POINT:
this.#pointsModel.delete(updateType, update);
this.#pointPresenters.get(update.id).setDeleting();
try {
await this.#pointsModel.delete(updateType, update);
} catch {
this.#pointPresenters.get(update.id).setAborting();
}
break;
case UserAction.ADD_POINT:
this.#pointsModel.add(updateType, update);
this.#newPointPresenter.setSaving();
try {
await this.#pointsModel.add(updateType, update);
} catch {
this.#newPointPresenter.setAborting();
}
break;
}
this.#uiBlocker.unblock();
};

#modelEventHandler = (updateType, data) => {
Expand Down
20 changes: 16 additions & 4 deletions src/service/points-api-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,23 @@ export default class PointsApiService extends ApiService {
return parsedResponse;
}

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

const parsedResponse = await ApiService.parseResponse(response);

return parsedResponse;
}

deletePoint() {
//...
async deletePoint(point) {
await this._load({
url: `points/${point.id}`,
method: Method.DELETE
});
}
}
2 changes: 1 addition & 1 deletion src/utils/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function adaptToClient(point) {
function adaptToServer(point) {
const adaptedPoint = {
...point,
['base_price']: point.basePrice,
['base_price']: Number(point.basePrice),
['date_from']: new Date(point.dateFrom).toISOString(),
['date_to']: new Date(point.dateTo).toISOString(),
['is_favorite']: point.isFavorite
Expand Down
Loading

0 comments on commit 56763c6

Please sign in to comment.