-
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 #12 from SergeySofronov/module7-task1
- Loading branch information
Showing
18 changed files
with
624 additions
and
165 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
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,74 @@ | ||
import { Method } from './const'; | ||
import ApiService from './framework/api-service'; | ||
|
||
export default class EventApiService extends ApiService { | ||
|
||
async getTripData() { | ||
const data = await Promise.all([ | ||
this._load({ url: 'points' }), | ||
this._load({ url: 'offers' }), | ||
this._load({ url: 'destinations' }) | ||
]).then((response) => response.map(ApiService.parseResponse)); | ||
|
||
const result = []; | ||
for await (const item of data) { | ||
result.push(item); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
async createEvent(event) { | ||
const response = await this._load({ | ||
url: 'points', | ||
method: Method.POST, | ||
body: JSON.stringify(this.#adaptToServer(event, true)), | ||
headers: new Headers({ 'Content-Type': 'application/json' }), | ||
}); | ||
|
||
const parsedResponse = await ApiService.parseResponse(response); | ||
|
||
return parsedResponse; | ||
} | ||
|
||
async updateEvent(event) { | ||
const response = await this._load({ | ||
url: `points/${event.id}`, | ||
method: Method.PUT, | ||
body: JSON.stringify(this.#adaptToServer(event)), | ||
headers: new Headers({ 'Content-Type': 'application/json' }), | ||
}); | ||
|
||
const parsedResponse = await ApiService.parseResponse(response); | ||
|
||
return parsedResponse; | ||
} | ||
|
||
async deleteEvent(event) { | ||
const response = await this._load({ | ||
url: `points/${event.id}`, | ||
method: Method.DELETE, | ||
}); | ||
|
||
return response; | ||
} | ||
|
||
#adaptToServer = (event, isCreation = false) => { | ||
const adaptedEvent = { | ||
id: event.id, | ||
'base_price': +event.basePrice, | ||
'date_from': event.dateFrom, | ||
'date_to': event.dateTo, | ||
destination: event.destinationId, | ||
'is_favorite': event.isFavorite, | ||
offers: event.offerIds, | ||
type: event.type, | ||
}; | ||
|
||
if (isCreation) { | ||
delete adaptedEvent.id; | ||
} | ||
|
||
return adaptedEvent; | ||
}; | ||
} |
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 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,16 +1,22 @@ | ||
import { nanoid } from 'nanoid'; | ||
import EventApiService from './event-api-service'; | ||
import TripModel from './model/trip-model'; | ||
import BoardPresenter from './presenter/board-presenter'; | ||
import HeaderPresenter from './presenter/header-presenter'; | ||
|
||
const AUTHORIZATION = `Basic ${nanoid()}`; | ||
const END_POINT = 'https://22.objects.htmlacademy.pro/big-trip'; | ||
|
||
const tripInfoContainer = document.querySelector('.trip-main'); | ||
const filtersContainer = tripInfoContainer.querySelector('.trip-controls__filters'); | ||
const contentContainer = document.querySelector('.trip-events'); | ||
|
||
const model = new TripModel(); | ||
model.init(); | ||
const model = new TripModel(new EventApiService(END_POINT, AUTHORIZATION)); | ||
|
||
const header = new HeaderPresenter(tripInfoContainer, filtersContainer, model); | ||
header.init(); | ||
|
||
const board = new BoardPresenter(contentContainer, tripInfoContainer, model); | ||
board.init(); | ||
|
||
model.init(); |
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
Oops, something went wrong.