@@ -5,7 +5,7 @@ import { RemoteData } from '../../core/data/remote-data';
55import { PaginatedList } from '../../core/data/paginated-list.model' ;
66import { ClarinLicense } from '../../core/shared/clarin/clarin-license.model' ;
77import { getFirstCompletedRemoteData , getFirstSucceededRemoteData } from '../../core/shared/operators' ;
8- import { scan , switchMap } from 'rxjs/operators' ;
8+ import { switchMap } from 'rxjs/operators' ;
99import { PaginationService } from '../../core/pagination/pagination.service' ;
1010import { ClarinLicenseDataService } from '../../core/data/clarin/clarin-license-data.service' ;
1111import { defaultPagination , defaultSortConfiguration } from '../clarin-license-table-pagination' ;
@@ -67,6 +67,11 @@ export class ClarinLicenseTableComponent implements OnInit {
6767 */
6868 searchingLicenseName = '' ;
6969
70+ /**
71+ * Stores the previous search term to detect when a new search should reset pagination.
72+ */
73+ private previousSearchTerm = '' ;
74+
7075 ngOnInit ( ) : void {
7176 this . initializePaginationOptions ( ) ;
7277 this . loadAllLicenses ( ) ;
@@ -315,33 +320,39 @@ export class ClarinLicenseTableComponent implements OnInit {
315320 this . loadAllLicenses ( ) ;
316321 }
317322
323+ /**
324+ * Run a search and reset the route-backed pagination when the search term changes.
325+ */
326+ searchLicenses ( ) {
327+ const hasSearchTermChanged = this . searchingLicenseName !== this . previousSearchTerm ;
328+
329+ if ( hasSearchTermChanged ) {
330+ this . paginationService . resetPage ( this . options . id ) ;
331+ }
332+
333+ this . loadAllLicenses ( hasSearchTermChanged ? 1 : undefined ) ;
334+ this . previousSearchTerm = this . searchingLicenseName ;
335+ }
336+
318337 /**
319338 * Fetch all licenses from the API.
320339 */
321- loadAllLicenses ( ) {
340+ loadAllLicenses ( pageOverride ?: number ) {
322341 this . selectedLicense = null ;
323342 this . licensesRD$ = new BehaviorSubject < RemoteData < PaginatedList < ClarinLicense > > > ( null ) ;
324343 this . isLoading = true ;
325344
326345 // load the current pagination and sorting options
327346 const currentPagination$ = this . getCurrentPagination ( ) ;
328347 const currentSort$ = this . getCurrentSort ( ) ;
329- const searchTerm$ = new BehaviorSubject < string > ( this . searchingLicenseName ) ;
330-
331- observableCombineLatest ( [ currentPagination$ , currentSort$ , searchTerm$ ] ) . pipe (
332- scan ( ( prevState , [ currentPagination , currentSort , searchTerm ] ) => {
333- // If search term has changed, reset to page 1; otherwise, keep current page
334- const currentPage = prevState . searchTerm !== searchTerm ? 1 : currentPagination . currentPage ;
335- return { currentPage, currentPagination, currentSort, searchTerm } ;
336- } , { searchTerm : '' , currentPage : 1 , currentPagination : this . getCurrentPagination ( ) ,
337- currentSort : this . getCurrentSort ( ) } ) ,
338348
339- switchMap ( ( { currentPage, currentPagination, currentSort, searchTerm } ) => {
349+ observableCombineLatest ( [ currentPagination$ , currentSort$ ] ) . pipe (
350+ switchMap ( ( [ currentPagination , currentSort ] ) => {
340351 return this . clarinLicenseService . searchBy ( 'byNameLike' , {
341- currentPage : currentPage , // Properly reset page only when needed
352+ currentPage : pageOverride ?? currentPagination . currentPage ,
342353 elementsPerPage : currentPagination . pageSize ,
343354 sort : { field : currentSort . field , direction : currentSort . direction } ,
344- searchParams : [ new RequestParam ( 'name' , searchTerm ) ]
355+ searchParams : [ new RequestParam ( 'name' , this . searchingLicenseName ) ]
345356 } , false
346357 ) ;
347358 } ) ,
0 commit comments