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

Пришёл, увидел, загрузил (часть 1) #13

Merged
merged 47 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
bcd6b2f
module8-task1
n3wstar Jun 9, 2024
16fd139
загрузка данных
n3wstar Jun 9, 2024
b008f4c
fix
n3wstar Jun 9, 2024
bb6b3c7
fix
n3wstar Jun 9, 2024
6e24a64
module8-task1
n3wstar Jun 9, 2024
3f13096
module8-task1
n3wstar Jun 10, 2024
9e49f28
fix
n3wstar Jun 10, 2024
1f17824
fix
n3wstar Jun 10, 2024
835f42e
настройка webpack
n3wstar Feb 26, 2024
309f5d6
разбиение на модули
n3wstar Mar 19, 2024
0ee9e6d
generate mocks
n3wstar Apr 11, 2024
26aaa10
generate mocks
n3wstar Apr 11, 2024
20d2e06
:package:
Apr 15, 2024
f47c466
рефакторинг с ООП
n3wstar Apr 15, 2024
5d9547b
доработка 4.14
n3wstar May 9, 2024
f62d4dc
module5-task1
n3wstar May 16, 2024
b701d45
module5-task2
n3wstar May 20, 2024
a617373
fix
n3wstar May 21, 2024
79515dd
module6-task1
n3wstar May 26, 2024
3c7c05b
module6-task2
n3wstar May 27, 2024
cf93397
фильтры
n3wstar Jun 8, 2024
fdac247
module8-task1
n3wstar Jun 9, 2024
562a49b
загрузка данных
n3wstar Jun 9, 2024
e9ebf37
fix
n3wstar Jun 9, 2024
ae1be0d
fix
n3wstar Jun 9, 2024
fe1fcae
module8-task1
n3wstar Jun 9, 2024
18a8860
module8-task1
n3wstar Jun 10, 2024
8571974
fix
n3wstar Jun 10, 2024
1ff13a8
fix
n3wstar Jun 10, 2024
4aafcd1
Merge branch 'module8-task1' of https://github.com/n3wstar/2433705-bi…
n3wstar Jun 10, 2024
8b5a5da
generate mocks
n3wstar Apr 11, 2024
302d78e
рефакторинг с ООП
n3wstar Apr 15, 2024
f57899a
module5-task1
n3wstar May 16, 2024
8cf9bdb
module5-task2
n3wstar May 20, 2024
fa0db65
module6-task2
n3wstar May 27, 2024
f3da0ba
фильтры
n3wstar Jun 8, 2024
e9f01c7
module8-task1
n3wstar Jun 9, 2024
3dc51a0
загрузка данных
n3wstar Jun 9, 2024
6bd7a41
fix
n3wstar Jun 9, 2024
1184805
fix
n3wstar Jun 9, 2024
ba9bb62
module8-task1
n3wstar Jun 9, 2024
1272288
module8-task1
n3wstar Jun 10, 2024
78c1766
fix
n3wstar Jun 10, 2024
c4bdfce
Merge branch 'module8-task1' of https://github.com/n3wstar/2433705-bi…
n3wstar Jun 10, 2024
6d217ae
fix
n3wstar Jun 10, 2024
e42e968
Merge branch 'master' into module8-task1
n3wstar Jun 10, 2024
22751e9
fix
n3wstar Jun 10, 2024
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
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
Loading