Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Большие перемены (часть 2) #18

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
FUTURE: 'future',
PRESENT: 'present',
PAST: 'past'
}

Check failure on line 67 in src/const.js

View workflow job for this annotation

GitHub Actions / Check

Missing semicolon

const Mode = {
DEFAULT: 'default',
Expand All @@ -79,6 +79,14 @@
OFFERS: 'offers'
};

const EnabledSortType = {
[SortType.DAY]: true,
[SortType.EVENT]: false,
[SortType.TIME]: true,
[SortType.PRICE]: true,
[SortType.OFFERS]: false
};

export {
OFFER_COUNT,
DESTINATION_COUNT,
Expand All @@ -92,5 +100,6 @@
POINT_EMPTY,
FilterType,
Mode,
SortType
SortType,
EnabledSortType
}

Check failure on line 105 in src/const.js

View workflow job for this annotation

GitHub Actions / Check

Missing semicolon
40 changes: 37 additions & 3 deletions src/presenter/trip-presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import TripView from '../view/point-list-view.js';
import EmptyListView from '../view/empty-list-view.js';
import PointPresenter from './point-presenter.js';
import { render, replace } from '../framework/render.js';
import { render, replace, remove } from '../framework/render.js';
import { updateItem } from '../utils.js';
import { SortType, EnabledSortType } from '../const.js';

Check failure on line 7 in src/presenter/trip-presenter.js

View workflow job for this annotation

GitHub Actions / Check

'EnabledSortType' is defined but never used
import { sort } from '../utils/sort.js';
import PointListView from '../view/point-list-view.js';

export default class TripPresenter {
Expand All @@ -12,18 +14,21 @@
#offersModel = null;
#pointsModel = null;
#pointListComponent = new TripView();
#sortComponent = new SortView();
#sortComponent = null;

#points = [];

#currentSortType = SortType.DAY;

#pointPresenters = new Map();

constructor({ tripContainer, destinationsModel, offersModel, pointsModel }) {
this.#tripContainer = tripContainer;
this.#destinationsModel = destinationsModel;
this.#offersModel = offersModel;
this.#pointsModel = pointsModel;
this.#points = [...this.#pointsModel.get()];

this.#points = sort[SortType.DAY]([...this.#pointsModel.get()]);
}

init() {
Expand All @@ -42,8 +47,13 @@
pointPresenter.init(point);

this.#pointPresenters.set(point.id, pointPresenter);
}

Check failure on line 50 in src/presenter/trip-presenter.js

View workflow job for this annotation

GitHub Actions / Check

Missing semicolon

#sortPoints = (sortType) => {
this.#currentSortType = sortType;
this.#points = sort[this.#currentSortType](this.#points);
}

Check failure on line 55 in src/presenter/trip-presenter.js

View workflow job for this annotation

GitHub Actions / Check

Missing semicolon

#renderPoints = () => {
this.#points.forEach((point) => {
this.#renderPoint(point);
Expand All @@ -55,6 +65,22 @@
this.#pointPresenters.clear();
}

#renderSort = () => {
const prevSortComponent = this.#sortComponent;

this.#sortComponent = new SortView({
sortType: this.#currentSortType,
onItemChange: this.#sortTypeChangeHandler
});

if (prevSortComponent) {
replace(this.#sortComponent, prevSortComponent);
remove(prevSortComponent);
} else {
render(this.#sortComponent, this.#tripContainer);
}
}

#renderPointContainer = () => {
this.#pointListComponent = new PointListView();
render(this.#pointListComponent, this.#tripContainer);
Expand All @@ -66,6 +92,7 @@
return;
}

this.#renderSort();
this.#renderPointContainer();
this.#renderPoints();
};
Expand All @@ -78,4 +105,11 @@
#modeChangeHandler = () => {
this.#pointPresenters.forEach((presenter) => presenter.resetView());
};

#sortTypeChangeHandler = (sortType) => {
this.#sortPoints(sortType);
this.#clearPoints();
this.#renderSort();
this.#renderPoints();
};
}
20 changes: 19 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ function updateItem(items, update) {
return items.map((item) => item.id === update.id ? update : item);
}

function getPointsDateDifference(pointA, pointB) {
return new Date(pointA.dateFrom) - new Date(pointB.dateFrom);
}

function getPointsPriceDifference(pointA, pointB) {
return pointB.basePrice - pointA.basePrice;
}

function getPointsDurationDifference(pointA, pointB) {
const durationA = new Date(pointA.dateTo) - new Date(pointA.dateFrom);
const durationB = new Date(pointB.dateTo) - new Date(pointB.dateFrom);

return durationB - durationA;
}

export {
getDate,
getRandomInteger,
Expand All @@ -114,5 +129,8 @@ export {
isPointFuture,
isPointPast,
isPointPresent,
updateItem
updateItem,
getPointsDateDifference,
getPointsPriceDifference,
getPointsDurationDifference
}
16 changes: 16 additions & 0 deletions src/utils/sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { SortType } from '../const.js';
import { getPointsDateDifference, getPointsDurationDifference, getPointsPriceDifference } from '../utils.js';

const sort = {
[SortType.DAY]: (points) => points.sort(getPointsDateDifference),
[SortType.PRICE]: (points) => points.sort(getPointsPriceDifference),
[SortType.TIME]: (points) => points.sort(getPointsDurationDifference),
[SortType.EVENT]: () => {
throw new Error(`Sort by ${SortType.EVENT} is not implemented`);
},
[SortType.OFFERS]: () => {
throw new Error(`Sort by ${SortType.OFFERS} is not implemented`);
}
};

export { sort };
79 changes: 54 additions & 25 deletions src/view/sort-view.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,64 @@
import AbstractView from '../framework/view/abstract-view.js';
import { EnabledSortType, SortType } from '../const.js';

const createSortTemplate = () => {
const createSortItemsTemplate = ({ items }) => {
const sortItems = items.map(sortItem => {
return (
`<div class="trip-sort__item trip-sort__item--${sortItem.type}">
<input
id="sort-${sortItem.type}"
class="trip-sort__input visually-hidden"
type="radio"
name="trip-sort"
value="sort-${sortItem.type}"
data-sort-type="${sortItem.type}"
${(sortItem.isChecked) ? 'checked' : ''}
${(sortItem.isDisabled) ? 'disabled' : ''}
>
<label
class="trip-sort__btn"
for="sort-${sortItem.type}">${sortItem.type}</label>
</div>`
)
}).join('');

return sortItems;
}

const createSortTemplate = ({ items }) => {
return (
`<form class="trip-events__trip-sort trip-sort" action="#" method="get">
<div class="trip-sort__item trip-sort__item--day">
<input id="sort-day" class="trip-sort__input visually-hidden" type="radio" name="trip-sort" value="sort-day">
<label class="trip-sort__btn" for="sort-day">Day</label>
</div>

<div class="trip-sort__item trip-sort__item--event">
<input id="sort-event" class="trip-sort__input visually-hidden" type="radio" name="trip-sort" value="sort-event" disabled>
<label class="trip-sort__btn" for="sort-event">Event</label>
</div>
<div class="trip-sort__item trip-sort__item--time">
<input id="sort-time" class="trip-sort__input visually-hidden" type="radio" name="trip-sort" value="sort-time">
<label class="trip-sort__btn" for="sort-time">Time</label>
</div>
<div class="trip-sort__item trip-sort__item--price">
<input id="sort-price" class="trip-sort__input visually-hidden" type="radio" name="trip-sort" value="sort-price" checked>
<label class="trip-sort__btn" for="sort-price">Price</label>
</div>
<div class="trip-sort__item trip-sort__item--offer">
<input id="sort-offer" class="trip-sort__input visually-hidden" type="radio" name="trip-sort" value="sort-offer" disabled>
<label class="trip-sort__btn" for="sort-offer">Offers</label>
</div>
</form>`
${createSortItemsTemplate({ items })}
</form>`
);
};
}

export default class SortView extends AbstractView {
#items = null;
#onItemChange = null;

constructor({ sortType, onItemChange }) {
super();

this.#items = Object.values(SortType).map((type) => ({
type,
isChecked: (type === sortType),
isDisabled: !EnabledSortType[type]
}));

this.#onItemChange = onItemChange;

this.element.addEventListener('change', this.#itemChangeHandler);
}

get template() {
return createSortTemplate();
return createSortTemplate({
items: this.#items
});
}

#itemChangeHandler = (evt) => {
evt.preventDefault();
this.#onItemChange(evt.target.dataset.sortType);
};
}
Loading