Skip to content

Commit

Permalink
Merge pull request #13 from n3wstar/module8-task1
Browse files Browse the repository at this point in the history
  • Loading branch information
keksobot authored Jun 10, 2024
2 parents 28215f7 + 22751e9 commit b46e44a
Show file tree
Hide file tree
Showing 21 changed files with 264 additions and 249 deletions.
1 change: 0 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ <h2 class="visually-hidden">Filter events</h2>
</div>
</div>


</div>
</div>
</header>
Expand Down
33 changes: 33 additions & 0 deletions src/adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function adaptToClient(point) {
const adaptedPoint = {
...point,
basePrice: point['base_price'],
dateFrom: point['date_from'] !== null ? new Date(point['date_from']) : point['date_from'],
dateTo: point['date_to'] !== null ? new Date(point['date_to']) : point['date_to'],
isFavorite: point['is_favorite']
};

delete adaptedPoint['base_price'];
delete adaptedPoint['date_from'];
delete adaptedPoint['date_to'];
delete adaptedPoint['is_favorite'];
return adaptedPoint;
}

function adaptToServer(point) {
const adaptedPoint = {
...point,
['base_price']: Number(point.basePrice),
['date_from']: point.dateFrom instanceof Date ? point.dateFrom.toISOString() : null,
['date_to']: point.dateTo instanceof Date ? point.dateTo.toISOString() : null,
['is_favorite']: point.isFavorite
};

delete adaptedPoint.basePrice;
delete adaptedPoint.dateFrom;
delete adaptedPoint.dateTo;
delete adaptedPoint.isFavorite;
return adaptedPoint;
}

export {adaptToClient, adaptToServer};
18 changes: 16 additions & 2 deletions src/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ const EMPTY_POINT = {
type: DEFAULT_TYPE
};


const TYPES = [
'taxi',
'bus',
'train',
'ship',
'drive',
'flight',
'check-in',
'sightseeing',
'restaurant'
];

const Mode = {
DEFAULT: 'DEFAULT',
EDITING: 'EDITING',
Expand Down Expand Up @@ -63,7 +76,8 @@ const UserAction = {
const UpdateType = {
PATCH:'PATCH',
MINOR: 'MINOR',
MAJOR: 'MAJOR'
MAJOR: 'MAJOR',
INIT: 'INIT'
};


Expand All @@ -75,4 +89,4 @@ const HOUR_IN_DAY = 24;
const MSEC_IN_HOUR = MIN_IN_HOUR * SEC_IN_MIN * MSEC_IN_SEC;
const MSEC_IN_DAY = HOUR_IN_DAY * MSEC_IN_HOUR;

export {EMPTY_POINT, MSEC_IN_DAY, MSEC_IN_HOUR, Mode, SortType, EnabledSortType, UserAction, UpdateType};
export {EMPTY_POINT, MSEC_IN_DAY, MSEC_IN_HOUR, Mode, SortType, EnabledSortType, UserAction, UpdateType, TYPES};
20 changes: 14 additions & 6 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
import TripInfoView from './view/trip-info-view.js';
import BoardPresenter from './presenter/board-presenter.js';
import { render, RenderPosition } from './render.js';
import MockService from './service/mock-service.js';
import DestinationsModel from './model/destinations-model.js';
import OffersModel from './model/offers-model.js';
import PointsModel from './model/points-model.js';
import FilterPresenter from './presenter/filter-presenter.js';
import FiltersModel from './model/filter-model.js';
import PointsApiService from './service/points-api-service.js';
import NewPointButtonView from './view/new-point-view.js';


const bodyElement = document.querySelector('body');
const headerElement = bodyElement.querySelector('.page-header');
const tripInfoElement = headerElement.querySelector('.trip-main');
Expand All @@ -18,10 +19,17 @@ const mainElement = bodyElement.querySelector('.page-main');
const eventListElement = mainElement.querySelector('.trip-events');


const mockService = new MockService();
const destinationsModel = new DestinationsModel(mockService);
const offersModel = new OffersModel(mockService);
const pointsModel = new PointsModel(mockService);
const AUTHORIZATION = 'Basic hk539hbGLpes0Ft';
const END_POINT = 'https://21.objects.htmlacademy.pro/big-trip';

const pointsApiService = new PointsApiService(END_POINT, AUTHORIZATION);

const destinationsModel = new DestinationsModel(pointsApiService);

const offersModel = new OffersModel(pointsApiService);

const pointsModel = new PointsModel({apiService: pointsApiService, destinationsModel, offersModel});

const filtersModel = new FiltersModel();


Expand Down Expand Up @@ -61,5 +69,5 @@ render(new TripInfoView(), tripInfoElement, RenderPosition.AFTERBEGIN);

filterPresenter.init();
boardPresenter.init();

pointsModel.init();

55 changes: 0 additions & 55 deletions src/mock/consts.js

This file was deleted.

18 changes: 0 additions & 18 deletions src/mock/destination.js

This file was deleted.

12 changes: 0 additions & 12 deletions src/mock/offer.js

This file was deleted.

19 changes: 0 additions & 19 deletions src/mock/point.js

This file was deleted.

22 changes: 14 additions & 8 deletions src/model/destinations-model.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
export default class DestinationsModel{
export default class DestinationsModel {
#apiService = null;
#destinations = [];

constructor(service){
this.service = service;
this.destinations = this.service.getDestinations();
constructor(apiService) {
this.#apiService = apiService;
}

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

getById(id){
return this.destinations.find((destination) => destination.id === id);
async init() {
this.#destinations = await this.#apiService.destinations;
return this.#destinations;
}

getById(id) {
return this.#destinations.find((destination) => destination.id === id);
}
}
22 changes: 14 additions & 8 deletions src/model/offers-model.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
export default class OffersModel{
export default class OffersModel {
#apiService = null;
#offers = null;

constructor(service){
this.service = service;
this.offers = this.service.getOffers();
constructor(apiService) {
this.#apiService = apiService;
}

getOffers(){
return this.offers;
get offers() {
return this.#offers;
}

getOfferByType(type){
return this.offers.find((offer) => offer.type === type);
async init() {
this.#offers = await this.#apiService.offers;
return this.#offers;
}

getByType(type) {
return this.#offers.find((offer) => offer.type === type);
}
}
68 changes: 52 additions & 16 deletions src/model/points-model.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,70 @@
import Observable from '../framework/observable';
import { updateItem } from '../utils';
import { UpdateType } from '../const';
import { adaptToClient, adaptToServer } from '../adapter';

export default class PointsModel extends Observable {
#service = null;
#apiService = null;
#points = [];
#destinationsModel = null;
#offersModel = null;

constructor(service){
constructor({apiService, destinationsModel, offersModel}){
super();
this.#service = service;
this.#points = this.#service.getPoints();
this.#apiService = apiService;
this.#destinationsModel = destinationsModel;
this.#offersModel = offersModel;
}

async init() {
try {
await Promise.all([
this.#destinationsModel.init(),
this.#offersModel.init()
]);
const points = await this.#apiService.points;
this.#points = points.map(adaptToClient);
this._notify(UpdateType.INIT, {});
} catch (err) {
this.#points = [];
this._notify(UpdateType.INIT, {isError: true});
}
}

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

update(updateType, point){
const updatedPoint = this.#service.updatePoint(point);
this.#points = updateItem(this.#points, updatedPoint);
this._notify(updateType, updatedPoint);
async update(updateType, point) {
try {
const adaptedPoint = adaptToServer(point);
const updatedPoint = await this.#apiService.updatePoint(adaptedPoint);
const adaptedUpdatedPoint = adaptToClient(updatedPoint);
this.#points = updateItem(this.#points, adaptedUpdatedPoint);
this._notify(updateType, adaptedUpdatedPoint);
} catch (error) {
throw new Error('cant update point');
}
}

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.#apiService.addPoint(adaptToServer(point));
const adaptedPoint = adaptToClient(addedPoint);
this.#points.push(adaptedPoint);
this._notify(updateType, adaptedPoint);
} catch (error){
throw new Error('cant add point');
}
}

deletePoint(updateType, point) {
this.#service.deletePoint(point);
this.#points = this.points.filter((pointItem) => pointItem.id !== point.id);
this._notify(updateType);
async deletePoint(updateType, point) {
try{
await this.#apiService.deletePoint(point);
this.#points = this.#points.filter((pointItem) => pointItem.id !== point.id);
this._notify(updateType);
} catch (error) {
throw new Error('cant delete point');
}
}
}
Loading

0 comments on commit b46e44a

Please sign in to comment.