11import { animate , state , style , transition , trigger } from '@angular/animations' ;
22import { BreakpointObserver , Breakpoints } from '@angular/cdk/layout' ;
33import { DatePipe , NgIf } from '@angular/common' ;
4- import { AfterViewInit , Component , ElementRef , inject , OnDestroy , OnInit , ViewChild } from '@angular/core' ;
4+ import { AfterViewInit , Component , DestroyRef , ElementRef , inject , OnInit , ViewChild } from '@angular/core' ;
5+ import { takeUntilDestroyed } from '@angular/core/rxjs-interop' ;
56import { MatFormFieldModule } from '@angular/material/form-field' ;
67import { MatInputModule } from '@angular/material/input' ;
78import { MatPaginator , MatPaginatorModule } from '@angular/material/paginator' ;
89import { MatTableDataSource , MatTableModule } from '@angular/material/table' ;
910import { AngularSplitModule , SplitComponent } from 'angular-split' ;
1011import * as L from 'leaflet' ;
11- import { debounceTime , distinctUntilChanged , fromEvent , map , merge , startWith , Subject , switchMap , takeUntil , tap } from 'rxjs' ;
12+ import { debounceTime , distinctUntilChanged , fromEvent } from 'rxjs' ;
1213import { Content } from './content.model' ;
1314import { SearchService } from './search.service' ;
1415
@@ -28,21 +29,23 @@ import { SearchService } from './search.service';
2829 // { provide: SearchService, useClass: SearchServiceMock }
2930 ] ,
3031} )
31- export class SearchComponent implements AfterViewInit , OnInit , OnDestroy {
32+ export class SearchComponent implements AfterViewInit , OnInit {
3233 private breakpointObserver = inject ( BreakpointObserver ) ;
3334 private searchService = inject ( SearchService ) ;
3435
3536 displayedColumns : string [ ] = [ 'title' , 'publication' ] ;
3637 dataSource = new MatTableDataSource < Content > ( ) ;
3738
38- destroyed = new Subject < void > ( ) ;
3939 isSmallSize = false ;
4040
4141 // eslint-disable-next-line @typescript-eslint/no-explicit-any
4242 expandedElement : any ;
4343
44- length = 0 ;
44+ totalElements = 0 ;
4545 author = '' ;
46+ searchTerm = '' ;
47+
48+ destroyRef = inject ( DestroyRef ) ;
4649
4750 private map ! : L . Map ;
4851 private markers ! : L . FeatureGroup ;
@@ -54,7 +57,7 @@ export class SearchComponent implements AfterViewInit, OnInit, OnDestroy {
5457 constructor ( ) {
5558 this . breakpointObserver
5659 . observe ( [ Breakpoints . XSmall , Breakpoints . Small , Breakpoints . Medium , Breakpoints . Large , Breakpoints . XLarge ] )
57- . pipe ( takeUntil ( this . destroyed ) )
60+ . pipe ( takeUntilDestroyed ( ) )
5861 . subscribe ( ( result ) => {
5962 for ( const query of Object . keys ( result . breakpoints ) ) {
6063 if ( result . breakpoints [ query ] ) {
@@ -76,12 +79,23 @@ export class SearchComponent implements AfterViewInit, OnInit, OnDestroy {
7679 ngAfterViewInit ( ) : void {
7780 this . initMap ( ) ;
7881 this . initSplit ( ) ;
79- this . loadContent ( ) ;
82+ this . initInput ( ) ;
83+
84+ this . loadPage ( 0 , 5 ) ;
85+ this . paginator . page . subscribe ( ( ) => {
86+ this . loadPage ( this . paginator . pageIndex , this . paginator . pageSize ) ;
87+ } ) ;
8088 }
8189
82- ngOnDestroy ( ) {
83- this . destroyed . next ( ) ;
84- this . destroyed . complete ( ) ;
90+ /**
91+ * Loads content from backend with parameters which contains much more information and extract the content of articles.
92+ */
93+ loadPage ( pageIndex : number , pageSize : number ) {
94+ this . searchService . fetch ( pageIndex , pageSize , 'datePublished,desc' , this . searchTerm ) . subscribe ( ( response ) => {
95+ this . totalElements = response . totalElements ;
96+ this . dataSource = new MatTableDataSource ( response . content ) ;
97+ this . addToMap ( response . content ) ;
98+ } ) ;
8599 }
86100
87101 /**
@@ -93,37 +107,6 @@ export class SearchComponent implements AfterViewInit, OnInit, OnDestroy {
93107 } ) ;
94108 }
95109
96- /**
97- * Loads content from backend with parameters which contains much more information and extract the content of articles.
98- */
99- private loadContent ( ) : void {
100- const filter = fromEvent ( this . input . nativeElement , 'keyup' ) . pipe (
101- debounceTime ( 500 ) ,
102- distinctUntilChanged ( ) ,
103- tap ( ( ) => this . paginator . firstPage ( ) )
104- ) ;
105-
106- merge ( filter , this . paginator . page )
107- . pipe (
108- takeUntil ( this . destroyed ) ,
109- startWith ( { } ) ,
110- switchMap ( ( ) => {
111- return this . searchService
112- . fetch ( this . paginator . pageIndex , this . paginator . pageSize , 'datePublished,desc' , this . input . nativeElement . value )
113- . pipe (
114- map ( ( data ) => {
115- this . length = data . length ;
116- return data ;
117- } )
118- ) ;
119- } )
120- )
121- . subscribe ( ( content ) => {
122- this . addToMap ( content ) ;
123- this . dataSource = new MatTableDataSource ( content ) ;
124- } ) ;
125- }
126-
127110 /**
128111 * Returns the query from the input field or undefined if the value is empty.
129112 *
@@ -145,6 +128,19 @@ export class SearchComponent implements AfterViewInit, OnInit, OnDestroy {
145128 } ) . addTo ( this . map ) ;
146129 }
147130
131+ /**
132+ * Initialize search field.
133+ */
134+ initInput ( ) : void {
135+ fromEvent ( this . input . nativeElement , 'keyup' )
136+ . pipe ( takeUntilDestroyed ( this . destroyRef ) , debounceTime ( 500 ) , distinctUntilChanged ( ) )
137+ . subscribe ( ( _ ) => {
138+ this . searchTerm = this . input . nativeElement . value ;
139+ this . paginator . firstPage ( ) ;
140+ this . loadPage ( 0 , this . paginator . pageSize ) ;
141+ } ) ;
142+ }
143+
148144 /**
149145 * Integrates content as marker and popup information returned from the server into the initialized map.
150146 *
0 commit comments