Skip to content

Commit

Permalink
119207: Reinitialize the BrowseByComponent's objectInjector when star…
Browse files Browse the repository at this point in the history
…tsWithOptions don't exist yet OnInit
  • Loading branch information
alexandrevryghem committed Oct 11, 2024
1 parent e74dc22 commit 4de3c58
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/app/shared/browse-by/browse-by.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ describe('BrowseByComponent', () => {
{ provide: RouteService, useValue: routeServiceStub},
{ provide: SelectableListService, useValue: {} },
{ provide: HostWindowService, useValue: new HostWindowServiceStub(800) },
{ provide: 'startsWithOptions', useValue: [] },
{ provide: 'paginationId', useValue: 'bbm' },
],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents();
Expand Down
30 changes: 20 additions & 10 deletions src/app/shared/browse-by/browse-by.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, EventEmitter, Injector, Input, OnDestroy, OnInit, Output } from '@angular/core';
import { Component, EventEmitter, Injector, Input, OnDestroy, OnInit, Output, SimpleChanges, OnChanges } from '@angular/core';
import { RemoteData } from '../../core/data/remote-data';
import { PaginatedList } from '../../core/data/paginated-list.model';
import { PaginationComponentOptions } from '../pagination/pagination-component-options.model';
Expand Down Expand Up @@ -26,7 +26,7 @@ import { TranslateService } from '@ngx-translate/core';
/**
* Component to display a browse-by page for any ListableObject
*/
export class BrowseByComponent implements OnInit, OnDestroy {
export class BrowseByComponent implements OnInit, OnDestroy, OnChanges {

/**
* ViewMode that should be passed to {@link ListableObjectComponentLoaderComponent}.
Expand Down Expand Up @@ -182,14 +182,7 @@ export class BrowseByComponent implements OnInit, OnDestroy {
}

ngOnInit(): void {
this.objectInjector = Injector.create({
providers: [
{ provide: 'startsWithOptions', useFactory: () => (this.startsWithOptions), deps:[] },
{ provide: 'paginationId', useFactory: () => (this.paginationConfig?.id), deps:[] }
],
parent: this.injector
});

this.generateInjector();
const startsWith$ = this.routeService.getQueryParameterValue('startsWith');
const value$ = this.routeService.getQueryParameterValue('value');

Expand All @@ -199,9 +192,26 @@ export class BrowseByComponent implements OnInit, OnDestroy {
this.sub = this.routeService.getQueryParameterValue(this.paginationConfig.id + '.return').subscribe(this.previousPage$);
}

ngOnChanges(changes: SimpleChanges): void {
if (changes.startsWithOptions || changes.paginationId) {
this.generateInjector();

Check warning on line 197 in src/app/shared/browse-by/browse-by.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/shared/browse-by/browse-by.component.ts#L197

Added line #L197 was not covered by tests
}
}

ngOnDestroy(): void {
if (this.sub) {
this.sub.unsubscribe();
}
}

generateInjector(): void {
this.objectInjector = Injector.create({
providers: [
{ provide: 'startsWithOptions', useFactory: () => (this.startsWithOptions), deps:[] },
{ provide: 'paginationId', useFactory: () => (this.paginationConfig?.id), deps:[] }
],
parent: this.injector
});
}

}

0 comments on commit 4de3c58

Please sign in to comment.