-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from Jey-Key30/module8-task2
- Loading branch information
Showing
8 changed files
with
106 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,66 @@ | ||
import {POINT_EMPTY} from '../mock/const.js'; | ||
import {POINT_EMPTY} from '../const.js'; | ||
import {formatToShortDate, formatToDay} from '../utils.js'; | ||
import AbstractView from '../framework/view/abstract-view.js'; | ||
import dayjs from 'dayjs'; | ||
|
||
const getOffersCoast = (offersIds = [], offers = []) => offersIds.reduce( | ||
(result, id) => result + (offers.find((offer) => offer.id === id)?.price ?? 0), | ||
0 | ||
); | ||
|
||
const getTripCost = (points = [], offers = []) => points.reduce( | ||
(result, point) => result + point.basePrice + getOffersCoast(point.offers, offers.find((offer) => point.type === offer.type)?.offers), | ||
0 | ||
); | ||
|
||
const findDestinationForPoint = (point, pointDestination) => | ||
pointDestination.find((destination) => destination.id === point.destination); | ||
|
||
const createDestinationElement = (pointDestination) => | ||
pointDestination.length <= 3 | ||
? pointDestination.map((destination) => (`${destination} - `)).join('').slice(0, -2) | ||
: `${pointDestination[0]} - ... - ${pointDestination[pointDestination.length - 1]}`; | ||
const createDestinationElement = (destinations) => | ||
destinations.length <= 3 | ||
? destinations.map((destination) => (`${destination} - `)).join('').slice(0, -2) | ||
: `${destinations[0]} - ... - ${destinations[destinations.length - 1]}`; | ||
|
||
const createTripInfoTemplate = ({points, pointDestination}) => (`<section class="trip-main__trip-info trip-info"> | ||
const createTripInfoTemplate = ({points, destination, isEmpty, cost}) => | ||
(`${!isEmpty | ||
? `<section class="trip-main__trip-info trip-info"> | ||
<div class="trip-info__main"> | ||
<h1 class="trip-info__title">${createDestinationElement(pointDestination)}</h1> | ||
<h1 class="trip-info__title">${createDestinationElement(destination)}</h1> | ||
<p class="trip-info__dates">${formatToShortDate(points[0].dateFrom)} — | ||
${dayjs(points[points.length - 1].dateTo).month() === dayjs(points[0].dateFrom).month() | ||
? formatToDay(points[points.length - 1].dateTo) | ||
: formatToShortDate(points[points.length - 1].dateTo)}</p> | ||
? formatToDay(points[points.length - 1].dateTo) | ||
: formatToShortDate(points[points.length - 1].dateTo)}</p> | ||
</div> | ||
<p class="trip-info__cost"> | ||
Total: € <span class="trip-info__cost-value"> | ||
${points.reduce((x, point) => (x + point.basePrice), 0)}</span> | ||
${cost}</span> | ||
</p> | ||
</section>`); | ||
</section>` | ||
: '' | ||
}`); | ||
|
||
export default class TripInfoView extends AbstractView { | ||
#points = null; | ||
#pointDestination = []; | ||
#points = 0; | ||
#destination = []; | ||
#offers = []; | ||
|
||
constructor({points = POINT_EMPTY, pointDestination}) { | ||
constructor({points = POINT_EMPTY, destinations, offers}) { | ||
super(); | ||
this.#points = points; | ||
this.#pointDestination = points | ||
.map((point) => findDestinationForPoint(point, pointDestination)) | ||
this.#offers = offers; | ||
this.#destination = points | ||
.map((point) => findDestinationForPoint(point, destinations)) | ||
.map((destination) => destination.name); | ||
} | ||
|
||
get template() { | ||
return createTripInfoTemplate({ | ||
points: this.#points, | ||
pointDestination: this.#pointDestination, | ||
destination: this.#destination, | ||
isEmpty: this.#points.length === 0, | ||
cost: getTripCost(this.#points, this.#offers) | ||
}); | ||
} | ||
} |