Skip to content

Commit

Permalink
Merge pull request #12 from SergeySofronov/module7-task1
Browse files Browse the repository at this point in the history
  • Loading branch information
keksobot authored Jan 22, 2025
2 parents 95a0df1 + 146bbcd commit d6f72e3
Show file tree
Hide file tree
Showing 18 changed files with 624 additions and 165 deletions.
36 changes: 29 additions & 7 deletions src/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export const DateFormat = {

export const EventMode = {
DEFAULT: 'default',
FORM: 'form',
};

export const FormMode = {
CREATE: 'create',
EDIT: 'edit',
};

Expand All @@ -26,6 +31,7 @@ export const EventListMessage = {
[FilterType.PAST]: 'There are no past events now',
[FilterType.PRESENT]: 'There are no present events now',
[FilterType.FUTURE]: 'There are no future events now',
LOADING: 'Loading...'
};

export const SortType = {
Expand All @@ -36,21 +42,15 @@ export const SortType = {
OFFER: 'offers',
};

export const FormType = {
CREATE: 'create',
EDIT: 'edit',
};

export const KeyCode = {
ESC: 'Escape',
ENTER: 'Enter',
};

export const tripDefault = {
basePrice: 0,
destination: null,
isFavorite: false,
offers: [],
offerIds: [],
type: 'flight',
};

Expand All @@ -72,3 +72,25 @@ export const TripTitleQuantity = {
MIN: 1,
MAX: 3,
};

export const UserAction = {
UPDATE_EVENT: 'UPDATE_EVENT',
CANCEL_EVENT: 'CANCEL_EVENT',
CREATE_EVENT: 'CREATE_EVENT',
DELETE_EVENT: 'DELETE_EVENT',
};

export const UpdateType = {
PATCH: 'PATCH',
MINOR: 'MINOR',
MAJOR: 'MAJOR',
FILTER: 'FILTER',
INIT: 'INIT',
};

export const Method = {
GET: 'GET',
PUT: 'PUT',
POST: 'POST',
DELETE: 'DELETE',
};
74 changes: 74 additions & 0 deletions src/event-api-service.js
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;
};
}
2 changes: 1 addition & 1 deletion src/framework/api-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class ApiService {

const response = await fetch(
`${this._endPoint}/${url}`,
{method, body, headers},
{ method, body, headers },
);

try {
Expand Down
7 changes: 6 additions & 1 deletion src/framework/view/abstract-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class AbstractView {
#element = null;

/**@type {AbortController} */
#controller = new AbortController();
#controller = null;

constructor() {
if (new.target === AbstractView) {
Expand All @@ -32,6 +32,10 @@ export default class AbstractView {
this.#element = createElement(this.template);
}

if (!this.#controller) {
this.#controller = new AbortController();
}

return this.#element;
}

Expand Down Expand Up @@ -92,6 +96,7 @@ export default class AbstractView {
/** Метод для удаления элемента */
removeElement() {
this.#controller.abort(); // Метод для удаления обработчиков событий элемента
this.#controller = null;
this.#element = null;
}

Expand Down
10 changes: 8 additions & 2 deletions src/main.js
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();
14 changes: 7 additions & 7 deletions src/model/data.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Offer = {
isChecked: boolean;
}

type OfferList = [
type Offers = [
{
type: EventType;
offers: Offer[];
Expand All @@ -32,11 +32,11 @@ type OfferList = [

type RawEvent = {
id: string;
basePrice: number;
dateFrom: string;
dateTo: string;
base_price: number;
date_from: string;
date_to: string;
destination: Id;
isFavorite: boolean;
is_favorite: boolean;
offers: Id[];
type: EventType;
};
Expand All @@ -46,8 +46,8 @@ type TripEvent = {
basePrice: number;
dateFrom: string;
dateTo: string;
destination: Destination;
destinationId: Id;
isFavorite: boolean;
offers: Offer[];
offerIds: Id[];
type: EventType;
};
Loading

0 comments on commit d6f72e3

Please sign in to comment.