Skip to content

Commit

Permalink
правки
Browse files Browse the repository at this point in the history
  • Loading branch information
ssftvyn committed Jun 18, 2024
1 parent 2c302aa commit 785c4f6
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 13 deletions.
10 changes: 6 additions & 4 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,22 @@ const newPointButtonComponent = new NewPointButtonView({
});

function newPointCloseHandler() {
newPointButtonComponent.element.disabled = false;
newPointButtonComponent.enableButton();
}

function NewPointButtonClickHandler() {
tripPresenter.createPoint();
newPointButtonComponent.element.disabled = true;
newPointButtonComponent.disableButton();
}

render(newPointButtonComponent, headerContainer);

filterPresenter.init();
tripPresenter.init();
offersModel.init().finally(() => {
destinationsModel.init().finally(() => {
pointsModel.init().finally(() => {
render(newPointButtonComponent, headerContainer);
pointsModel.init().then(() => {
newPointButtonComponent.enableButton();
});
});
});
5 changes: 3 additions & 2 deletions src/model/destinations-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ export default class DestinationModel extends Observable{
this.#destinationsApiService = destinationsApiService;
}

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

async init(){
try{
this.#destinations = await this.#destinationsApiService.destinations;
this._notify(UpdateType.INIT);
} catch (error){
this.#destinations = null;
this._notify(UpdateType.INIT, {error});
}
this._notify(UpdateType.INIT);
}
}
3 changes: 2 additions & 1 deletion src/model/offers-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ export default class OfferModel extends Observable{
async init(){
try{
this.#offers = await this.#offersApiService.offers;
this._notify(UpdateType.INIT);
} catch (error){
this.#offers = null;
this._notify(UpdateType.INIT, {error});
}
this._notify(UpdateType.INIT);
}
}
4 changes: 3 additions & 1 deletion src/model/point-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ export default class PointModel extends Observable{
try{
const points = await this.#pointsApiService.points;
this.#points = points.map(this.#adaptToClient);
this._notify(UpdateType.INIT);
} catch (error){
this.#points = [];
this._notify(UpdateType.INIT, {error});
throw new Error('Points fetch error');
}
this._notify(UpdateType.INIT);
}

async updatePoint(updateType, update) {
Expand Down
18 changes: 17 additions & 1 deletion src/presenter/trip-presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class TripPresenter {
#filterType = FilterType.EVERYTHING;
#headerContainer = document.querySelector('.trip-main');
#isLoading = true;
#isFetchError = false;
#uiBlocker = new UiBlocker({
lowerLimit: TimeLimit.LOWER_LIMIT,
upperLimit: TimeLimit.UPPER_LIMIT
Expand Down Expand Up @@ -129,6 +130,9 @@ class TripPresenter {
this.#renderBoard();
break;
case UpdateType.INIT:
if (data && data.error) {
this.#isFetchError = true;
}
this.#isLoading = false;
remove(this.#loadingComponent);
this.#renderBoard();
Expand Down Expand Up @@ -175,7 +179,15 @@ class TripPresenter {

#renderNoPoints() {
this.#noPoint = new NoPointView({
filterType: this.#filterType
filterType: this.#filterType,
});
render(this.#noPoint, this.#container, RenderPosition.AFTERBEGIN);
}

#renderFetchError() {
this.#noPoint = new NoPointView({
filterType: this.#filterType,
isFetchError: this.#isFetchError
});
render(this.#noPoint, this.#container, RenderPosition.AFTERBEGIN);
}
Expand All @@ -193,6 +205,10 @@ class TripPresenter {
#renderBoard() {
const points = this.points;
const pointsCount = points.length;
if (this.#isFetchError) {
this.#renderFetchError();
return;
}
if (pointsCount === 0 && !this.#isLoading) {
this.#renderNoPoints();
return;
Expand Down
8 changes: 5 additions & 3 deletions src/view/edit-point-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,11 @@ export default class EditingPointView extends AbstractStatefulView{
#changeDestinationHandler = (event) => {
event.preventDefault();
const destination = this.#destinations.find((dest) => dest.name === event.target.value);
this.updateElement({
destination: destination.id,
});
if(destination) {
this.updateElement({
destination: destination.id,
});
}
};

#changeOfferHandler = (event) => {
Expand Down
9 changes: 9 additions & 0 deletions src/view/new-point-button-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,21 @@ export default class NewPointButtonView extends AbstractView {
super();
this.#handleClick = onClick;
this.element.addEventListener('click', this.#clickHandler);
this.disableButton();
}

get template() {
return createNewPointButton();
}

disableButton() {
this.element.disabled = true;
}

enableButton() {
this.element.disabled = false;
}

#clickHandler = (event) => {
event.preventDefault();
this.#handleClick();
Expand Down
15 changes: 14 additions & 1 deletion src/view/no-point-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,27 @@ export const createNoPoint = (filterType) => {
`<p class="trip-events__msg">${noPointTextValue}</p>`
);
};

const CreateFetchError = () =>
`<p class="trip-events__msg">
Failed to load latest route information
</p >
`;

export default class NoPointView extends AbstractView {
#filterType = null;
constructor({filterType}) {
#isFetchError = null;

constructor({filterType, isFetchError = false}) {
super();
this.#filterType = filterType;
this.#isFetchError = isFetchError;
}

get template(){
if (this.#isFetchError) {
return CreateFetchError();
}
return createNoPoint(this.#filterType);
}
}
Expand Down

0 comments on commit 785c4f6

Please sign in to comment.