Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
rasulov1337 committed Nov 10, 2024
1 parent 0b2a987 commit 651ea68
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
13 changes: 5 additions & 8 deletions components/AdCard/AdCard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ interface AdCardState {
/** Карточка объявления на главной странице */
export default class AdCard extends ReactiveComponent {
#data;
#circles: HTMLCollectionOf<Element>;
currentImagePath: string;

/**
Expand All @@ -35,7 +34,6 @@ export default class AdCard extends ReactiveComponent {
});
this.currentImagePath = '';
this.#data = data;
this.#circles = [];
}

addEventListeners() {
Expand Down Expand Up @@ -65,14 +63,14 @@ export default class AdCard extends ReactiveComponent {
const areaFraction =
imgElem.getBoundingClientRect().width / imagesAmount;

this.#circles = this.thisElement.getElementsByClassName(
const circles = this.thisElement.getElementsByClassName(
'housing-card__circle'
);

imgElem.onmousemove = (e) => this.#onMouseMove(e, areaFraction);
imgElem.onmouseout = () => this.#onMouseOut();

this.#circles[this.state.toShowIndex as number].classList.add(
circles[this.state.toShowIndex as number].classList.add(
'housing-card__circle--fill'
);
}
Expand All @@ -89,12 +87,11 @@ export default class AdCard extends ReactiveComponent {
if (toShowIndex === this.state.toShowIndex) {
return;
}
console.log('new to show index:', toShowIndex);

// todo: fix this
// setTimeout(() => {
this.state.toShowIndex = toShowIndex;
// }, SCROLL_DELAY);
setTimeout(() => {
this.state.toShowIndex = toShowIndex;
}, SCROLL_DELAY);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions components/ReactiveComponent/ReactiveComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ export default abstract class ReactiveComponent {
return target[prop];
},
set: (target: { [key: string]: unknown }, prop: string, value) => {
if (target[prop] === value) return true; // Protect from setting the same value & useless rerendering
target[prop] = value;
this.#deps.get(prop).notify();
console.log('gonna rerender');
this.rerender();
this.update();
return true;
},
});
Expand Down Expand Up @@ -114,7 +115,7 @@ export default abstract class ReactiveComponent {
});
}

rerender() {
update() {
this.thisElement = document.getElementById(
this.#elementId
) as HTMLElement;
Expand Down

0 comments on commit 651ea68

Please sign in to comment.