Skip to content

Commit c4b56f3

Browse files
committed
6.8. Обновление века (часть 1)
1 parent 3a7d1e2 commit c4b56f3

File tree

3 files changed

+90
-54
lines changed

3 files changed

+90
-54
lines changed

src/mocks/destinations.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const destinations = [
3131
},
3232
{
3333
id: 'bfa5cb75-a1fe-4b77-a83c-1e528e910e04',
34-
description: 'Тара та там, ту та я',
34+
description: '',
3535
name: 'Геленджик',
3636
pictures: [],
3737
},

src/presenter/event-presenter.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,6 @@ export default class EventPresenter {
5151
return;
5252
}
5353

54-
// if (this.#listEventsComponent.contains(prevEventComponent.element)) {
55-
// replace(this.#eventComponent, prevEventComponent);
56-
// }
57-
58-
// if (this.#listEventsComponent.contains(prevEventEditComponent.element)) {
59-
// replace(this.#eventEditComponent, prevEventEditComponent);
60-
// }
61-
6254
if (this.#mode === Mode.DEFAULT) {
6355
replace(this.#eventComponent, prevEventComponent);
6456
}
@@ -73,6 +65,7 @@ export default class EventPresenter {
7365

7466
resetView() {
7567
if (this.#mode !== Mode.DEFAULT) {
68+
this.#eventEditComponent.reset(this.#point);
7669
this.#replaceFormToCard();
7770
}
7871
}
@@ -107,6 +100,7 @@ export default class EventPresenter {
107100
#escKeyDownHandler = (evt) => {
108101
if (evt.key === 'Escape') {
109102
evt.preventDefault();
103+
this.#eventEditComponent.reset(this.#point);
110104
this.#replaceFormToCard();
111105
}
112106
};

src/view/edit-event.js

Lines changed: 87 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import AbstractView from '../framework/view/abstract-view.js';
1+
import AbstractStatefulView from '../framework/view/abstract-stateful-view.js';
22
import { humanizeTaskDueDate, DateFormat } from '../utils.js';
33

44
function createOffferTemplate (offer, checkedOffer) {
5-
if (offer.length === 0) {
5+
if (offer.offers.length === 0) {
66
return '';
77
}
88

@@ -104,20 +104,40 @@ function createPictureTemplate(pictures) {
104104
return `${pictures.map((picture) => `<img class="event__photo" src="${picture.src}" alt="${picture.description}">`).join('')}`;
105105
}
106106

107+
function createDetailsTemplate (offer, checkedOffer, description, pictures) {
108+
const offresTemplate = createOffferTemplate(offer, checkedOffer);
109+
const descriptionTemplate = createDescriptionTemplate(description);
110+
const pictureTemplate = createPictureTemplate(pictures);
111+
if (offer.offers.length || description || pictures.length){
112+
return `<section class="event__details">
113+
${offresTemplate}
114+
${description || pictures.length ? `<section class="event__section event__section--destination">
115+
<h3 class="event__section-title event__section-title--destination">Destination</h3>
116+
${description ? descriptionTemplate : ''}
117+
<div class="event__photos-container">
118+
<div class="event__photos-tape">
119+
${pictureTemplate}
120+
</div>
121+
</div>
122+
</section>` : ''}
123+
</section>`;
124+
} else {
125+
return '';
126+
}
127+
}
128+
107129
function createEditEventTemplate(point, checkedOffer, offer, allOffers, destination, allDestinations) {
108130
const {basePrice, dateFrom, dateTo, type} = point;
109131
const pointId = point.id || 0;
110-
const description = destination.description ? destination.description : '';
132+
const description = destination.description;
111133
const pictures = destination.pictures.length > 0 ? destination.pictures : [];
112134

113-
const offresTemplate = createOffferTemplate(offer, checkedOffer);
114135
const buttonsTemplate = createButtonsTemplate(pointId);
115136
const priceTemplate = createPriceTemplate(pointId, basePrice);
116137
const timeTemplate = createTimeTemplate(pointId, dateFrom, dateTo);
117138
const destinationTemplate = createGroupDestinationTemplate(allDestinations, type, destination, pointId);
118139
const typeTemplate = createTypeTemplate(allOffers, type, pointId);
119-
const descriptionTemplate = createDescriptionTemplate(description);
120-
const pictureTemplate = createPictureTemplate(pictures);
140+
const detailsTemplate = createDetailsTemplate(offer, checkedOffer, description, pictures);
121141

122142

123143
return `<li class="trip-events__item">
@@ -133,74 +153,96 @@ function createEditEventTemplate(point, checkedOffer, offer, allOffers, destinat
133153
134154
${buttonsTemplate}
135155
</header>
136-
<section class="event__details">
137-
${offresTemplate}
138-
139-
<section class="event__section event__section--destination">
140-
<h3 class="event__section-title event__section-title--destination">Destination</h3>
141-
${descriptionTemplate}
142-
143-
<div class="event__photos-container">
144-
<div class="event__photos-tape">
145-
${pictureTemplate}
146-
</div>
147-
</div>
148-
</section>
149-
</section>
156+
${detailsTemplate}
150157
</form>
151158
</li>`;
152159
}
153160

154-
export default class EditEventView extends AbstractView {
161+
export default class EditEventView extends AbstractStatefulView {
155162
#pointsModel = null;
156163
#point = null;
157-
#checkedOffer = null;
158-
#offer = null;
159-
#allOffers = null;
160-
#destination = null;
161-
#allDestinations = null;
162164
#handleFormSubmit = null;
163165
#handleFormCloseClick = null;
166+
#destinations = null;
164167

165168
constructor({pointsModel, point, onFormSubmit, onFormCloseClick}) {
166169
super();
167170
this.#pointsModel = pointsModel;
168-
169171
this.#point = point;
170-
this.#checkedOffer = [...this.#pointsModel.getOfferById(this.#point.type, this.#point.offers)];
171-
this.#offer = this.#pointsModel.getOfferByType(this.#point.type);
172-
this.#allOffers = this.#pointsModel.getOffer();
173-
this.#destination = this.#pointsModel.getDestinationById(this.#point.destination);
174-
this.#allDestinations = this.#pointsModel.getDestination();
172+
this._setState(EditEventView.parseEditToState(point));
175173
this.#handleFormSubmit = onFormSubmit;
176174
this.#handleFormCloseClick = onFormCloseClick;
177-
178-
this.element.querySelector('form')
179-
.addEventListener('submit', this.#formFormHandler);
180-
181-
this.element.querySelector('.event__rollup-btn')
182-
.addEventListener('click', this.#formClickHandler);
175+
this.#destinations = this.#pointsModel.getDestination();
176+
this._restoreHandlers();
183177
}
184178

185179
get template() {
186180

187181
return createEditEventTemplate(
188-
this.#point,
189-
this.#checkedOffer,
190-
this.#offer,
191-
this.#allOffers,
192-
this.#destination,
193-
this.#allDestinations,
182+
this._state,
183+
[...this.#pointsModel.getOfferById(this._state.type, this._state.offers)],
184+
this.#pointsModel.getOfferByType(this._state.type),
185+
this.#pointsModel.getOffer(),
186+
this.#pointsModel.getDestinationById(this._state.destination),
187+
this.#pointsModel.getDestination(),
188+
);
189+
}
190+
191+
reset(event) {
192+
this.updateElement(
193+
EditEventView.parseEditToState(event),
194194
);
195195
}
196196

197+
_restoreHandlers() {
198+
this.element.querySelector('form').addEventListener('submit', this.#formFormHandler);
199+
this.element.querySelector('.event__rollup-btn').addEventListener('click', this.#formClickHandler);
200+
this.element.querySelector('.event__type-group').addEventListener('change', this.#typeChangeHandler);
201+
this.element.querySelector('.event__input--destination').addEventListener('change', this.#destinationChangeHandler);
202+
this.element.querySelector('.event__input--price').addEventListener('input', this.#priceInputHandler);
203+
}
204+
197205
#formFormHandler = (evt) => {
198206
evt.preventDefault();
199-
this.#handleFormSubmit(this.#point);
207+
this.#handleFormSubmit(EditEventView.parseStateToEdit(this._state));
200208
};
201209

202210
#formClickHandler = (evt) => {
203211
evt.preventDefault();
212+
this.reset(this.#point);
204213
this.#handleFormCloseClick();
205214
};
215+
216+
#typeChangeHandler = (evt) => {
217+
this.updateElement({type: evt.target.value, offers: []});
218+
};
219+
220+
#destinationChangeHandler = (evt) => {
221+
evt.preventDefault();
222+
const selectedName = evt.target.value.trim();
223+
const selectedDest = this.#destinations.find((dest) => dest.name === selectedName);
224+
225+
if (selectedDest) {
226+
this.updateElement({
227+
destination: selectedDest.id
228+
});
229+
} else {
230+
231+
evt.target.value = '';
232+
}
233+
};
234+
235+
#priceInputHandler = (evt) => {
236+
evt.preventDefault();
237+
const newPrice = parseInt(evt.target.value, 10) || 0;
238+
this._setState({ basePrice: newPrice });
239+
};
240+
241+
static parseEditToState(edit) {
242+
return { ...edit };
243+
}
244+
245+
static parseStateToEdit(state) {
246+
return { ...state };
247+
}
206248
}

0 commit comments

Comments
 (0)