From af7c685c7059facce2d014c73a72a9bdffe5e7dd Mon Sep 17 00:00:00 2001 From: InsomniaRolodex Date: Tue, 20 Aug 2024 21:24:51 +0300 Subject: [PATCH] Sorting full --- src/mock/points.js | 24 ++++++++++++------------ src/presenter/trip-presenter.js | 13 +++++++------ src/util.js | 18 +++++------------- src/view/sorting-view.js | 4 ++-- src/view/trip-point-view.js | 2 +- 5 files changed, 27 insertions(+), 34 deletions(-) diff --git a/src/mock/points.js b/src/mock/points.js index bfa16b6..cfedac2 100644 --- a/src/mock/points.js +++ b/src/mock/points.js @@ -1,18 +1,6 @@ import { toCamelCase } from '../util.js' export const points = [ - { - id: 'f4b62099-293f-4c3d-a702-94eec4a2808c', - basePrice: 11300, - dateFrom: '2025-07-10T00:55:56.845Z', - dateTo: '2025-07-11T11:01:13.375Z', - destination: 'bfa5cb75-a1fe-4b77-a83c-0e528e910e04', - isFavorite: false, - offers: [ - 'b4c3e4e6-9053-42ce-b747-e281314baa31' - ], - type: 'taxi' - }, { id: 'f4b62099-293f-4c3d-a702-94eec4a2808d', basePrice: 11200, @@ -37,6 +25,18 @@ export const points = [ offers: [], type: 'ship' }, + { + id: 'f4b62099-293f-4c3d-a702-94eec4a2808c', + basePrice: 11300, + dateFrom: '2025-07-10T00:55:56.845Z', + dateTo: '2025-07-11T11:01:13.375Z', + destination: 'bfa5cb75-a1fe-4b77-a83c-0e528e910e04', + isFavorite: false, + offers: [ + 'b4c3e4e6-9053-42ce-b747-e281314baa31' + ], + type: 'taxi' + }, { id: 'f4b62099-293f-4c3d-a702-94eec4a2808f', basePrice: 11100, diff --git a/src/presenter/trip-presenter.js b/src/presenter/trip-presenter.js index 64b283f..46ab5b1 100644 --- a/src/presenter/trip-presenter.js +++ b/src/presenter/trip-presenter.js @@ -5,7 +5,7 @@ import TripListView from '../view/trip-list-view.js'; import ListEmptyView from '../view/list-empty-view.js'; import PointModel from '../model/point-model.js'; import PointPresenter from './point-presenter.js'; -import { updateItem, sortPointsByDay, sortPointsByPrice, sortPointsByTime } from '../util.js'; +import { updateItem, sortPointsByDay, findSortingDuration } from '../util.js'; import { SortingTypes } from '../const.js'; @@ -64,10 +64,11 @@ export default class TripPresenter { this.#points.sort(sortPointsByDay); break; case SortingTypes.PRICE: - this.#points.sort(sortPointsByPrice); + this.#points.sort((pointA, pointB) => pointA.basePrice - pointB.basePrice); break; case SortingTypes.TIME: - this.#points.sort(sortPointsByTime); + + this.#points.sort((pointA, pointB) => findSortingDuration(pointA) - findSortingDuration(pointB)); break; default: this.#points = [...this.#sourcedPointsOrder]; @@ -76,19 +77,19 @@ export default class TripPresenter { } #handleSortingTypeChange(sortingType) { - if (this.#currentSortingType === sortingType) { return; } this.#sortPoints(sortingType); this.#clearPointList(); - this.#points.forEach((point) => this.#renderPoint(point, this.#destinations, this.#offers)); + this.#renderSorting(); + this.#points.forEach((point) => this.#renderPoint(point, this.#destinations, this.#offers)); } #renderSorting() { this.#sortingComponent = new SortingView({ - onSortTypeChange: this.#handleSortingTypeChange + onSortTypeChange: (sortingType) => this.#handleSortingTypeChange(sortingType), }); render(this.#sortingComponent, this.#container); diff --git a/src/util.js b/src/util.js index ee33132..efd21f0 100644 --- a/src/util.js +++ b/src/util.js @@ -5,8 +5,10 @@ const humanizeDueDate = (dueDate, dateFormat) => dueDate ? dayjs(dueDate).format const capitalize = (str) => str[0].toUpperCase() + str.slice(1); -const findDuration = (date1, date2) => { - let minutesDuration = dayjs(date1).diff(dayjs(date2), 'm'); +const findSortingDuration = (point) => dayjs(point.dateTo).diff(dayjs(point.dateFrom), 'm'); + +const findDuration = (point) => { + let minutesDuration = findSortingDuration(point); const minutesAfterHours = minutesDuration % 60; if (minutesDuration >= 60 && minutesAfterHours !== 0) { @@ -62,14 +64,4 @@ const sortPointsByDay = (pointA, pointB) => { return weight ?? dayjs(pointA.dateFrom).diff(dayjs(pointB.dateFrom)); }; -const sortPointsByPrice = (pointA, pointB) => getWeightForDate(pointA.basPrice, pointB.basPrice); - -const sortPointsByTime = (pointA, pointB) => { - pointA = findDuration(pointA.dateFrom, pointA.dateTo); - pointB = findDuration(pointB.dateFrom, pointB.dateTo); - - return getWeightForDate(pointA, pointB); -}; - - -export { humanizeDueDate, capitalize, findDuration, toCamelCase, checkPastPoints, checkPresentPoints, updateItem, sortPointsByDay, sortPointsByPrice, sortPointsByTime }; +export { humanizeDueDate, capitalize, findDuration, toCamelCase, checkPastPoints, checkPresentPoints, updateItem, sortPointsByDay, findSortingDuration }; diff --git a/src/view/sorting-view.js b/src/view/sorting-view.js index 7ea3a02..765b8a9 100644 --- a/src/view/sorting-view.js +++ b/src/view/sorting-view.js @@ -2,11 +2,11 @@ import { SortingTypes } from '../const.js'; import AbstractView from '../framework/view/abstract-view.js'; import { capitalize } from '../util.js'; -const findCheckedElement = (element) => element === 'price' ? 'checked' : ''; +// const findCheckedElement = (element) => element === 'day' ? 'checked' : ''; const createSortingElement = (sortingType) => `
- diff --git a/src/view/trip-point-view.js b/src/view/trip-point-view.js index c7bde7a..37b9373 100644 --- a/src/view/trip-point-view.js +++ b/src/view/trip-point-view.js @@ -34,7 +34,7 @@ const createScheduleTemplate = (point) => { —

-

${findDuration(dateTo, dateFrom)}

+

${findDuration(point)}

€ ${basePrice}