Skip to content

Commit

Permalink
Sorting full
Browse files Browse the repository at this point in the history
  • Loading branch information
InsomniaRolodex committed Aug 20, 2024
1 parent 16304da commit af7c685
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 34 deletions.
24 changes: 12 additions & 12 deletions src/mock/points.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down
13 changes: 7 additions & 6 deletions src/presenter/trip-presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';


Expand Down Expand Up @@ -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];
Expand All @@ -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);
Expand Down
18 changes: 5 additions & 13 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 };
4 changes: 2 additions & 2 deletions src/view/sorting-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => `
<div class="trip-sort__item trip-sort__item--${sortingType}">
<input id="sort-${sortingType}" class="trip-sort__input visually-hidden" type="radio" name="trip-sort" value="sort-${sortingType}" ${findCheckedElement(sortingType)}
<input id="sort-${sortingType}" class="trip-sort__input visually-hidden" type="radio" name="trip-sort" value="sort-${sortingType}"
data-sort-type="${sortingType}"
${sortingType === 'event' ? 'disabled' : ''}
${sortingType === 'offers' ? 'disabled' : ''}>
Expand Down
2 changes: 1 addition & 1 deletion src/view/trip-point-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const createScheduleTemplate = (point) => {
&mdash;
<time class="event__end-time" datetime="${dateTo}">${humanizeDueDate(dateTo, dateFormat.HMM)}</time>
</p>
<p class="event__duration">${findDuration(dateTo, dateFrom)}</p>
<p class="event__duration">${findDuration(point)}</p>
</div>
<p class="event__price">
&euro;&nbsp;<span class="event__price-value">${basePrice}</span>
Expand Down

0 comments on commit af7c685

Please sign in to comment.