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

feat: lecture-5 HW #122

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ import {PopupHostModule} from './components/popup-host/popup-host.module';
PopupHostModule,
],
bootstrap: [AppComponent],
exports: [],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Неплохо было бы подчистить)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Принял

})
export class AppModule {}
28 changes: 14 additions & 14 deletions src/app/pages/products-list/products-list.component.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<ng-template let-productsFromNgIf [ngIf]="getFilteredProducts" [ngIfElse]="loader">
<!-- Раскомментировать div при выполнении ДЗ -->
<!-- <div appScrollWithLoading class="scroll-container" (loadData)="onLoad($event)"> -->
<!-- <ng-template [appDumpNgIf]="getFilteredProducts" let-productsFromNgIf> -->
<!-- {{productsFromNgIf}} -->
<app-card class="card" [product]="productsFromNgIf?.[0]"></app-card>
<app-card class="card" [product]="productsFromNgIf?.[1]"></app-card>
<app-card class="card" [product]="productsFromNgIf?.[2]"></app-card>
<app-card class="card" [product]="productsFromNgIf?.[3]"></app-card>
<app-card class="card" [product]="productsFromNgIf?.[4]"></app-card>
<app-card class="card" [product]="productsFromNgIf?.[0]"></app-card>
<app-card class="card" [product]="productsFromNgIf?.[1]"></app-card>
<app-card class="card" [product]="productsFromNgIf?.[2]"></app-card>
<app-card class="card" [product]="productsFromNgIf?.[3]"></app-card>
<app-card class="card" [product]="productsFromNgIf?.[4]"></app-card>
<!-- </div> -->
<div appScrollWithLoading class="scroll-container" (loadData)="onLoad($event)">
<!-- <ng-template [appDumpNgIf]="getFilteredProducts" let-productsFromNgIf> -->
<!-- {{productsFromNgIf}} -->
<app-card class="card" [product]="productsFromNgIf?.[0]"></app-card>
<app-card class="card" [product]="productsFromNgIf?.[1]"></app-card>
<app-card class="card" [product]="productsFromNgIf?.[2]"></app-card>
<app-card class="card" [product]="productsFromNgIf?.[3]"></app-card>
<app-card class="card" [product]="productsFromNgIf?.[4]"></app-card>
<app-card class="card" [product]="productsFromNgIf?.[0]"></app-card>
<app-card class="card" [product]="productsFromNgIf?.[1]"></app-card>
<app-card class="card" [product]="productsFromNgIf?.[2]"></app-card>
<app-card class="card" [product]="productsFromNgIf?.[3]"></app-card>
<app-card class="card" [product]="productsFromNgIf?.[4]"></app-card>
</div>
</ng-template>

<!-- <ng-template [ngIf]="!products"> -->
Expand Down
5 changes: 5 additions & 0 deletions src/app/pages/products-list/products-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ export class ProductsListComponent implements OnInit {
// eslint-disable-next-line no-console
console.log(event);
}

onLoad(event: unknown) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почему в типизации поставил unknown? Разве не известно, что будет приходить?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это была заглушка про которую я потом забыл, исправлю

// eslint-disable-next-line no-console
console.log(event);
}
}
9 changes: 8 additions & 1 deletion src/app/pages/products-list/products-list.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {MatProgressSpinnerModule} from '@angular/material/progress-spinner';
import {ScrollWithLoadModule} from 'src/app/shared/scroll-with-load/scroll-with-load.module';
import {ProductsListComponent} from './products-list.component';
import {CardModule} from './card/card.module';
import {DumpNgIfModule} from '../../shared/dump-ng-if/dump-ng-if.module';

@NgModule({
declarations: [ProductsListComponent],
imports: [CommonModule, CardModule, DumpNgIfModule, MatProgressSpinnerModule],
imports: [
CommonModule,
CardModule,
DumpNgIfModule,
MatProgressSpinnerModule,
ScrollWithLoadModule,
],
exports: [ProductsListComponent],
})
export class ProductsListModule {}
4 changes: 4 additions & 0 deletions src/app/shared/scroll-with-load/load-direction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum LoadDirection {
UP,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше значениям enum давать говорящие имена, а не использовать дефолтные (0, 1, 2, ...)
Почему говорящие имена лучше? - если захотим добавить логирование или собирать метрики, то сделать это будет проще, т.к. будет отправляться не 0, 1 и т.д., а, например, before, after и т.д.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Спасибо за совет, думаю так действительно лучше

DOWN,
}
1 change: 1 addition & 0 deletions src/app/shared/scroll-with-load/scroll-const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const SCROLL_CONST = 100;
10 changes: 10 additions & 0 deletions src/app/shared/scroll-with-load/scroll-with-load.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {ScrollWithLoadingDirective} from './scroll-with-loading.directive';

@NgModule({
declarations: [ScrollWithLoadingDirective],
imports: [CommonModule],
exports: [ScrollWithLoadingDirective],
})
export class ScrollWithLoadModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {ScrollWithLoadingDirective} from './scroll-with-loading.directive';

describe('ScrollWithLoadingDirective', () => {
it('should create an instance', () => {
const directive = new ScrollWithLoadingDirective();

expect(directive).toBeTruthy();
});
});
39 changes: 39 additions & 0 deletions src/app/shared/scroll-with-load/scroll-with-loading.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {Directive, Output, EventEmitter, HostListener} from '@angular/core';
import {LoadDirection} from './load-direction';
import {bottomLoadCondition} from './utils/bottom-load-condition';
import {upLoadCondition} from './utils/up-load-condition';

@Directive({
selector: '[appScrollWithLoading]',
})
export class ScrollWithLoadingDirective {
@Output() loadData = new EventEmitter<LoadDirection>();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я бы readonly добавил еще)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Принял


private prevScrollTop = -1; // Значение требуется для сравнения, из него можно вычислить направление скролла.

@HostListener('scroll', ['$event.target']) scrollAction({
scrollHeight,
clientHeight,
scrollTop,
}: HTMLElement) {
const prevScrollTop = this.prevScrollTop; // Сохраняем предыдущее значение скролла

this.prevScrollTop = scrollTop; // Заменяем текущим значением скролла

const lowerScrollPosition = scrollHeight - clientHeight; // Нижнее значение на которое может быть промотан скролл. lowerScrollPosition + clientHeight = scrollHeight

const loadDown = bottomLoadCondition(scrollTop, lowerScrollPosition, prevScrollTop);

if (loadDown) {
this.loadData.emit(LoadDirection.DOWN);

return;
}

const loadUp = upLoadCondition(scrollTop, prevScrollTop);

if (loadUp) {
this.loadData.emit(LoadDirection.UP);
}
}
}
14 changes: 14 additions & 0 deletions src/app/shared/scroll-with-load/utils/bottom-load-condition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {SCROLL_CONST} from '../scroll-const';

export function bottomLoadCondition(
scrollTop: number,
lowerScrollPosition: number,
prevScrollTop: number,
): boolean {
const nearBotttomBorder = lowerScrollPosition - scrollTop < SCROLL_CONST;
// true, когда до нижней границы останется меньше значения SCROLL_CONST;
const bottomScrollContinue = scrollTop > prevScrollTop;
// Проверяет направление промотки, и не даёт сработать если промотка идёт вверх.

return nearBotttomBorder && bottomScrollContinue;
}
10 changes: 10 additions & 0 deletions src/app/shared/scroll-with-load/utils/up-load-condition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {SCROLL_CONST} from '../scroll-const';

export function upLoadCondition(scrollTop: number, prevScrollTop: number): boolean {
const nearUpBorder = scrollTop < SCROLL_CONST;
// true, когда попадаем в диапазон 0 - SCROLL_CONST
const scrollUpContinue = scrollTop < prevScrollTop;
// проверяет направление, выбросит false если промотка направлена вниз

return nearUpBorder && scrollUpContinue;
}