diff --git a/src/presenter/trip-events-presenter.js b/src/presenter/trip-events-presenter.js index c368880..b47c537 100644 --- a/src/presenter/trip-events-presenter.js +++ b/src/presenter/trip-events-presenter.js @@ -3,8 +3,9 @@ import ListView from '../view/list-view.js'; import FilterView from '../view/filter-view.js'; import ListEmptyView from '../view/list-empty-view.js'; import TripPointPresenter from './trip-point-presenter.js'; -import { render} from '../framework/render.js'; -import { LIST_EMPTY_TEXT } from '../const.js'; +import {render} from '../framework/render.js'; +import {LIST_EMPTY_TEXT} from '../const.js'; +import { calculateDateDifference } from '../util.js'; export default class TripEventsPresenter { @@ -62,7 +63,9 @@ export default class TripEventsPresenter { this.#renderPoints(this.#points); break; case 'time': - // code block + this.#points.sort((a, b) => + calculateDateDifference(b.dateTo, b.dateFrom) - calculateDateDifference(a.dateTo, a.dateFrom)); + this.#renderPoints(this.#points); break; case 'price': this.#points.sort((a, b) => a.basePrice - b.basePrice); diff --git a/src/util.js b/src/util.js index ea75fec..a0e958f 100644 --- a/src/util.js +++ b/src/util.js @@ -4,6 +4,13 @@ function formatEventDate(dueDate, dateFormat) { return dueDate ? dayjs(dueDate).format(dateFormat) : ''; } +function formatEventDuration(diff) { + const hours = Math.floor(diff / 60); + const minutes = diff % 60; + + return hours === 0 ? `${minutes}M` : `${hours}H ${minutes}M`; +} + function createId() { let lastGeneratedId = 0; return function () { @@ -19,10 +26,17 @@ function getRandomInteger(a, b) { return Math.floor(result); } +function calculateDateDifference(start, end) { + start = dayjs(start); + end = dayjs(end); + + return end.diff(start, 'minute'); +} + const isElementHas = (element) => element.length > 0; const getRandomArrayElement = (arr) => arr[getRandomInteger(0, arr.length - 1)]; const isEscapeKey = (evt) => evt.key === 'Escape'; -export {createId, getRandomInteger, getRandomArrayElement, formatEventDate, isElementHas, isEscapeKey}; +export {createId, getRandomInteger, getRandomArrayElement, formatEventDate, isElementHas, isEscapeKey, calculateDateDifference, formatEventDuration}; diff --git a/src/view/point-view.js b/src/view/point-view.js index 9da73b3..47c9392 100644 --- a/src/view/point-view.js +++ b/src/view/point-view.js @@ -1,25 +1,29 @@ import AbstractView from '../framework/view/abstract-view.js'; -import { formatEventDate, isElementHas } from '../util.js'; +import { formatEventDate, isElementHas, calculateDateDifference, formatEventDuration } from '../util.js'; const DATE_FORMAT = 'MMM D'; const TIME_FORMAT = 'HH:mm'; function createPointTemplate(point, city, offer) { + const dateFrom = point.dateFrom; + const dateTo = point.dateTo; + const type = point.type; + return( `
- + — - +
- +${formatEventDuration(calculateDateDifference(dateFrom, dateTo))}
€ ${point.basePrice}