-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from n3wstar/module8-task1
- Loading branch information
Showing
21 changed files
with
264 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,6 @@ <h2 class="visually-hidden">Filter events</h2> | |
</div> | ||
</div> | ||
|
||
|
||
</div> | ||
</div> | ||
</header> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
} |
Oops, something went wrong.