diff --git a/src/const.js b/src/const.js index 528fc9f..e675ccd 100644 --- a/src/const.js +++ b/src/const.js @@ -1,5 +1,5 @@ const OFFER_COUNT = 5; -const DESTINATION_COUNT = 5; + const POINT_COUNT = 5; const CITIES = [ @@ -14,9 +14,11 @@ const CITIES = [ 'Saint Petersburg', 'Moscow', 'Sochi', - 'Tokio', + 'Tokyo', ]; +const DESTINATION_COUNT = CITIES.length; + const OFFERS = [ 'Order Uber', 'Add luggage', diff --git a/src/mock/destination.js b/src/mock/destination.js index a677cb2..765419a 100644 --- a/src/mock/destination.js +++ b/src/mock/destination.js @@ -1,14 +1,19 @@ -import { getRandomValue, getRandomInteger } from '../utils.js'; -import { CITIES, DESCRIPTION } from '../const.js'; +import { getRandomInteger } from "../utils.js"; +import { CITIES, DESCRIPTION } from "../const.js"; -function generateDestination () { - const city = getRandomValue(CITIES); +const remainingCities = [...CITIES]; + +const generateDestination = () => { + const randomIndex = getRandomInteger(0, remainingCities.length - 1); + const city = remainingCities[randomIndex]; + + remainingCities.splice(randomIndex, 1); return { id: crypto.randomUUID(), description: DESCRIPTION, name: city, - pictures: Array.from({ length: getRandomInteger(1, 5) }, () => ({ + pictures: Array.from({ length: getRandomInteger(0, 5) }, () => ({ 'src': `https://loremflickr.com/248/152?random=${crypto.randomUUID()}`, 'description': `${city} description` })) diff --git a/src/presenter/point-presenter.js b/src/presenter/point-presenter.js index 4e53a44..300f7ba 100644 --- a/src/presenter/point-presenter.js +++ b/src/presenter/point-presenter.js @@ -41,13 +41,13 @@ export default class PointPresenter { this.#editPointComponent = new EditPointView({ point: this.#point, - pointDestination: this.#destinationsModel.getById(point.destination), - pointOffers: this.#offersModel.getByType(point.type), + pointDestination: this.#destinationsModel.get(), + pointOffers: this.#offersModel.get(), onSubmitClick: this.#formSubmitHandler, onResetClick: this.#resetButtonClickHandler }) - if (prevPointComponent === null || prevEditPointComponent === null) { + if (!prevPointComponent || !prevEditPointComponent) { render(this.#pointComponent, this.#container); return; } @@ -57,7 +57,7 @@ export default class PointPresenter { } if (this.#mode === Mode.EDITING) { - replace(this.#pointComponent, prevEditPointComponent); + replace(this.#editPointComponent, prevEditPointComponent); } remove(prevPointComponent); @@ -66,6 +66,7 @@ export default class PointPresenter { resetView = () => { if (this.#mode !== Mode.DEFAULT) { + this.#editPointComponent.reset(this.#point); this.#replaceFormToPoint(); } }; @@ -91,6 +92,7 @@ export default class PointPresenter { #escKeyDownHandler = (evt) => { if (evt.key === 'Escape') { evt.preventDefault(); + this.#editPointComponent.reset(this.#point); this.#replaceFormToPoint(); } }; @@ -112,6 +114,7 @@ export default class PointPresenter { } #resetButtonClickHandler = () => { + this.#editPointComponent.reset(this.#point); this.#replaceFormToPoint(); } } diff --git a/src/presenter/trip-presenter.js b/src/presenter/trip-presenter.js index 68b80d5..5b4f7bb 100644 --- a/src/presenter/trip-presenter.js +++ b/src/presenter/trip-presenter.js @@ -4,7 +4,7 @@ import EmptyListView from '../view/empty-list-view.js'; import PointPresenter from './point-presenter.js'; import { render, replace, remove } from '../framework/render.js'; import { updateItem } from '../utils.js'; -import { SortType, EnabledSortType } from '../const.js'; +import { SortType } from '../const.js'; import { sort } from '../utils/sort.js'; import PointListView from '../view/point-list-view.js'; diff --git a/src/service/mock-service.js b/src/service/mock-service.js index 2bc3fc2..fbcf25b 100644 --- a/src/service/mock-service.js +++ b/src/service/mock-service.js @@ -20,11 +20,35 @@ export default class MockService { return this.destinations; } + getOffers() { + return this.offers; + } + + getPoints() { + return this.points; + } + + generateDestinations() { + return Array.from( + { length: DESTINATION_COUNT }, + () => generateDestination() + ); + } + generateOffers() { - return TYPES.map((type) => ({ - type, - offers: Array.from({ length: getRandomInteger(0, OFFER_COUNT) }, () => generateOffer(type)) - })); + return TYPES.map((type) => { + const offers = Array.from({ length: getRandomInteger(0, OFFER_COUNT) }, () => generateOffer(type)); + + const offersWithRandomSelection = offers.map((offer, index) => ({ + ...offer, + included: index < getRandomInteger(0, offers.length - 1) + })); + + return { + type, + offers: offersWithRandomSelection + }; + }); } generatePoints() { @@ -32,7 +56,9 @@ export default class MockService { const type = getRandomValue(TYPES); const destination = getRandomValue(this.destinations); const hasOffers = getRandomInteger(0, 1); - const offersByType = this.offers.find((offerByType) => offerByType.type === type); + const offersByType = this.offers + .find((offerByType) => offerByType.type === type); + const offerIds = (hasOffers) ? offersByType.offers .slice(0, getRandomInteger(0, OFFER_COUNT)) @@ -42,19 +68,4 @@ export default class MockService { return generatePoint(type, destination.id, offerIds); }); } - - getOffers() { - return this.offers; - } - - getPoints() { - return this.points; - } - - generateDestinations() { - return Array.from( - { length: DESTINATION_COUNT }, - () => generateDestination() - ); - } } diff --git a/src/view/edit-point-view.js b/src/view/edit-point-view.js index 31766d4..63cab6b 100644 --- a/src/view/edit-point-view.js +++ b/src/view/edit-point-view.js @@ -1,5 +1,5 @@ -import AbstractView from '../framework/view/abstract-view.js'; -import { POINT_EMPTY, TYPES, CITIES } from '../const.js'; +import AbstractStatefulView from '../framework/view/abstract-stateful-view.js'; +import { POINT_EMPTY, TYPES, CITIES } from "../const.js"; import { formatStringToDateTime } from '../utils.js'; const createPointCitiesOptionsTemplate = () => { @@ -8,7 +8,7 @@ const createPointCitiesOptionsTemplate = () => { ${CITIES.map((city) => ``).join('')} ` ); -}; +} const createPointPhotosTemplate = (pointDestination) => { return ( @@ -17,7 +17,7 @@ const createPointPhotosTemplate = (pointDestination) => { ``).join('')} ` ); -}; +} const createPointTypesTemplate = (currentType) => { return TYPES.map((type) => @@ -25,13 +25,14 @@ const createPointTypesTemplate = (currentType) => { `).join(''); -}; +} -const createPointOffersTemplate = ({ pointOffers }) => { - const offerItems = pointOffers.map(offer => { +const createPointOffersTemplate = ({ currentOffers }) => { + const offerItems = currentOffers.map(offer => { + const isChecked = offer.included ? 'checked' : ''; return ( `