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

fix #17

Merged
merged 13 commits into from
Jun 10, 2024
Merged

fix #17

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
416 changes: 214 additions & 202 deletions package-lock.json

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@
"start": "webpack serve --mode development --open"
},
"devDependencies": {
"@babel/core": "^7.20.5",
"@babel/preset-env": "^7.20.2",
"babel-loader": "^9.1.0",
"copy-webpack-plugin": "^11.0.0",
"css-loader": "^7.1.1",
"@babel/core": "7.20.5",
"@babel/preset-env": "7.20.2",
"babel-loader": "9.1.0",
"copy-webpack-plugin": "11.0.0",
"css-loader": "7.1.1",
"eslint": "8.28.0",
"eslint-config-htmlacademy": "8.0.0",
"html-webpack-plugin": "^5.6.0",
"style-loader": "^4.0.0",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.0",
"webpack-dev-server": "^4.11.1"
"html-webpack-plugin": "5.6.0",
"style-loader": "4.0.0",
"webpack": "5.75.0",
"webpack-cli": "5.0.0",
"webpack-dev-server": "4.11.1"
},
"engines": {
"node": "18"
},
"dependencies": {
"dayjs": "1.11.10",
"flatpickr": "^4.6.13",
"he": "^1.2.0"
"flatpickr": "4.6.13",
"he": "1.2.0"
}
}
6 changes: 3 additions & 3 deletions src/api-service/points-api-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ export default class PointsApiService extends ApiService {

#adaptToServer(point) {
const adaptedPoint = {...point,
'base_price': point.basePrice,
'date_from': point.dateFrom instanceof Date ? point.dueDate.toISOString() : null,
'date_to': point.dateTo instanceof Date ? point.dueDate.toISOString() : null,
'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
10 changes: 2 additions & 8 deletions src/const.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const DEFAULT_TYPE = 'taxi';
const DEFAULT_TYPE = 'flight';

const AUTHORIZATION = 'Basic mofy87osm1d';
const AUTHORIZATION = 'Basic moLfy87osjm1dqP';
const END_POINT = 'https://21.objects.htmlacademy.pro/big-trip';

//пустая точка
Expand Down Expand Up @@ -33,11 +33,6 @@ const FilterType = {
PAST: 'past'
};

const Mode = {
DEFAULT: 'DEFAULT',
EDITING: 'EDITING',
};

const SortType = {
DAY: 'day',
TIME: 'time',
Expand Down Expand Up @@ -87,7 +82,6 @@ export {POINT_EMPTY,
END_POINT,
POINT_TYPE,
FilterType,
Mode,
SortType,
UserAction,
EditingType,
Expand Down
63 changes: 40 additions & 23 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,64 @@
import TripInfoView from './view/trip-info-view.js';
import NewPointButtonView from './view/new-point-button-view.js';

import TripPresenter from './presenter/trip-presenter.js';
import FilterPresenter from './presenter/filter-presenter.js';
import NewPointButtonView from './view/new-point-button-view.js';
import TripInfoPresenter from './presenter/trip-info-presenter.js';

import { render, RenderPosition } from './framework/render.js';
import { render } from './framework/render.js';

import PointsModel from './model/point-model.js';
import DestinationModel from './model/destination-model.js';
import OffersModel from './model/offer-model.js';
import FilterModel from './model/filter-model.js';

import PointsApiService from './api-service/points-api-service.js';
import DestinationsApiService from './api-service/destinations-api-server.js';
import OffersApiService from './api-service/offers-api-server.js';

import { AUTHORIZATION, END_POINT } from './const.js';

const siteMainElement = document.querySelector('.page-main');
const siteMainElement = document.querySelector('.trip-events');
const tripInfoElement = document.querySelector('.trip-main');
const filterElement = tripInfoElement.querySelector('.trip-controls__filters');

const pointsModel = new PointsModel(new PointsApiService(END_POINT, AUTHORIZATION));
const destinationModel = new DestinationModel(new PointsApiService(END_POINT, AUTHORIZATION));
const offersModel = new OffersModel(new PointsApiService(END_POINT, AUTHORIZATION));
const pointsModel = new PointsModel({
pointsApiService: new PointsApiService(END_POINT, AUTHORIZATION)
});
const destinationModel = new DestinationModel({
destinationsApiService: new DestinationsApiService(END_POINT, AUTHORIZATION)
});
const offersModel = new OffersModel({
offersApiService: new OffersApiService(END_POINT, AUTHORIZATION)
});
const filterModel = new FilterModel();

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

const tripInfoPresenter = new TripInfoPresenter({
container: tripInfoElement,
pointsModel,
destinationModel,
offersModel,
});

const tripPresenter = new TripPresenter({
listContainer: siteMainElement,
pointsModel,
filterModel,
destinationsModel: destinationModel,
offersModel,
onNewPointDestroy: handleNewPointFormClose
filterModel,
onNewPointDestroy: handleNewPointFormClose,
newPointButtonComponent
});

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

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

function handleNewPointFormClose() {
newPointButtonComponent.element.disabled = false;
}
Expand All @@ -50,15 +68,14 @@ function handleNewPointButtonClick() {
newPointButtonComponent.element.disabled = true;
}

render(new TripInfoView(), tripInfoElement, RenderPosition.AFTERBEGIN);
async function initModels() {
await destinationModel.init();
await offersModel.init();
await pointsModel.init();
render(newPointButtonComponent, tripInfoElement);
}

tripInfoPresenter.init();
filterPresenter.init();
tripPresenter.init();
offersModel.init().finally(() => {
destinationModel.init().finally(() => {
pointsModel.init().finally(() => {
render(newPointButtonComponent, tripInfoElement);
});
});
});

initModels();
13 changes: 3 additions & 10 deletions src/model/destination-model.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Observable from '../framework/observable';
import { UpdateType } from '../const';

export default class DestinationModel extends Observable{
#destination = [];
#destinationsApiService = null;

constructor({destinationsApiService}) {
constructor({ destinationsApiService }) {
super();
this.#destinationsApiService = destinationsApiService;
}
Expand All @@ -14,22 +15,14 @@ export default class DestinationModel extends Observable{
this.#destination = await this.#destinationsApiService.destinations;
} catch (err) {
this.#destination = [];
this._notify(UpdateType.INIT);
}
}

get destinations() {
return this.#destination;
}

getDestinationByType(type) {
const destination = this.#destination.find((dest) => dest.type === type);

if (destination) {
return destination.destination;
}
return null;
}

getDestinationById(id) {
return this.#destination.find((dest) => dest.id === id);
}
Expand Down
2 changes: 1 addition & 1 deletion src/model/filter-model.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Observable from '../framework/observable.js';
import {FilterType} from '../const.js';
import { FilterType } from '../const.js';

export default class FilterModel extends Observable {
#filter = FilterType.EVERYTHING;
Expand Down
27 changes: 10 additions & 17 deletions src/model/offer-model.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import Observable from '../framework/observable';
import { UpdateType } from '../const';

export default class OffersModel extends Observable{
#allOffers = [];
#offersApiService = null;

constructor({offersApiService}) {
constructor({ offersApiService }) {
super();
this.#offersApiService = offersApiService;
}

async init(){
try {
this.#allOffers = await this.#offersApiService.offers;
} catch (err) {
this.#allOffers = [];
}
}

get allOffers() {
return this.#allOffers;
}
Expand All @@ -25,12 +18,12 @@ export default class OffersModel extends Observable{
return this.#allOffers.find((offer) => offer.type === type);
}

/* getById() {
return this.allOffers.forEach((element) => {
const result = element.offers.find((offer) => offer.id === id);
if (result) {
return result;
}
});
} */
async init(){
try {
this.#allOffers = await this.#offersApiService.offers;
} catch (err) {
this.#allOffers = [];
this._notify(UpdateType.INIT);
}
}
}
10 changes: 5 additions & 5 deletions src/model/point-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default class PointsModel extends Observable {
#pointsApiService = null;
#points = [];

constructor({pointsApiService}) {
constructor({ pointsApiService }) {
super();
this.#pointsApiService = pointsApiService;
}
Expand Down Expand Up @@ -46,7 +46,7 @@ export default class PointsModel extends Observable {
}
}

addPoint = async (updateType, update) => {
async addPoint(updateType, update) {
try {
const response = await this.#pointsApiService.addPoint(update);
const newPoint = this.#adaptToClient(response);
Expand All @@ -55,9 +55,9 @@ export default class PointsModel extends Observable {
} catch (err) {
throw new Error('Can\'t add point');
}
};
}

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

if (index === -1) {
Expand All @@ -74,7 +74,7 @@ export default class PointsModel extends Observable {
} catch (err) {
throw new Error('Can\'t delete point');
}
};
}

#adaptToClient = (point) => {
const adaptedPoint = {
Expand Down
12 changes: 7 additions & 5 deletions src/presenter/filter-presenter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {render, replace, remove} from '../framework/render.js';
import { render, replace, remove } from '../framework/render.js';
import FilterPointsView from '../view/filter-points-view.js';
//import {filter} from '../utils/filter.js';
import {FilterType, UpdateType} from '../const.js';
import { filter } from '../utils/filter.js';
import { FilterType, UpdateType } from '../const.js';

export default class FilterPresenter {
#filterContainer = null;
Expand All @@ -12,7 +12,7 @@ export default class FilterPresenter {

#currentFilter = null;

constructor({filterContainer, filterModel, pointsModel}) {
constructor({ filterContainer, filterModel, pointsModel }) {
this.#filterContainer = filterContainer;
this.#filterModel = filterModel;
this.#pointsModel = pointsModel;
Expand All @@ -22,8 +22,10 @@ export default class FilterPresenter {
}

get filters() {
const points = this.#pointsModel.points;
return Object.values(FilterType).map((type) => ({
type
type,
isDisabled: !filter[type](points).length
}));
}

Expand Down
Loading
Loading