@@ -9,7 +9,7 @@ import { getFirstCompletedRemoteData, getFirstSucceededRemoteData } from '../../
99import { SearchFilter } from '../../shared/search/models/search-filter.model' ;
1010import { LuckySearchService } from '../lucky-search.service' ;
1111import { Params , Router } from '@angular/router' ;
12- import { filter , map , switchMap , tap } from 'rxjs/operators' ;
12+ import { filter , map , switchMap , tap , withLatestFrom } from 'rxjs/operators' ;
1313import { Context } from '../../core/shared/context.model' ;
1414import { SearchConfigurationService } from '../../core/shared/search/search-configuration.service' ;
1515import { Item } from '../../core/shared/item.model' ;
@@ -54,17 +54,18 @@ export class LuckySearchComponent implements OnInit, OnDestroy {
5454 private SOURCE_METADATA = 'dc.source' ;
5555 private DESCRIPTION_METADATA = 'dc.description' ;
5656
57- bitstreamFilters : MetadataFilter [ ] ;
58- bitstreams$ = new Subject < Bitstream [ ] > ( ) ;
57+ bitstreamFilters$ = new BehaviorSubject < MetadataFilter [ ] > ( null ) ;
58+ bitstreams$ = new BehaviorSubject < Bitstream [ ] > ( null ) ;
5959 item$ = new Subject < Item > ( ) ;
6060
6161 private readonly subscription = new Subscription ( ) ;
6262
63- constructor ( private luckySearchService : LuckySearchService ,
64- private router : Router ,
65- private bitstreamDataService : BitstreamDataService ,
66- public searchConfigService : SearchConfigurationService ) {
67- }
63+ constructor (
64+ private luckySearchService : LuckySearchService ,
65+ private router : Router ,
66+ private bitstreamDataService : BitstreamDataService ,
67+ public searchConfigService : SearchConfigurationService
68+ ) { }
6869
6970 ngOnInit ( ) : void {
7071 this . searchOptions$ = this . getSearchOptions ( ) ;
@@ -81,7 +82,8 @@ export class LuckySearchComponent implements OnInit, OnDestroy {
8182 this . currentFilter . value = queryParams [ key ] ;
8283 }
8384 } ) ;
84- this . bitstreamFilters = this . parseBitstreamFilters ( queryParams ) ;
85+ const value = this . parseBitstreamFilters ( queryParams ) ;
86+ this . bitstreamFilters$ . next ( value ) ;
8587 }
8688 if ( ! ( this . currentFilter . value !== '' && this . currentFilter . identifier !== '' ) ) {
8789 this . showEmptySearchSection = true ;
@@ -101,11 +103,6 @@ export class LuckySearchComponent implements OnInit, OnDestroy {
101103 map ( bitstreams => getBitstreamDownloadRoute ( bitstreams [ 0 ] ) )
102104 ) . subscribe ( bitstreamRoute => this . redirect ( bitstreamRoute ) )
103105 ) ;
104- this . subscription . add (
105- this . bitstreams$ . pipe (
106- filter ( isEmpty )
107- ) . subscribe ( bitstreamRoute => this . showEmptySearchSection = true )
108- ) ;
109106 }
110107
111108 private getLuckySearchResults ( options : PaginatedSearchOptions ) {
@@ -141,17 +138,21 @@ export class LuckySearchComponent implements OnInit, OnDestroy {
141138 this . subscription . add (
142139 this . resultsRD$ . pipe (
143140 filter ( results =>
144- this . isBitstreamSearch ( ) && results ?. payload ?. totalElements === 1
141+ this . hasBitstreamFilters ( ) && results ?. payload ?. totalElements === 1
145142 ) ,
146143 map ( results => results . payload . page [ 0 ] . indexableObject as Item ) ,
147144 tap ( item => this . item$ . next ( item ) ) ,
148- mergeMap ( item => this . loadBitstreamsAndRedirectIfNeeded ( item ) )
149- ) . subscribe ( results => this . bitstreams$ . next ( results ) )
145+ withLatestFrom ( this . bitstreamFilters$ ) ,
146+ mergeMap ( ( [ item , bitstreamFilters ] ) => this . loadBitstreamsAndRedirectIfNeeded ( item , bitstreamFilters ) ) ,
147+ ) . subscribe ( results => {
148+ this . showEmptySearchSection = isEmpty ( results ) ;
149+ this . bitstreams$ . next ( results ) ;
150+ } )
150151 ) ;
151152 this . subscription . add (
152153 this . resultsRD$ . pipe (
153154 filter ( results =>
154- ! this . isBitstreamSearch ( ) && results ?. payload ?. totalElements === 1
155+ ! this . hasBitstreamFilters ( ) && results ?. payload ?. totalElements === 1
155156 ) ,
156157 map ( results => results . payload . page [ 0 ] . indexableObject as Item ) ,
157158 tap ( item => this . item$ . next ( item ) ) ,
@@ -165,8 +166,8 @@ export class LuckySearchComponent implements OnInit, OnDestroy {
165166 ) ;
166167 }
167168
168- private isBitstreamSearch ( ) {
169- return this . bitstreamFilters ?. length ;
169+ private hasBitstreamFilters ( ) : boolean {
170+ return this . bitstreamFilters$ . getValue ( ) ?. length > 0 ;
170171 }
171172
172173 private getSearchOptions ( ) : Observable < PaginatedSearchOptions > {
@@ -193,11 +194,11 @@ export class LuckySearchComponent implements OnInit, OnDestroy {
193194 return [ ] ;
194195 }
195196
196- private loadBitstreamsAndRedirectIfNeeded ( item : Item ) : Observable < Bitstream [ ] > {
197- return this . bitstreamDataService . findByItem ( item . uuid , 'ORIGINAL' , this . bitstreamFilters , { } )
197+ private loadBitstreamsAndRedirectIfNeeded ( item : Item , bitstreamFilters : MetadataFilter [ ] ) : Observable < Bitstream [ ] > {
198+ return this . bitstreamDataService . findByItem ( item . uuid , 'ORIGINAL' , bitstreamFilters , { } )
198199 . pipe (
199200 getFirstCompletedRemoteData ( ) ,
200- map ( bitstreamsResult => bitstreamsResult . payload ?. page )
201+ map ( bitstreamsResult => bitstreamsResult . payload ?. page ) ,
201202 ) ;
202203 }
203204
0 commit comments