Skip to content

Commit

Permalink
Use optional chaining, in case the property is not exposed
Browse files Browse the repository at this point in the history
  • Loading branch information
abelgomez committed Oct 10, 2024
1 parent def3032 commit 1565c95
Showing 1 changed file with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,40 +68,40 @@ export class ItemPageCcLicenseFieldComponent implements OnInit {
name: string;
showImage = true;
imgSrc: string;

Check failure on line 71 in src/app/item-page/simple/field-components/specific-field/cc-license/item-page-cc-license-field.component.ts

View workflow job for this annotation

GitHub Actions / tests (18.x)

Trailing spaces not allowed

Check failure on line 71 in src/app/item-page/simple/field-components/specific-field/cc-license/item-page-cc-license-field.component.ts

View workflow job for this annotation

GitHub Actions / tests (20.x)

Trailing spaces not allowed
constructor(
@Inject(APP_CONFIG) protected appConfig: AppConfig,
protected configService: ConfigurationDataService,
) {
}

Check failure on line 77 in src/app/item-page/simple/field-components/specific-field/cc-license/item-page-cc-license-field.component.ts

View workflow job for this annotation

GitHub Actions / tests (18.x)

Trailing spaces not allowed

Check failure on line 77 in src/app/item-page/simple/field-components/specific-field/cc-license/item-page-cc-license-field.component.ts

View workflow job for this annotation

GitHub Actions / tests (20.x)

Trailing spaces not allowed
ngOnInit() {
const regex = /.*creativecommons.org\/(licenses|publicdomain)\/([^/]+)/gm;

this.configService.findByPropertyName('cc.license.uri').pipe(
getFirstCompletedRemoteData(),
getRemoteDataPayload(),
).subscribe((remoteData: ConfigurationProperty) => {
if (this.ccLicenseUriField === undefined) {
// Set the value only if it has not manually set when declaring this component
this.ccLicenseUriField = remoteData.values && remoteData.values.length > 0 ? remoteData.values[0] : 'dc.rights.uri';
this.ccLicenseUriField = remoteData?.values && remoteData?.values?.length > 0 ? remoteData.values[0] : 'dc.rights.uri';
}
this.uri = this.item.firstMetadataValue(this.ccLicenseUriField);
// Extract the CC license code from the URI
const matches = regex.exec(this.uri ?? '') ?? [];
const ccCode = matches.length > 2 ? matches[2] : null;
this.imgSrc = ccCode ? `assets/images/cc-licenses/${ccCode}.png` : null;
});

Check failure on line 95 in src/app/item-page/simple/field-components/specific-field/cc-license/item-page-cc-license-field.component.ts

View workflow job for this annotation

GitHub Actions / tests (18.x)

Trailing spaces not allowed

Check failure on line 95 in src/app/item-page/simple/field-components/specific-field/cc-license/item-page-cc-license-field.component.ts

View workflow job for this annotation

GitHub Actions / tests (20.x)

Trailing spaces not allowed
this.configService.findByPropertyName('cc.license.name').pipe(
getFirstCompletedRemoteData(),
getRemoteDataPayload(),
).subscribe((remoteData: ConfigurationProperty) => {
if (this.ccLicenseNameField === undefined) {
// Set the value only if it has not manually set when declaring this component
this.ccLicenseNameField = remoteData.values && remoteData.values.length > 0 ? remoteData.values[0] : 'dc.rights';
this.ccLicenseNameField = remoteData?.values && remoteData?.values?.length > 0 ? remoteData.values[0] : 'dc.rights';
}
this.name = this.item.firstMetadataValue(this.ccLicenseNameField);
});

this.uri = this.item.firstMetadataValue(this.ccLicenseUriField);
this.name = this.item.firstMetadataValue(this.ccLicenseNameField);

// Extracts the CC license code from the URI
const regex = /.*creativecommons.org\/(licenses|publicdomain)\/([^/]+)/gm;
const matches = regex.exec(this.uri ?? '') ?? [];
const ccCode = matches.length > 2 ? matches[2] : null;
this.imgSrc = ccCode ? `assets/images/cc-licenses/${ccCode}.png` : null;
}
}

0 comments on commit 1565c95

Please sign in to comment.