Skip to content

Commit

Permalink
Merge pull request #6 from Talurion/module5-task1
Browse files Browse the repository at this point in the history
  • Loading branch information
keksobot authored Oct 18, 2024
2 parents 52e9d7a + a986516 commit 14c032d
Show file tree
Hide file tree
Showing 14 changed files with 595 additions and 183 deletions.
33 changes: 26 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"dependencies": {
"dayjs": "1.11.7",
"flatpickr": "4.6.13",
"he": "1.2.0"
"he": "1.2.0",
"nanoid": "4.0.0"
},
"browserslist": [
"last 2 versions",
Expand Down
9 changes: 8 additions & 1 deletion src/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,16 @@ const FilterType = {
PAST: 'past',
};

const SortType = {
DEFAULT: 'default',
PRICE: 'price',
TIME: 'time',
};

export {
EVENTS_TYPES,
CITIES,
SENTENCES,
FilterType
FilterType,
SortType
};
32 changes: 21 additions & 11 deletions src/mock/events.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
import { getRandomArrayElement, getRandomNumber, getRandomBoolean, getRandomDate } from '../utils/common';
import { EVENTS_TYPES } from '../const';
import {nanoid} from 'nanoid';

const TIME_SKIP = 125;

const getRandomEvent = (id, date, destinationsList) => {
const getOfferIdsByType = (offersMap, type) => {
const offerIds = [];

if (offersMap.has(type)) {
const offersById = offersMap.get(type);

offersById.forEach((_, id) => {
offerIds.push(id);
});
}
return offerIds;
};

const getRandomEvent = (date, destinationsList, offersMap) => {
const firstDate = new Date(date);
const secondDate = new Date(firstDate);
const destinationsIds = destinationsList.map((destination) => destination.id);
secondDate.setMinutes(firstDate.getMinutes() + TIME_SKIP);
const randomType = getRandomArrayElement(EVENTS_TYPES).toLowerCase();
const randomEvent = {
'id': `${id}4b62099-293f-4c3d-a702-94eec4a2808c`,
'id': nanoid(),
'base_price': getRandomNumber(499, 4999),
'date_from': firstDate.toISOString(),
'date_to': secondDate.toISOString(),
'destination': getRandomArrayElement(destinationsIds),
'is_favorite': getRandomBoolean(),
'offers': [
'04c3e4e6-9053-42ce-b747-e281314baa31',
'14c3e4e6-9053-42ce-b747-e281314baa31',
'24c3e4e6-9053-42ce-b747-e281314baa31',
'34c3e4e6-9053-42ce-b747-e281314baa31'
],
'type': getRandomArrayElement(EVENTS_TYPES)
'offers': getOfferIdsByType(offersMap, randomType),
'type': randomType,
};
return randomEvent;
};

const getRandomEvents = (count, destinationsList) => {
const getRandomEvents = (count, destinationsList, offersMap) => {
const date = getRandomDate();
const events = [];
for (let i = 0; i < count; i++) {
events.push(getRandomEvent(i, date, destinationsList));
events.push(getRandomEvent(date, destinationsList, offersMap));
date.setMinutes(date.getMinutes() + TIME_SKIP);
}
return events;
Expand Down
36 changes: 20 additions & 16 deletions src/mock/offers.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
import { getRandomNumber} from '../utils/common';
import { getRandomNumber } from '../utils/common';
import { nanoid } from 'nanoid';
import { EVENTS_TYPES } from '../const';

const OFFERS_COUNT = 4;
const OFFERS_COUNT = 3;

const getRandomOffer = (id) => {
const getRandomOffer = (index) => {
const offer = {
'id': `${id }4c3e4e6-9053-42ce-b747-e281314baa31`,
'title': `Upgrade ${ id}`,
'price': getRandomNumber(19,499)
'id': nanoid(),
'title': `Upgrade ${index}`,
'price': getRandomNumber(19, 499)
};
return offer;
};

const getRandomOffers = (type) => {
const randomOffers = [
{
'type': type,
'offers': []
}
];

for (let i = 0; i < OFFERS_COUNT; i ++) {
randomOffers[0].offers.push(getRandomOffer(i));
const getRandomOffersOfSameType = (type) => {
const offers = [];
for (let i = 0; i < OFFERS_COUNT; i++) {
offers.push(getRandomOffer(i));
}
return {
'type': type.toLowerCase(),
'offers': offers
};
};

const getRandomOffers = () => {
const randomOffers = EVENTS_TYPES.map((type) => getRandomOffersOfSameType(type));
return randomOffers;
};

Expand Down
98 changes: 66 additions & 32 deletions src/model/events-connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,74 @@ import { getRandomOffers } from '../mock/offers';
import { getRandomDestinations } from '../mock/destinations';
import { convertKeysToCamelCase } from '../utils/common';

const EVENTS_COUNT = 3;

const createUserEvents = () => {
const offersList = convertKeysToCamelCase(getRandomOffers());
const destinationsList = convertKeysToCamelCase(getRandomDestinations());
const eventsList = convertKeysToCamelCase(getRandomEvents(EVENTS_COUNT, destinationsList));

const destinationsMap = new Map(destinationsList.map((destination) => [destination.id, destination]));
const offers = offersList[0].offers;

const mergedEvents = eventsList.map((event) => {
const destinationData = destinationsMap.get(event.destination);

const offersData = event.offers
.map((offerId) => offers.find((offer) => offer.id === offerId))
.filter(Boolean);

return {
...event,
destination: {
id: destinationData.id,
name: destinationData.name,
description: destinationData.description,
pictures: destinationData.pictures,
},
offers: offersData
};
});

return Array.from(new Map(mergedEvents.map((event) => [event.id, event])).values());
};
const EVENTS_COUNT = 5;

export default class EventsConnector {
#destinationsData = convertKeysToCamelCase(getRandomDestinations());
#offersMap = this.#initializeOffersMap(convertKeysToCamelCase(getRandomOffers()));
#clonedOffersMap = structuredClone(this.#offersMap);
#eventsList = convertKeysToCamelCase(getRandomEvents(EVENTS_COUNT, this.#destinationsData, this.#clonedOffersMap));

#initializeOffersMap (data) {
return data.reduce((offersMap, { type, offers }) => {
const offersById = new Map(
offers.map(({ id, title, price }) => [id, { title, price, isActive: false }])
);
offersMap.set(type, offersById);
return offersMap;
}, new Map());
}

#updateOffersActivity () {
this.#eventsList.forEach(({ offers: eventOffers, type }) => {
const offersById = this.#clonedOffersMap.get(type);
eventOffers.forEach((offerId) => {
const offer = offersById.get(offerId);
if (offer) {
offer.isActive = true;
}
});
});
}

constructor () {
this.#updateOffersActivity();
}

#getOfferById (type, id) {
const offer = this.#clonedOffersMap.get(type)?.get(id);
return offer ? { id, ...offer } : null;
}

#createUserEvents () {
const destinationsMap = new Map(this.#destinationsData.map(({ id, ...rest }) => [id, rest]));

return Array.from(this.#eventsList.map((event) => {
const destinationData = destinationsMap.get(event.destination);
const offersMap = new Map(
event.offers.map((offerId) => this.#getOfferById(event.type, offerId)).filter(Boolean).map((offer) => [offer.id, offer])
);

return {
...event,
destination: { id: destinationData.id, ...destinationData },
offers: offersMap
};
}).reduce((acc, event) => {
acc.set(event.id, event);
return acc;
}, new Map()).values());
}

get userEvents () {
return structuredClone(createUserEvents());
return this.#createUserEvents();
}

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

get offersMap () {
return this.#offersMap;
}
}
11 changes: 11 additions & 0 deletions src/model/events-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@ import EventsConnector from './events-connector';
export default class EventsModel {
#EventsConnector = new EventsConnector;
#eventsList = this.#EventsConnector.userEvents;
#destinationsData = this.#EventsConnector.destinationsData;
#offersMap = this.#EventsConnector.offersMap;

get userEvents () {
return this.#eventsList;
}

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

findDestinationData = (destinationId) => this.#destinationsData.find((destination) => destination.id === destinationId);

getOffersMapByType = (type) => this.#offersMap.get(type) || null;

}
Loading

0 comments on commit 14c032d

Please sign in to comment.