Skip to content

Commit

Permalink
fix: infinite scroll + use IntersectionObserver
Browse files Browse the repository at this point in the history
  • Loading branch information
manekinekko committed Mar 13, 2023
1 parent 7b36d4c commit 9e0ce98
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 54 deletions.
41 changes: 0 additions & 41 deletions src/app/search/infinite-scroll.directive.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/app/search/search-result/search-result.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<section
*ngIf="packages.length > 0"
appInfiniteScroll
appInfiniteScroll="load-more"
(scroll)="onScroll()"
>
<app-card
Expand All @@ -9,4 +9,5 @@
[isFullMode]="false"
>
</app-card>
<div class="load-more"></div>
</section>
1 change: 1 addition & 0 deletions src/app/search/search-result/search-result.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ export class SearchResultComponent implements OnChanges {
onScroll() {
this.scrollReachedBottom.emit();
}

}
5 changes: 2 additions & 3 deletions src/app/search/search.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import { NgModule } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { SharedModule } from '../shared/shared.module';
import { MatModule } from './../core/mat/mat.module';
import { InfiniteScrollDirective } from './infinite-scroll.directive';
import { SearchResultComponent } from './search-result/search-result.component';
import { SearchRoutingModule } from './search-routing.module';
import { SearchResultComponent } from './search-result/search-result.component';
import { SearchComponent } from './search/search.component';

@NgModule({
imports: [CommonModule, ReactiveFormsModule, MatModule, SearchRoutingModule, SharedModule],
declarations: [SearchComponent, SearchResultComponent, InfiniteScrollDirective],
declarations: [SearchComponent, SearchResultComponent],
})
export class SearchModule { }
8 changes: 4 additions & 4 deletions src/app/search/search/search.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { debounceTime, distinctUntilChanged, map } from 'rxjs/operators';
import { MetricsService } from 'src/app/shared/metrics.service';
import { PackageType } from 'src/typings';
import { AlgoliaService } from './../../core/algolia/algolia.service';
import { DeeplinkService } from './../deeplink.service';
import { DeeplinkService } from '../../shared/deeplink.service';

@Component({
selector: 'app-search',
Expand Down Expand Up @@ -81,8 +81,8 @@ export class SearchComponent implements OnInit, AfterContentInit {
} else {
// get the actual result
if (this.shouldAppendResults()) {
this.packages.mutate((packages: PackageType[]) => {
packages = [...packages, ...results.hits];
this.packages.update((packages: PackageType[]) => {
return [...packages, ...results.hits];
});

} else {
Expand Down Expand Up @@ -155,7 +155,7 @@ export class SearchComponent implements OnInit, AfterContentInit {
if (!this.hasReachedLastPage()) {
this.shouldAppendResults.set(true);
this.search.nextPage();
this.snackBar.open('Loading Packages...', undefined, {
this.snackBar.open('Loading more packages...', undefined, {
duration: 2000,
horizontalPosition: 'center',
verticalPosition: 'bottom'
Expand Down
2 changes: 1 addition & 1 deletion src/app/shared/card/card.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Renderer2,
} from '@angular/core';
import { Router } from '@angular/router';
import { DeeplinkService } from 'src/app/search/deeplink.service';
import { DeeplinkService } from 'src/app/shared/deeplink.service';
import {
CSSStyleDeclarationWithViewTransitionAPI,
DocumentWithViewTransitionAPI,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AlgoliaService } from './../core/algolia/algolia.service';
import { AlgoliaService } from '../core/algolia/algolia.service';
import { FormGroup } from '@angular/forms';
import { Injectable } from '@angular/core';
import { ActivatedRoute, Router, Params } from '@angular/router';
Expand Down
File renamed without changes.
27 changes: 27 additions & 0 deletions src/app/shared/infinite-scroll.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { DOCUMENT } from '@angular/common';
import {
AfterViewInit,
Directive,
EventEmitter,
Inject,
Input,
Output,
} from '@angular/core';

@Directive({
selector: '[appInfiniteScroll]',
})
export class InfiniteScrollDirective implements AfterViewInit {
@Output() scroll = new EventEmitter();
@Input() appInfiniteScroll = 'load-more';

constructor(@Inject(DOCUMENT) private document: Document) {}

ngAfterViewInit() {
const intersectionObserver = new IntersectionObserver((entries) => {
if (entries[0].intersectionRatio <= 0) return;
this.scroll.emit();
});
intersectionObserver.observe(this.document.querySelector(`.${this.appInfiniteScroll}`) as HTMLElement);
}
}
7 changes: 4 additions & 3 deletions src/app/shared/shared.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { CardComponent } from './card/card.component';
import { MatCardModule } from '@angular/material/card';
import { MatIconModule } from '@angular/material/icon';
import { MatTooltipModule } from '@angular/material/tooltip';
import { HumanDatePipe } from '../search/humain-date.pipe';
import { HumanDatePipe } from './humain-date.pipe';
import { MatChipsModule } from '@angular/material/chips';
import { RouterModule } from '@angular/router';
import { MatButtonModule } from '@angular/material/button';
import { InfiniteScrollDirective } from './infinite-scroll.directive';

const MAT_MODULES = [
MatCardModule,
Expand All @@ -20,7 +21,7 @@ const MAT_MODULES = [

@NgModule({
imports: [CommonModule, RouterModule, ...MAT_MODULES],
declarations: [NotFoundComponent, CardComponent, HumanDatePipe],
exports: [CardComponent]
declarations: [NotFoundComponent, CardComponent, HumanDatePipe, InfiniteScrollDirective],
exports: [NotFoundComponent, CardComponent, HumanDatePipe, InfiniteScrollDirective]
})
export class SharedModule {}

0 comments on commit 9e0ce98

Please sign in to comment.