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

Merged
merged 3 commits into from
Jun 16, 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
31 changes: 17 additions & 14 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
import Presenter from './presenter/presenter.js';
import PresenterMain from './presenter/presenter-main.js';
import PointsModel from './model/points-model.js';
import FilterModel from './model/filter-model.js';
import FilterPresenter from './presenter/presenter-filter.js';
import PointsApiService from './points-api-service.js';
import PresenterInfoPanel from './presenter/presenter-info-panel.js';
import CreationForm from './view/creation-form.js';
import { render } from './framework/render.js';

const filterContainer = document.querySelector('.trip-controls__filters');
const tripMainElement = document.querySelector('.trip-main');


const AUTHORIZATION = 'Basic hf7898sdfscv83';
const AUTHORIZATION = 'Basic hf7898sdfscv89';
const END_POINT = 'https://23.objects.htmlacademy.pro/big-trip';


const pointsModel = new PointsModel({
pointsApiService: new PointsApiService(END_POINT, AUTHORIZATION)
pointsApiService: new PointsApiService(END_POINT, AUTHORIZATION),
});
const filterModel = new FilterModel();

const presenter = new Presenter({
const presenter = new PresenterMain({
pointsModel,
filterModel,
onNewPointDestroy: handleNewPointButtonClose
onNewPointDestroy: handleNewPointButtonClose,
});

const filterPresenter = new FilterPresenter({
filterContainer: filterContainer,
filterModel,
pointsModel
pointsModel,
});

const newPointButtonComponent = new CreationForm({
onClick: handleNewPointButtonClick
onClick: handleNewPointButtonClick,
});

const presenterInfoPanel = new PresenterInfoPanel({
tripInfoContainer: tripMainElement,
pointsModel,
});

function handleNewPointButtonClick() {
Expand All @@ -48,10 +52,9 @@ render(newPointButtonComponent, tripMainElement);

newPointButtonComponent.element.disabled = true;


presenter.init();
filterPresenter.init();
pointsModel.init()
.finally(() => {
newPointButtonComponent.element.disabled = false;
});
pointsModel.init().finally(() => {
newPointButtonComponent.element.disabled = false;
});
presenterInfoPanel.init();
2 changes: 1 addition & 1 deletion src/model/filter-model.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FilterType } from '../utils.js/filter.js';
import { FilterType } from '../utils/filter.js';
import Observable from '../framework/observable.js';

export default class FilterModel extends Observable {
Expand Down
29 changes: 10 additions & 19 deletions src/model/points-model.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// import { MOCKED_EVENTS } from '../mock/trip-event.js';
import Observable from '../framework/observable.js';
import { UpdateType } from '../utils.js/const.js';
import { UpdateType } from '../utils/const.js';

export default class PointsModel extends Observable {
#pointsApiService = null;
Expand All @@ -13,7 +13,7 @@ export default class PointsModel extends Observable {
this.#pointsApiService = pointsApiService;
}

get event() {
get events() {
return this.#points;
}

Expand All @@ -40,7 +40,6 @@ export default class PointsModel extends Observable {
this.#destinations = [];
}


this._notify(UpdateType.INIT);
}

Expand All @@ -54,11 +53,7 @@ export default class PointsModel extends Observable {
const response = await this.#pointsApiService.updatePoint(update);
const updatedPoint = this.#adaptToClient(response);

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

this._notify(updateType, updatedPoint);
} catch (err) {
Expand All @@ -71,16 +66,12 @@ export default class PointsModel extends Observable {
const response = await this.#pointsApiService.addPoint(update);
const newPoint = this.#adaptToClient(response);

this.#points = [
newPoint,
...this.#points
];
this.#points = [newPoint, ...this.#points];

this._notify(updateType, newPoint);
} catch (err) {
throw new Error('Can\'t add point');
}

}

async deletePoint(updateType, update) {
Expand All @@ -91,10 +82,7 @@ export default class PointsModel extends Observable {

try {
await this.#pointsApiService.deletePoint(update);
this.#points = [
...this.#points.slice(0, index),
...this.#points.slice(index + 1)
];
this.#points = [...this.#points.slice(0, index), ...this.#points.slice(index + 1)];

this._notify(updateType);
} catch (err) {
Expand All @@ -108,7 +96,7 @@ export default class PointsModel extends Observable {
basePrice: point['base_price'],
dateFrom: new Date(point['date_from']),
dateTo: new Date(point['date_to']),
isFavorite: point['is_favorite']
isFavorite: point['is_favorite'],
};

delete adaptedPoint['base_price'];
Expand All @@ -128,5 +116,8 @@ export default class PointsModel extends Observable {
getDestinationId(id) {
return this.destinations.find((item) => item.id === id);
}
}

getDestinationName(name) {
return this.#destinations.find((item) => item.name === name);
}
}
21 changes: 10 additions & 11 deletions src/presenter/presenter-filter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { FilterType } from '../utils.js/filter.js';
import { FilterType } from '../utils/filter.js';
import { remove, render, replace } from '../framework/render.js';
import { filter } from '../utils.js/filter.js';
import { filter } from '../utils/filter.js';
import FilterView from '../view/filter-view.js';
import { UpdateType } from '../utils.js/const.js';

import { UpdateType } from '../utils/const.js';

export default class FilterPresenter {
#filterContainer = null;
Expand All @@ -21,25 +20,25 @@ export default class FilterPresenter {
}

get filters() {
const points = this.#pointsModel.event;
const points = this.#pointsModel.events;

return [
{
name: FilterType.EVERYTHING,
count: filter[FilterType.EVERYTHING](points).length
count: filter[FilterType.EVERYTHING](points).length,
},
{
name: FilterType.PAST,
count: filter[FilterType.PAST](points).length
count: filter[FilterType.PAST](points).length,
},
{
name: FilterType.PRESENT,
count: filter[FilterType.PRESENT](points).length
count: filter[FilterType.PRESENT](points).length,
},
{
name: FilterType.FUTURE,
count: filter[FilterType.FUTURE](points).length
}
count: filter[FilterType.FUTURE](points).length,
},
];
}

Expand All @@ -50,7 +49,7 @@ export default class FilterPresenter {
this.#filterComponent = new FilterView({
filters,
currentFilterType: this.#filterModel.filter,
onFilterChange: this.#handleFilterTypeChange
onFilterChange: this.#handleFilterTypeChange,
});

if (prevFilterComponent === null) {
Expand Down
36 changes: 36 additions & 0 deletions src/presenter/presenter-info-panel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import TripInfo from '../view/trip-info.js';
import { remove, render, RenderPosition, replace } from '../framework/render.js';
import { sortByDay } from '../utils/sort.js';

export default class PresenterInfoPanel {
#tripInfoContainer = null;
#pointsModel = null;
#tripInfoComponent = null;

constructor({ tripInfoContainer, pointsModel }) {
this.#tripInfoContainer = tripInfoContainer;
this.#pointsModel = pointsModel;

this.#pointsModel.addObserver(this.#handleModelEvent);
}

init() {
const points = this.#pointsModel.events.sort(sortByDay);
const offers = this.#pointsModel.offers;
const destinations = this.#pointsModel.destinations;
const prevInfoComponent = this.#tripInfoComponent;
this.#tripInfoComponent = new TripInfo(points, offers, destinations);

if (prevInfoComponent === null) {
render(this.#tripInfoComponent, this.#tripInfoContainer, RenderPosition.AFTERBEGIN);
return;
}

replace(this.#tripInfoComponent, prevInfoComponent);
remove(prevInfoComponent);
}

#handleModelEvent = () => {
this.init();
};
}
Loading
Loading