Skip to content

Commit c441319

Browse files
committed
Implemented configurable showing of component
1 parent 66140d1 commit c441319

2 files changed

Lines changed: 51 additions & 9 deletions

File tree

src/app/item-page/simple/field-components/file-section/total-downloads.component.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<ds-metadata-field-wrapper [label]="downloadsLabel | translate">
1+
<ds-metadata-field-wrapper
2+
*ngIf="totalDownloadsEnabled | async"
3+
[label]="downloadsLabel | translate">
24
<div class="total-downloads-section">
35
<div class="total-downloads-count">
46
<span>{{ totalDownloads }}</span>

src/app/item-page/simple/field-components/file-section/total-downloads.component.ts

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Component, OnInit, Input } from '@angular/core';
22
import { UsageReportDataService } from 'src/app/core/statistics/usage-report-data.service';
3+
import { ConfigurationDataService } from 'src/app/core/data/configuration-data.service';
34
import { catchError } from 'rxjs/operators';
4-
import { of } from 'rxjs';
5+
import { BehaviorSubject, of } from 'rxjs';
56

67
/**
78
* Component that displays the total number of downloads for all bitstreams within a DSpace item.
@@ -26,23 +27,34 @@ export class TotalDownloadsComponent implements OnInit {
2627
* The total number of downloads across all bitstreams for the item.
2728
* Defaults to 0 and will show 0 if no data is available or an error occurs.
2829
*/
29-
totalDownloads: number = 0;
30+
totalDownloads = 0;
31+
32+
/**
33+
* Flag indicating whether the total downloads feature is enabled in the configuration.
34+
* Defaults to false to hide downloads unless explicitly enabled in configuration.
35+
*/
36+
totalDownloadsEnabled = new BehaviorSubject<boolean>(false);
3037

3138
/**
3239
* The translation key for the downloadsLabel displayed alongside the download count.
3340
*/
3441
readonly downloadsLabel = 'item.page.files.downloads';
3542

3643

37-
constructor(private usageReportDataService: UsageReportDataService) { }
44+
constructor(
45+
private usageReportDataService: UsageReportDataService,
46+
private configService: ConfigurationDataService
47+
) { }
3848

3949
/**
40-
* Fetches the total download statistics for the item specified by itemUuid.
50+
* Fetches the configuration to check if total downloads should be shown,
51+
* and if enabled, fetches the total download statistics for the item specified by itemUuid.
4152
* The component will:
42-
* 1. Call the UsageReportDataService with the item UUID and 'TotalDownloads' report type
43-
* 2. Aggregate all download counts (views) from all bitstreams in the response
44-
* 3. Set the totalDownloads property with the sum
45-
* 4. Handle errors gracefully by setting totalDownloads to 0 and logging the error
53+
* 1. Check the 'item.view.total.downloads.enabled' configuration property
54+
* 2. If enabled (or config not found - defaults to true), call the UsageReportDataService
55+
* 3. Aggregate all download counts (views) from all bitstreams in the response
56+
* 4. Set the totalDownloads property with the sum
57+
* 5. Handle errors gracefully by setting totalDownloads to 0 and logging the error
4658
*
4759
* @throws Will log an error to console if the API call fails, but won't throw an exception
4860
*/
@@ -51,6 +63,34 @@ export class TotalDownloadsComponent implements OnInit {
5163
return;
5264
}
5365

66+
// First, check if total downloads feature is enabled in configuration
67+
this.configService.findByPropertyName('item.view.total.downloads.enabled')
68+
.pipe(
69+
catchError(error => {
70+
console.error('Failed to fetch total downloads configuration:', error);
71+
// Default to true if configuration cannot be retrieved
72+
return of(null);
73+
})
74+
)
75+
.subscribe(configData => {
76+
// Extract configuration value, default to 'true' if not found
77+
const itemViewTotalDownloadsEnabled = configData?.payload?.values?.[0];
78+
this.totalDownloadsEnabled.next(itemViewTotalDownloadsEnabled === 'true');
79+
80+
// Only fetch download statistics if the feature is enabled
81+
if (this.totalDownloadsEnabled) {
82+
this.fetchDownloadStatistics();
83+
} else {
84+
this.totalDownloads = 0; // Ensure it's 0 when disabled
85+
}
86+
});
87+
}
88+
89+
/**
90+
* Private method to fetch download statistics from the usage report service.
91+
* This method is called only when the total downloads feature is enabled.
92+
*/
93+
private fetchDownloadStatistics(): void {
5494
const reportType = 'TotalDownloads';
5595
this.usageReportDataService.getStatistic(this.itemUuid, reportType)
5696
.pipe(

0 commit comments

Comments
 (0)