Adding the new embargo badge on the file download component

Fixed lint no-trailing-spaces
This commit is contained in:
Nicolas Boulay
2025-01-22 08:18:25 -05:00
parent 5b7d246f68
commit 5f83139fcd
24 changed files with 389 additions and 18 deletions

View File

@@ -350,6 +350,8 @@ item:
# Rounded to the nearest size in the list of selectable sizes on the
# settings menu. See pageSizeOptions in 'pagination-component-options.model.ts'.
pageSize: 5
# Show the bitstream access status label
showAccessStatuses: false
# Community Page Config
community:

View File

@@ -45,7 +45,7 @@ describe('ItemAdminSearchResultGridElementComponent', () => {
};
const mockAccessStatusDataService = {
findAccessStatusFor(item: Item): Observable<RemoteData<AccessStatusObject>> {
findItemAccessStatusFor(item: Item): Observable<RemoteData<AccessStatusObject>> {
return createSuccessfulRemoteDataObject$(new AccessStatusObject());
},
};

View File

@@ -11,6 +11,7 @@ import { HALEndpointServiceStub } from '../../shared/testing/hal-endpoint-servic
import { NotificationsServiceStub } from '../../shared/testing/notifications-service.stub';
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
import { ObjectCacheService } from '../cache/object-cache.service';
import { Bitstream } from '../shared/bitstream.model';
import { Item } from '../shared/item.model';
import { AccessStatusDataService } from './access-status-data.service';
import { RemoteData } from './remote-data';
@@ -41,16 +42,44 @@ describe('AccessStatusDataService', () => {
},
});
const bitstreamId = '3d4c730u-5a4b-438b-9686-be1d5b4a1c5a';
const mockBitstream: Bitstream = Object.assign(new Bitstream(), {
id: bitstreamId,
name: 'test-bitstream',
_links: {
accessStatus: {
href: `https://rest.api/core/bitstreams/${bitstreamId}/accessStatus`,
},
self: {
href: `https://rest.api/core/bitstreams/${bitstreamId}`,
},
},
});
describe('when the requests are successful', () => {
beforeEach(() => {
createService();
});
describe('when calling findAccessStatusFor', () => {
describe('when calling findItemAccessStatusFor', () => {
let contentSource$;
beforeEach(() => {
contentSource$ = service.findAccessStatusFor(mockItem);
contentSource$ = service.findItemAccessStatusFor(mockItem);
});
it('should send a new GetRequest', fakeAsync(() => {
contentSource$.subscribe();
tick();
expect(requestService.send).toHaveBeenCalledWith(jasmine.any(GetRequest), true);
}));
});
describe('when calling findBitstreamAccessStatusFor', () => {
let contentSource$;
beforeEach(() => {
contentSource$ = service.findBitstreamAccessStatusFor(mockBitstream);
});
it('should send a new GetRequest', fakeAsync(() => {

View File

@@ -4,6 +4,7 @@ import { AccessStatusObject } from 'src/app/shared/object-collection/shared/badg
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
import { ObjectCacheService } from '../cache/object-cache.service';
import { Bitstream } from '../shared/bitstream.model';
import { HALEndpointService } from '../shared/hal-endpoint.service';
import { Item } from '../shared/item.model';
import { BaseDataService } from './base/base-data.service';
@@ -29,7 +30,15 @@ export class AccessStatusDataService extends BaseDataService<AccessStatusObject>
* Returns {@link RemoteData} of {@link AccessStatusObject} that is the access status of the given item
* @param item Item we want the access status of
*/
findAccessStatusFor(item: Item): Observable<RemoteData<AccessStatusObject>> {
findItemAccessStatusFor(item: Item): Observable<RemoteData<AccessStatusObject>> {
return this.findByHref(item._links.accessStatus.href);
}
/**
* Returns {@link RemoteData} of {@link AccessStatusObject} that is the access status of the given bitstream
* @param bitstream Bitstream we want the access status of
*/
findBitstreamAccessStatusFor(bitstream: Bitstream): Observable<RemoteData<AccessStatusObject>> {
return this.findByHref(bitstream._links.accessStatus.href);
}
}

View File

@@ -176,7 +176,6 @@ export const models =
ResearcherProfile,
OrcidQueue,
OrcidHistory,
AccessStatusObject,
IdentifierData,
Subscription,
ItemRequest,

View File

@@ -4,6 +4,8 @@ import {
inheritSerialization,
} from 'cerialize';
import { Observable } from 'rxjs';
import { AccessStatusObject } from 'src/app/shared/object-collection/shared/badges/access-status-badge/access-status.model';
import { ACCESS_STATUS } from 'src/app/shared/object-collection/shared/badges/access-status-badge/access-status.resource-type';
import {
link,
@@ -52,6 +54,7 @@ export class Bitstream extends DSpaceObject implements ChildHALResource {
format: HALLink;
content: HALLink;
thumbnail: HALLink;
accessStatus: HALLink;
};
/**
@@ -75,6 +78,13 @@ export class Bitstream extends DSpaceObject implements ChildHALResource {
@link(BUNDLE)
bundle?: Observable<RemoteData<Bundle>>;
/**
* The access status for this Bitstream
* Will be undefined unless the access status {@link HALLink} has been resolved.
*/
@link(ACCESS_STATUS)
accessStatus?: Observable<RemoteData<AccessStatusObject>>;
getParentLinkKey(): keyof this['_links'] {
return 'format';
}

View File

@@ -8,6 +8,7 @@
}
<ng-container *ngTemplateOutlet="content"></ng-container>
</a>
<ds-embargo-badge [bitstream]="bitstream"></ds-embargo-badge>
<ng-template #content>
<ng-content></ng-content>

View File

@@ -8,11 +8,14 @@ import {
ActivatedRoute,
RouterLink,
} from '@angular/router';
import { Store } from '@ngrx/store';
import { TranslateModule } from '@ngx-translate/core';
import {
cold,
getTestScheduler,
} from 'jasmine-marbles';
import { of as observableOf } from 'rxjs';
import { APP_DATA_SERVICES_MAP } from 'src/config/app-config.interface';
import { getBitstreamModuleRoute } from '../../app-routing-paths';
import { AuthorizationDataService } from '../../core/data/feature-authorization/authorization-data.service';
@@ -34,6 +37,7 @@ describe('FileDownloadLinkComponent', () => {
let bitstream: Bitstream;
let item: Item;
let storeMock: any;
function init() {
authorizationService = jasmine.createSpyObj('authorizationService', {
@@ -51,6 +55,11 @@ describe('FileDownloadLinkComponent', () => {
self: { href: 'obj-selflink' },
},
});
storeMock = jasmine.createSpyObj('store', {
dispatch: jasmine.createSpy('dispatch'),
select: jasmine.createSpy('select'),
pipe: observableOf(true),
});
}
function initTestbed() {
@@ -63,6 +72,8 @@ describe('FileDownloadLinkComponent', () => {
RouterLinkDirectiveStub,
{ provide: AuthorizationDataService, useValue: authorizationService },
{ provide: ActivatedRoute, useValue: new ActivatedRouteStub() },
{ provide: Store, useValue: storeMock },
{ provide: APP_DATA_SERVICES_MAP, useValue: {} },
],
})
.overrideComponent(FileDownloadLinkComponent, {

View File

@@ -30,13 +30,14 @@ import {
hasValue,
isNotEmpty,
} from '../empty.util';
import { ThemedEmbargoBadgeComponent } from '../object-collection/shared/badges/embargo-badge/themed-embargo-badge.component';
@Component({
selector: 'ds-base-file-download-link',
templateUrl: './file-download-link.component.html',
styleUrls: ['./file-download-link.component.scss'],
standalone: true,
imports: [RouterLink, NgClass, NgTemplateOutlet, AsyncPipe, TranslateModule],
imports: [RouterLink, NgClass, NgTemplateOutlet, AsyncPipe, TranslateModule, ThemedEmbargoBadgeComponent],
})
/**
* Component displaying a download link

View File

@@ -51,7 +51,7 @@ describe('ItemAccessStatusBadgeComponent', () => {
});
accessStatusDataService = jasmine.createSpyObj('accessStatusDataService', {
findAccessStatusFor: createSuccessfulRemoteDataObject$(unknownStatus),
findItemAccessStatusFor: createSuccessfulRemoteDataObject$(unknownStatus),
});
item = Object.assign(new Item(), {
@@ -97,7 +97,7 @@ describe('ItemAccessStatusBadgeComponent', () => {
});
});
describe('When the findAccessStatusFor method returns unknown', () => {
describe('When the findItemAccessStatusFor method returns unknown', () => {
beforeEach(waitForAsync(() => {
init();
initTestBed();
@@ -110,10 +110,10 @@ describe('ItemAccessStatusBadgeComponent', () => {
});
});
describe('When the findAccessStatusFor method returns metadata.only', () => {
describe('When the findItemAccessStatusFor method returns metadata.only', () => {
beforeEach(waitForAsync(() => {
init();
(accessStatusDataService.findAccessStatusFor as jasmine.Spy).and.returnValue(createSuccessfulRemoteDataObject$(metadataOnlyStatus));
(accessStatusDataService.findItemAccessStatusFor as jasmine.Spy).and.returnValue(createSuccessfulRemoteDataObject$(metadataOnlyStatus));
initTestBed();
}));
beforeEach(() => {
@@ -124,10 +124,10 @@ describe('ItemAccessStatusBadgeComponent', () => {
});
});
describe('When the findAccessStatusFor method returns open.access', () => {
describe('When the findItemAccessStatusFor method returns open.access', () => {
beforeEach(waitForAsync(() => {
init();
(accessStatusDataService.findAccessStatusFor as jasmine.Spy).and.returnValue(createSuccessfulRemoteDataObject$(openAccessStatus));
(accessStatusDataService.findItemAccessStatusFor as jasmine.Spy).and.returnValue(createSuccessfulRemoteDataObject$(openAccessStatus));
initTestBed();
}));
beforeEach(() => {
@@ -138,10 +138,10 @@ describe('ItemAccessStatusBadgeComponent', () => {
});
});
describe('When the findAccessStatusFor method returns embargo', () => {
describe('When the findItemAccessStatusFor method returns embargo', () => {
beforeEach(waitForAsync(() => {
init();
(accessStatusDataService.findAccessStatusFor as jasmine.Spy).and.returnValue(createSuccessfulRemoteDataObject$(embargoStatus));
(accessStatusDataService.findItemAccessStatusFor as jasmine.Spy).and.returnValue(createSuccessfulRemoteDataObject$(embargoStatus));
initTestBed();
}));
beforeEach(() => {
@@ -152,10 +152,10 @@ describe('ItemAccessStatusBadgeComponent', () => {
});
});
describe('When the findAccessStatusFor method returns restricted', () => {
describe('When the findItemAccessStatusFor method returns restricted', () => {
beforeEach(waitForAsync(() => {
init();
(accessStatusDataService.findAccessStatusFor as jasmine.Spy).and.returnValue(createSuccessfulRemoteDataObject$(restrictedStatus));
(accessStatusDataService.findItemAccessStatusFor as jasmine.Spy).and.returnValue(createSuccessfulRemoteDataObject$(restrictedStatus));
initTestBed();
}));
beforeEach(() => {

View File

@@ -71,7 +71,7 @@ export class AccessStatusBadgeComponent implements OnDestroy, OnInit {
const item = this.object as Item;
if (item.accessStatus == null) {
// In case the access status has not been loaded, do it individually.
item.accessStatus = this.accessStatusDataService.findAccessStatusFor(item);
item.accessStatus = this.accessStatusDataService.findItemAccessStatusFor(item);
}
this.accessStatus$ = item.accessStatus.pipe(
map((accessStatusRD) => {

View File

@@ -27,6 +27,12 @@ export class AccessStatusObject implements CacheableObject {
@autoserialize
status: string;
/**
* The embargo date value
*/
@autoserialize
embargoDate: string;
/**
* The {@link HALLink}s for this AccessStatusObject
*/

View File

@@ -0,0 +1,5 @@
<ng-container *ngIf="showAccessStatus">
<div *ngIf="embargoDate$ | async as embargoDate">
<span [class]="'badge badge-secondary embargo-list-element-badge'">{{ 'embargo.listelement.badge' | translate: {date: embargoDate} }}</span>
</div>
</ng-container>

View File

@@ -0,0 +1,177 @@
import { NO_ERRORS_SCHEMA } from '@angular/core';
import {
ComponentFixture,
TestBed,
waitForAsync,
} from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { TranslateModule } from '@ngx-translate/core';
import { AccessStatusDataService } from 'src/app/core/data/access-status-data.service';
import { Bitstream } from 'src/app/core/shared/bitstream.model';
import { createSuccessfulRemoteDataObject$ } from 'src/app/shared/remote-data.utils';
import { environment } from 'src/environments/environment';
import { TruncatePipe } from '../../../../utils/truncate.pipe';
import { AccessStatusObject } from '../access-status-badge/access-status.model';
import { EmbargoBadgeComponent } from './embargo-badge.component';
describe('ItemEmbargoBadgeComponent', () => {
let component: EmbargoBadgeComponent;
let fixture: ComponentFixture<EmbargoBadgeComponent>;
let unknownStatus: AccessStatusObject;
let metadataOnlyStatus: AccessStatusObject;
let openAccessStatus: AccessStatusObject;
let embargoStatus: AccessStatusObject;
let restrictedStatus: AccessStatusObject;
let accessStatusDataService: AccessStatusDataService;
let bitstream: Bitstream;
function init() {
unknownStatus = Object.assign(new AccessStatusObject(), {
status: 'unknown',
embargoDate: null,
});
metadataOnlyStatus = Object.assign(new AccessStatusObject(), {
status: 'metadata.only',
embargoDate: null,
});
openAccessStatus = Object.assign(new AccessStatusObject(), {
status: 'open.access',
embargoDate: null,
});
embargoStatus = Object.assign(new AccessStatusObject(), {
status: 'embargo',
embargoDate: '2050-01-01',
});
restrictedStatus = Object.assign(new AccessStatusObject(), {
status: 'restricted',
embargoDate: null,
});
accessStatusDataService = jasmine.createSpyObj('accessStatusDataService', {
findBitstreamAccessStatusFor: createSuccessfulRemoteDataObject$(unknownStatus),
});
bitstream = Object.assign(new Bitstream(), {
uuid: 'bitstream-uuid',
});
}
function initTestBed() {
TestBed.configureTestingModule({
imports: [TranslateModule.forRoot(), EmbargoBadgeComponent, TruncatePipe],
schemas: [NO_ERRORS_SCHEMA],
providers: [
{ provide: AccessStatusDataService, useValue: accessStatusDataService },
],
}).compileComponents();
}
function initFixtureAndComponent() {
environment.item.bitstream.showAccessStatuses = true;
fixture = TestBed.createComponent(EmbargoBadgeComponent);
component = fixture.componentInstance;
component.bitstream = bitstream;
fixture.detectChanges();
environment.item.bitstream.showAccessStatuses = false;
}
function lookForNoEmbargoBadge() {
const badge = fixture.debugElement.query(By.css('span.badge'));
expect(badge).toBeNull();
}
function lookForEmbargoBadge() {
const badge = fixture.debugElement.query(By.css('span.badge'));
expect(badge).toBeDefined();
}
describe('init', () => {
beforeEach(waitForAsync(() => {
init();
initTestBed();
}));
beforeEach(() => {
initFixtureAndComponent();
});
it('should init the component', () => {
expect(component).toBeTruthy();
});
});
describe('When the findBitstreamAccessStatusFor method returns unknown', () => {
beforeEach(waitForAsync(() => {
init();
initTestBed();
}));
beforeEach(() => {
initFixtureAndComponent();
});
it('should not show the embargo badge', () => {
lookForNoEmbargoBadge();
});
});
describe('When the findBitstreamAccessStatusFor method returns metadata.only', () => {
beforeEach(waitForAsync(() => {
init();
(accessStatusDataService.findBitstreamAccessStatusFor as jasmine.Spy).and.returnValue(createSuccessfulRemoteDataObject$(metadataOnlyStatus));
initTestBed();
}));
beforeEach(() => {
initFixtureAndComponent();
});
it('should not show the embargo badge', () => {
lookForNoEmbargoBadge();
});
});
describe('When the findBitstreamAccessStatusFor method returns open.access', () => {
beforeEach(waitForAsync(() => {
init();
(accessStatusDataService.findBitstreamAccessStatusFor as jasmine.Spy).and.returnValue(createSuccessfulRemoteDataObject$(openAccessStatus));
initTestBed();
}));
beforeEach(() => {
initFixtureAndComponent();
});
it('should not show the embargo badge', () => {
lookForNoEmbargoBadge();
});
});
describe('When the findBitstreamAccessStatusFor method returns embargo', () => {
beforeEach(waitForAsync(() => {
init();
(accessStatusDataService.findBitstreamAccessStatusFor as jasmine.Spy).and.returnValue(createSuccessfulRemoteDataObject$(embargoStatus));
initTestBed();
}));
beforeEach(() => {
initFixtureAndComponent();
});
it('should show the embargo badge', () => {
lookForEmbargoBadge();
});
});
describe('When the findBitstreamAccessStatusFor method returns restricted', () => {
beforeEach(waitForAsync(() => {
init();
(accessStatusDataService.findBitstreamAccessStatusFor as jasmine.Spy).and.returnValue(createSuccessfulRemoteDataObject$(restrictedStatus));
initTestBed();
}));
beforeEach(() => {
initFixtureAndComponent();
});
it('should not show the embargo badge', () => {
lookForNoEmbargoBadge();
});
});
});

View File

@@ -0,0 +1,70 @@
import {
AsyncPipe,
NgIf,
} from '@angular/common';
import {
Component,
Input,
OnInit,
} from '@angular/core';
import { TranslateModule } from '@ngx-translate/core';
import {
catchError,
map,
Observable,
of as observableOf,
} from 'rxjs';
import { AccessStatusDataService } from 'src/app/core/data/access-status-data.service';
import { Bitstream } from 'src/app/core/shared/bitstream.model';
import { hasValue } from 'src/app/shared/empty.util';
import { environment } from 'src/environments/environment';
import { AccessStatusObject } from '../access-status-badge/access-status.model';
@Component({
selector: 'ds-base-embargo-badge',
templateUrl: './embargo-badge.component.html',
styleUrls: ['./embargo-badge.component.scss'],
standalone: true,
imports: [NgIf, AsyncPipe, TranslateModule],
})
/**
* Component rendering the embargo date of a bitstream as a badge
*/
export class EmbargoBadgeComponent implements OnInit {
@Input() bitstream: Bitstream;
embargoDate$: Observable<string>;
/**
* Whether to show the badge or not
*/
showAccessStatus: boolean;
/**
* Initialize instance variables
*
* @param {AccessStatusDataService} accessStatusDataService
*/
constructor(private accessStatusDataService: AccessStatusDataService) { }
ngOnInit(): void {
this.showAccessStatus = environment.item.bitstream.showAccessStatuses;
if (!this.showAccessStatus || this.bitstream == null) {
// Do not show the badge if the feature is inactive or if the bitstream is null.
return;
}
this.embargoDate$ = this.accessStatusDataService.findBitstreamAccessStatusFor(this.bitstream).pipe(
map((accessStatusRD) => {
if (accessStatusRD.statusCode !== 401 && hasValue(accessStatusRD.payload)) {
return accessStatusRD.payload;
} else {
return null;
}
}),
map((accessStatus: AccessStatusObject) => hasValue(accessStatus) ? accessStatus.embargoDate : null),
catchError(() => observableOf(null)),
);
}
}

View File

@@ -0,0 +1,36 @@
import {
Component,
Input,
} from '@angular/core';
import { Bitstream } from 'src/app/core/shared/bitstream.model';
import { ThemedComponent } from '../../../../theme-support/themed.component';
import { EmbargoBadgeComponent } from './embargo-badge.component';
/**
* Themed wrapper for EmbargoBadgeComponent
*/
@Component({
selector: 'ds-embargo-badge',
styleUrls: [],
templateUrl: '../../../../theme-support/themed.component.html',
standalone: true,
imports: [EmbargoBadgeComponent],
})
export class ThemedEmbargoBadgeComponent extends ThemedComponent<EmbargoBadgeComponent> {
@Input() bitstream: Bitstream;
protected inAndOutputNames: (keyof EmbargoBadgeComponent & keyof this)[] = ['bitstream'];
protected getComponentName(): string {
return 'EmbargoBadgeComponent';
}
protected importThemedComponent(themeName: string): Promise<any> {
return import(`../../../../../../themes/${themeName}/app/shared/object-collection/shared/badges/embargo-badge/embargo-badge.component`);
}
protected importUnthemedComponent(): Promise<any> {
return import(`./embargo-badge.component`);
}
}

View File

@@ -288,6 +288,7 @@ describe('ThumbnailComponent', () => {
format: { href: 'format.url' },
content: { href: CONTENT },
thumbnail: undefined,
accessStatus: { href: 'accessStatus.url' },
};
comp.thumbnail = thumbnail;
});
@@ -324,6 +325,7 @@ describe('ThumbnailComponent', () => {
format: { href: 'format.url' },
content: { href: CONTENT },
thumbnail: undefined,
accessStatus: { href: 'accessStatus.url' },
};
});

View File

@@ -6869,4 +6869,6 @@
"metadata-export-filtered-items.submit.error": "CSV export failed.",
"metadata-export-filtered-items.columns.warning": "CSV export automatically includes all relevant fields, so selections in this list are not taken into account.",
"embargo.listelement.badge": "embargo until {{ date }}",
}

View File

@@ -8595,4 +8595,7 @@
//"metadata-export-filtered-items.columns.warning": "CSV export automatically includes all relevant fields, so selections in this list are not taken into account.",
"metadata-export-filtered-items.columns.warning": "L'exportation CSV inclut automatiquement tous les champs pertinents, sans égard au contenu sélectionné de cette liste.",
// "embargo.listelement.badge": "embargo until {{ date }}",
"embargo.listelement.badge": "embargo jusqu'à {{ date }}",
}

View File

@@ -329,6 +329,8 @@ export class DefaultAppConfig implements AppConfig {
// Rounded to the nearest size in the list of selectable sizes on the
// settings menu. See pageSizeOptions in 'pagination-component-options.model.ts'.
pageSize: 5,
// Show the bitstream access status label
showAccessStatuses: false,
},
};

View File

@@ -12,5 +12,7 @@ export interface ItemConfig extends Config {
// Rounded to the nearest size in the list of selectable sizes on the
// settings menu. See pageSizeOptions in 'pagination-component-options.model.ts'.
pageSize: number;
// Show the bitstream access status label
showAccessStatuses: boolean;
}
}

View File

@@ -269,6 +269,8 @@ export const environment: BuildConfig = {
// Rounded to the nearest size in the list of selectable sizes on the
// settings menu. See pageSizeOptions in 'pagination-component-options.model.ts'.
pageSize: 5,
// Show the bitstream access status label
showAccessStatuses: false,
},
},
community: {

View File

@@ -8,6 +8,7 @@ import { RouterLink } from '@angular/router';
import { TranslateModule } from '@ngx-translate/core';
import { FileDownloadLinkComponent as BaseComponent } from '../../../../../app/shared/file-download-link/file-download-link.component';
import { ThemedEmbargoBadgeComponent } from '../../../../../app/shared/object-collection/shared/badges/embargo-badge/themed-embargo-badge.component';
@Component({
selector: 'ds-themed-file-download-link',
@@ -16,7 +17,7 @@ import { FileDownloadLinkComponent as BaseComponent } from '../../../../../app/s
// styleUrls: ['./file-download-link.component.scss'],
styleUrls: ['../../../../../app/shared/file-download-link/file-download-link.component.scss'],
standalone: true,
imports: [RouterLink, NgClass, NgTemplateOutlet, AsyncPipe, TranslateModule],
imports: [RouterLink, NgClass, NgTemplateOutlet, AsyncPipe, TranslateModule, ThemedEmbargoBadgeComponent],
})
export class FileDownloadLinkComponent extends BaseComponent {
}