Skip to content

Commit 57bea01

Browse files
committed
Don't use manual subscriptions
1 parent c23c075 commit 57bea01

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

src/app/item-page/simple/field-components/specific-field/license/item-page-license-field.component.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ import {
66
import {
77
Component,
88
Input,
9-
OnDestroy,
109
OnInit,
1110
ViewContainerRef,
1211
} from '@angular/core';
1312
import { TranslateModule } from '@ngx-translate/core';
1413
import {
14+
map,
1515
Observable,
16-
of,
17-
Subscription,
1816
} from 'rxjs';
1917
import { ConfigurationDataService } from 'src/app/core/data/configuration-data.service';
2018
import { ConfigurationProperty } from 'src/app/core/shared/configuration-property.model';
@@ -25,6 +23,7 @@ import {
2523
getRemoteDataPayload,
2624
} from 'src/app/core/shared/operators';
2725
import { ItemPageCcLicenseFieldComponent } from 'src/app/item-page/simple/field-components/specific-field/cc-license/item-page-cc-license-field.component';
26+
import { hasValue } from 'src/app/shared/empty.util';
2827
import { MetadataFieldWrapperComponent } from 'src/app/shared/metadata-field-wrapper/metadata-field-wrapper.component';
2928
import { isCcLicense } from 'src/app/shared/utils/license.utils';
3029

@@ -43,7 +42,7 @@ import { isCcLicense } from 'src/app/shared/utils/license.utils';
4342
* appear. In any other case, all the 'dc.rights*' fields will be shown as a list (where the URIs
4443
* will be rendered as links).
4544
*/
46-
export class ItemPageLicenseFieldComponent implements OnInit, OnDestroy {
45+
export class ItemPageLicenseFieldComponent implements OnInit {
4746
/**
4847
* The item to display the license for
4948
*/
@@ -54,8 +53,6 @@ export class ItemPageLicenseFieldComponent implements OnInit, OnDestroy {
5453
*/
5554
@Input() separator = '<br>';
5655

57-
subscriptions: Subscription[] = [];
58-
5956
hasCcLicenseName$: Observable<boolean>;
6057
hasCcLicenseUri$: Observable<boolean>;
6158

@@ -67,28 +64,24 @@ export class ItemPageLicenseFieldComponent implements OnInit, OnDestroy {
6764
protected configService: ConfigurationDataService,
6865
) {}
6966

70-
ngOnDestroy(): void {
71-
this.subscriptions.forEach((sub: Subscription) => sub.unsubscribe());
72-
}
73-
7467
ngOnInit() {
7568
// First, retrieve from the back-end the configuration regarding CC fields...
76-
this.subscriptions.push(this.configService.findByPropertyName('cc.license.uri').pipe(
69+
this.hasCcLicenseName$ = this.configService.findByPropertyName('cc.license.name').pipe(
7770
getFirstCompletedRemoteData(),
7871
getRemoteDataPayload(),
79-
).subscribe((remoteData: ConfigurationProperty) => {
80-
const ccLicenseUriField = remoteData?.values && remoteData?.values?.length > 0 ? remoteData.values[0] : 'dc.rights.uri';
81-
this.hasCcLicenseUri$ = of(isCcLicense(this.item.firstMetadataValue(ccLicenseUriField)));
82-
}),
72+
map((configurationProperty: ConfigurationProperty) => configurationProperty?.values?.[0]),
73+
map((metadataField: string) => hasValue(metadataField) ? metadataField : 'dc.rights'),
74+
map((metadataField: string) => this.item.firstMetadataValue(metadataField)),
75+
map((metadataValue: string) => hasValue(metadataValue)),
8376
);
8477

85-
this.subscriptions.push(this.configService.findByPropertyName('cc.license.name').pipe(
78+
this.hasCcLicenseUri$ = this.configService.findByPropertyName('cc.license.uri').pipe(
8679
getFirstCompletedRemoteData(),
8780
getRemoteDataPayload(),
88-
).subscribe((remoteData: ConfigurationProperty) => {
89-
const ccLicenseNameField = remoteData?.values && remoteData?.values?.length > 0 ? remoteData.values[0] : 'dc.rights';
90-
this.hasCcLicenseName$ = of(!!this.item.firstMetadataValue(ccLicenseNameField));
91-
}),
81+
map((configurationProperty: ConfigurationProperty) => configurationProperty?.values?.[0]),
82+
map((metadataField: string) => hasValue(metadataField) ? metadataField : 'dc.rights'),
83+
map((metadataField: string) => this.item.firstMetadataValue(metadataField)),
84+
map((metadataValue: string) => isCcLicense(metadataValue)),
9285
);
9386

9487
// Now, get the data for this component, in case we need to render the license data as a generic license...

0 commit comments

Comments
 (0)