Skip to content

Commit

Permalink
Merge pull request #19 from lunianka/module6-task1
Browse files Browse the repository at this point in the history
  • Loading branch information
keksobot authored May 27, 2024
2 parents 744cd2c + 8d007fc commit c833e98
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 83 deletions.
6 changes: 4 additions & 2 deletions src/const.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const OFFER_COUNT = 5;
const DESTINATION_COUNT = 5;

const POINT_COUNT = 5;

const CITIES = [
Expand All @@ -14,9 +14,11 @@ const CITIES = [
'Saint Petersburg',
'Moscow',
'Sochi',
'Tokio',
'Tokyo',
];

const DESTINATION_COUNT = CITIES.length;

const OFFERS = [
'Order Uber',
'Add luggage',
Expand Down
15 changes: 10 additions & 5 deletions src/mock/destination.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { getRandomValue, getRandomInteger } from '../utils.js';
import { CITIES, DESCRIPTION } from '../const.js';
import { getRandomInteger } from "../utils.js";

Check failure on line 1 in src/mock/destination.js

View workflow job for this annotation

GitHub Actions / Check

Strings must use singlequote
import { CITIES, DESCRIPTION } from "../const.js";

Check failure on line 2 in src/mock/destination.js

View workflow job for this annotation

GitHub Actions / Check

Strings must use singlequote

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`
}))
Expand Down
11 changes: 7 additions & 4 deletions src/presenter/point-presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})

Check failure on line 48 in src/presenter/point-presenter.js

View workflow job for this annotation

GitHub Actions / Check

Missing semicolon

if (prevPointComponent === null || prevEditPointComponent === null) {
if (!prevPointComponent || !prevEditPointComponent) {
render(this.#pointComponent, this.#container);
return;
}
Expand All @@ -57,7 +57,7 @@ export default class PointPresenter {
}

if (this.#mode === Mode.EDITING) {
replace(this.#pointComponent, prevEditPointComponent);
replace(this.#editPointComponent, prevEditPointComponent);
}

remove(prevPointComponent);
Expand All @@ -66,6 +66,7 @@ export default class PointPresenter {

resetView = () => {
if (this.#mode !== Mode.DEFAULT) {
this.#editPointComponent.reset(this.#point);
this.#replaceFormToPoint();
}
};
Expand All @@ -91,6 +92,7 @@ export default class PointPresenter {
#escKeyDownHandler = (evt) => {
if (evt.key === 'Escape') {
evt.preventDefault();
this.#editPointComponent.reset(this.#point);
this.#replaceFormToPoint();
}
};
Expand All @@ -112,6 +114,7 @@ export default class PointPresenter {
}

Check failure on line 114 in src/presenter/point-presenter.js

View workflow job for this annotation

GitHub Actions / Check

Missing semicolon

#resetButtonClickHandler = () => {
this.#editPointComponent.reset(this.#point);
this.#replaceFormToPoint();
}

Check failure on line 119 in src/presenter/point-presenter.js

View workflow job for this annotation

GitHub Actions / Check

Missing semicolon
}
2 changes: 1 addition & 1 deletion src/presenter/trip-presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
51 changes: 31 additions & 20 deletions src/service/mock-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,45 @@ 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() {
return Array.from({ length: POINT_COUNT }, () => {
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))
Expand All @@ -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()
);
}
}
Loading

0 comments on commit c833e98

Please sign in to comment.