Skip to content

Commit

Permalink
Merge pull request #13 from DashaKukartseva/module6-task2
Browse files Browse the repository at this point in the history
  • Loading branch information
keksobot authored May 23, 2024
2 parents 64cde79 + 107bf5f commit 8fe0b3b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/presenters/trippresenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ListView from '../view/list.js';
import PointPresenter from './pointpresenter.js';
import NoPointView from '../view/nopoint.js';
import { updateItem } from '../utils.js';
import { render, RenderPosition } from '../framework/render.js';;
import { render, RenderPosition } from '../framework/render.js';

export default class TripPointsPresenter {
#listComponent = new ListView();
Expand Down Expand Up @@ -79,4 +79,4 @@ export default class TripPointsPresenter {
pointPresenter.init(point);
this.#pointPresenters.set(point.id, pointPresenter);
}
}
}
49 changes: 19 additions & 30 deletions src/service/mockservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,25 @@ export default class MockService {
#destinations = null;
#offers = null;
#points = null;
constructor() {
this.#destinations = mockDestinations;
this.#offers = mockOffer;
this.#points = this.#generatePoints();
}
constructor() {
this.#destinations = mockDestinations;
this.#offers = mockOffer;
this.#points = this.#generatePoints();}

#generatePoints() {
return Array.from({length: EVENT_COUNT}, () => {
const type = getRandomValue(POINT_TYPES);
const destinationIDs = (getRandomInteger(0, 1)) ? getRandomValue(this.#destinations).id : null;
const offersByType = this.#offers.find((offerByType) => offerByType.type === type);
const offersIDs = [];
offersByType.offers.forEach((offer) => {
if (getRandomInteger(0, 1)) {
offersIDs.push(offer.id);
}
});
return generatePoint(type, offersIDs, destinationIDs);
#generatePoints() {
return Array.from({length: EVENT_COUNT}, () => {
const type = getRandomValue(POINT_TYPES);
const destinationIDs = (getRandomInteger(0, 1)) ? getRandomValue(this.#destinations).id : null;
const offersByType = this.#offers.find((offerByType) => offerByType.type === type);
const offersIDs = [];
offersByType.offers.forEach((offer) => {
if (getRandomInteger(0, 1)) {
offersIDs.push(offer.id);}
});

Check failure on line 25 in src/service/mockservice.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 6 spaces but found 8
}

get destinations() {
return this.#destinations;
}

get offers() {
return this.#offers;
}

get events() {
return this.#points;
}
return generatePoint(type, offersIDs, destinationIDs);

Check failure on line 26 in src/service/mockservice.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 6 spaces but found 8
});

Check failure on line 27 in src/service/mockservice.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 4 spaces but found 6
}

Check failure on line 28 in src/service/mockservice.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 2 spaces but found 4
get destinations() {return this.#destinations;}

Check failure on line 29 in src/service/mockservice.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 2 spaces but found 4

Check failure on line 29 in src/service/mockservice.js

View workflow job for this annotation

GitHub Actions / Check

Expected blank line between class members
get offers() {return this.#offers;}

Check failure on line 30 in src/service/mockservice.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 2 spaces but found 4
get events() {return this.#points;}

Check failure on line 31 in src/service/mockservice.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 2 spaces but found 4
}
66 changes: 63 additions & 3 deletions src/view/modpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ export default class EditablePointView extends AbstractStatefulView {
#datepickerFrom = null;
#datepickerTo = null;

constructor({point = POINT_EMPTY, pointDestination, pointOffers, onEditSubmit, onResetClick}) {
constructor({point = POINT_EMPTY, pointDestination, pointOffers, onEditSubmit, onRollupClick}) {
super();
this.#pointDestination = pointDestination;
this.#pointOffers = pointOffers;
this.#onEditSubmit = onEditSubmit;
this.#onRollupClick = onResetClick;
this.#onRollupClick = onRollupClick;

this._setState(EditablePointView.parseEventToState(point));
this._restoreHandlers();
Expand All @@ -136,6 +136,19 @@ export default class EditablePointView extends AbstractStatefulView {
);
}

removeElement() {
super.removeElement();

if (this.#datepickerFrom) {
this.#datepickerFrom.destroy();
this.#datepickerFrom = null;
}

if (this.#datepickerTo) {
this.#datepickerTo.destroy();
this.#datepickerTo = null;
}
}
_restoreHandlers() {
this.element.querySelector('.event--edit')
.addEventListener('submit', this.#editSubmitHandler);
Expand All @@ -149,7 +162,54 @@ export default class EditablePointView extends AbstractStatefulView {
.addEventListener('change', this.#destinationChangeHandler);
this.element.querySelector('.event__input--price')
.addEventListener('change', this.#priceChangeHandler);
}
this.#setDatepickers();
}

#dateFromCloseHandler = ([userDate]) => {
this._setState({
...this._state,
dateFrom: userDate});
this.#datepickerTo.set('minDate', this._state.dateFrom);
};

#dateToCloseHandler = ([userDate]) => {
this._setState({
...this._state,
dateTo: userDate});
this.#datepickerFrom.set('maxDate', this._state.dateTo);
};

#setDatepickers() {
const [dateFromElement, dateToElement] = this.element.querySelectorAll('.event__input--time');
const commonConfig = {
dateFormat: 'd/m/y H:i',
enableTime: true,
locale: {
firstDayOfWeek: 1,
},
'time_24hr': true
};

this.#datepickerFrom = flatpickr(
dateFromElement,
{
...commonConfig,
defaultDate: this._state.dateFrom,
onClose: this.#dateFromCloseHandler,
maxDate: this._state.dateTo,
},
);

this.#datepickerTo = flatpickr(
dateToElement,
{
...commonConfig,
defaultDate: this._state.dateTo,
onClose: this.#dateToCloseHandler,
minDate: this._state.dateFrom,
},
);
}

#editSubmitHandler = (evt) => {
evt.preventDefault();
Expand Down

0 comments on commit 8fe0b3b

Please sign in to comment.