Skip to content

Commit ffffb6f

Browse files
[DSC-1437] Fixes wrong behavior in SSR
1 parent 28bef4d commit ffffb6f

3 files changed

Lines changed: 28 additions & 26 deletions

File tree

src/app/lucky-search/search/lucky-search.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ <h5 class="text-center">{{'lucky.search.results.notfound' | translate}} {{curren
3030
</div>
3131
</div>
3232

33-
<div class="w-100" *ngIf="bitstreamFilters?.length">
33+
<div class="w-100" *ngIf="(bitstreamFilters$ | async)?.length">
3434
<div *ngFor="let attachment of (bitstreams$ | async)" class="mb-3 mx-auto" [style.width]="'33%'">
3535
<ds-truncatable [id]="attachment.id">
3636
<ds-file-download-link [bitstream]="attachment" [enableRequestACopy]="true" [item]="(item$ | async)" [showIcon]="true">

src/app/lucky-search/search/lucky-search.component.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { BitstreamDataService, MetadataFilter } from '../../core/data/bitstream-
1919
import { Bitstream } from '../../core/shared/bitstream.model';
2020
import { RouterMock } from '../../shared/mocks/router.mock';
2121
import { MetadataMap, MetadataValue } from '../../core/shared/metadata.models';
22+
import { FileSizePipe } from '../../shared/utils/file-size-pipe';
2223

2324
describe('SearchComponent', () => {
2425
let fixture: ComponentFixture<LuckySearchComponent>;
@@ -67,7 +68,7 @@ describe('SearchComponent', () => {
6768
const routerStub = new RouterMock();
6869
beforeEach(async () => {
6970
await TestBed.configureTestingModule({
70-
declarations: [LuckySearchComponent],
71+
declarations: [LuckySearchComponent, FileSizePipe],
7172
imports: [TranslateModule.forRoot()],
7273
providers: [
7374
{provide: Router, useValue: routerStub},
@@ -187,7 +188,7 @@ describe('SearchComponent', () => {
187188
});
188189
const data = createSuccessfulRemoteDataObject(createPaginatedList([firstSearchResult]));
189190
const metadataFilters = [{ metadataName: 'dc.title', metadataValue: 'test.pdf' }] as MetadataFilter[];
190-
component.bitstreamFilters = metadataFilters;
191+
component.bitstreamFilters$.next(metadataFilters);
191192
bitstreamDataService.findByItem.withArgs(itemUUID, 'ORIGINAL', metadataFilters, {})
192193
.and.returnValue(createSuccessfulRemoteDataObject$(createPaginatedList([bitstream])));
193194

src/app/lucky-search/search/lucky-search.component.ts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { getFirstCompletedRemoteData, getFirstSucceededRemoteData } from '../../
99
import { SearchFilter } from '../../shared/search/models/search-filter.model';
1010
import { LuckySearchService } from '../lucky-search.service';
1111
import { Params, Router } from '@angular/router';
12-
import { filter, map, switchMap, tap } from 'rxjs/operators';
12+
import { filter, map, switchMap, tap, withLatestFrom } from 'rxjs/operators';
1313
import { Context } from '../../core/shared/context.model';
1414
import { SearchConfigurationService } from '../../core/shared/search/search-configuration.service';
1515
import { 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

Comments
 (0)