Merge branch 'master' into Item-Collection_Mapper

Conflicts:
	resources/i18n/en.json
	src/app/+item-page/edit-item-page/edit-item-page.component.ts
	src/app/+item-page/edit-item-page/edit-item-page.module.ts
	src/app/+item-page/edit-item-page/edit-item-page.routing.module.ts
	src/app/+item-page/edit-item-page/item-status/item-status.component.html
	src/app/+item-page/edit-item-page/item-status/item-status.component.spec.ts
	src/app/+item-page/edit-item-page/item-status/item-status.component.ts
	src/app/+item-page/item-page-routing.module.ts
	src/app/core/data/item-data.service.spec.ts
	src/app/core/data/item-data.service.ts
	src/app/core/shared/operators.spec.ts
	src/app/core/shared/operators.ts
This commit is contained in:
Kristof De Langhe
2019-01-17 15:57:25 +01:00
35 changed files with 1591 additions and 44 deletions

View File

@@ -116,6 +116,10 @@
"label": "Withdraw item from the repository",
"button": "Withdraw..."
},
"reinstate": {
"label": "Reinstate item into the repository",
"button": "Reinstate..."
},
"move": {
"label": "Move item to another collection",
"button": "Move..."
@@ -124,6 +128,10 @@
"label": "Make item private",
"button": "Make it private..."
},
"public": {
"label": "Make item public",
"button": "Make it public..."
},
"delete": {
"label": "Completely expunge item",
"button": "Permanently delete"
@@ -147,6 +155,51 @@
"head": "Curate"
}
},
"modify.overview": {
"field": "Field",
"value": "Value",
"language": "Language"
},
"withdraw": {
"header": "Withdraw item: {{ id }}",
"description": "Are you sure this item should be withdrawn from the archive?",
"confirm": "Withdraw",
"cancel": "Cancel",
"success": "The item was withdrawn successfully",
"error": "An error occured while withdrawing the item"
},
"reinstate": {
"header": "Reinstate item: {{ id }}",
"description": "Are you sure this item should be reinstated to the archive?",
"confirm": "Reinstate",
"cancel": "Cancel",
"success": "The item was reinstated successfully",
"error": "An error occured while reinstating the item"
},
"private": {
"header": "Make item private: {{ id }}",
"description": "Are you sure this item should be made private in the archive?",
"confirm": "Make it Private",
"cancel": "Cancel",
"success": "The item is now private",
"error": "An error occured while making the item private"
},
"public": {
"header": "Make item public: {{ id }}",
"description": "Are you sure this item should be made public in the archive?",
"confirm": "Make it Public",
"cancel": "Cancel",
"success": "The item is now public",
"error": "An error occured while making the item public"
},
"delete": {
"header": "Delete item: {{ id }}",
"description": "Are you sure this item should be completely deleted? Caution: At present, no tombstone would be left.",
"confirm": "Delete",
"cancel": "Cancel",
"success": "The item has been deleted",
"error": "An error occured while deleting the item"
},
"item-mapper": {
"head": "Item Mapper - Map Item to Collections",
"item": "Item: \"<b>{{name}}</b>\"",

View File

@@ -0,0 +1,35 @@
import {RemoteData} from '../../core/data/remote-data';
import {hot} from 'jasmine-marbles';
import {Item} from '../../core/shared/item.model';
import {findSuccessfulAccordingTo} from './edit-item-operators';
describe('findSuccessfulAccordingTo', () => {
let mockItem1;
let mockItem2;
let predicate;
beforeEach(() => {
mockItem1 = new Item();
mockItem1.isWithdrawn = true;
mockItem2 = new Item();
mockItem1.isWithdrawn = false;
predicate = (rd: RemoteData<Item>) => rd.payload.isWithdrawn;
});
it('should return first successful RemoteData Observable that complies to predicate', () => {
const testRD = {
a: new RemoteData(false, false, true, null, undefined),
b: new RemoteData(false, false, false, null, mockItem1),
c: new RemoteData(false, false, true, null, mockItem2),
d: new RemoteData(false, false, true, null, mockItem1),
e: new RemoteData(false, false, true, null, mockItem2),
};
const source = hot('abcde', testRD);
const result = source.pipe(findSuccessfulAccordingTo(predicate));
result.subscribe((value) => expect(value).toEqual(testRD.d));
});
});

View File

@@ -0,0 +1,13 @@
import {RemoteData} from '../../core/data/remote-data';
import {Observable} from 'rxjs';
import {first} from 'rxjs/operators';
import {getAllSucceededRemoteData} from '../../core/shared/operators';
/**
* Return first Observable of a RemoteData object that complies to the provided predicate
* @param predicate
*/
export const findSuccessfulAccordingTo = <T>(predicate: (rd: RemoteData<T>) => boolean) =>
(source: Observable<RemoteData<T>>): Observable<RemoteData<T>> =>
source.pipe(getAllSucceededRemoteData(),
first(predicate));

View File

@@ -6,7 +6,18 @@ import { SearchPageModule } from '../../+search-page/search-page.module';
import { EditItemPageComponent } from './edit-item-page.component';
import { ItemCollectionMapperComponent } from './item-collection-mapper/item-collection-mapper.component';
import { ItemStatusComponent } from './item-status/item-status.component';
import { ItemOperationComponent } from './item-operation/item-operation.component';
import { AbstractSimpleItemActionComponent } from './simple-item-action/abstract-simple-item-action.component';
import { ModifyItemOverviewComponent } from './modify-item-overview/modify-item-overview.component';
import { ItemWithdrawComponent } from './item-withdraw/item-withdraw.component';
import { ItemReinstateComponent } from './item-reinstate/item-reinstate.component';
import { ItemPrivateComponent } from './item-private/item-private.component';
import { ItemPublicComponent } from './item-public/item-public.component';
import { ItemDeleteComponent } from './item-delete/item-delete.component';
/**
* Module that contains all components related to the Edit Item page administrator functionality
*/
@NgModule({
imports: [
CommonModule,
@@ -16,6 +27,14 @@ import { ItemStatusComponent } from './item-status/item-status.component';
],
declarations: [
EditItemPageComponent,
ItemOperationComponent,
AbstractSimpleItemActionComponent,
ModifyItemOverviewComponent,
ItemWithdrawComponent,
ItemReinstateComponent,
ItemPrivateComponent,
ItemPublicComponent,
ItemDeleteComponent,
ItemStatusComponent,
ItemCollectionMapperComponent
]

View File

@@ -3,7 +3,21 @@ import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { EditItemPageComponent } from './edit-item-page.component';
import { ItemCollectionMapperComponent } from './item-collection-mapper/item-collection-mapper.component';
import { ItemWithdrawComponent } from './item-withdraw/item-withdraw.component';
import { ItemReinstateComponent } from './item-reinstate/item-reinstate.component';
import { ItemPrivateComponent } from './item-private/item-private.component';
import { ItemPublicComponent } from './item-public/item-public.component';
import { ItemDeleteComponent } from './item-delete/item-delete.component';
const ITEM_EDIT_WITHDRAW_PATH = 'withdraw';
const ITEM_EDIT_REINSTATE_PATH = 'reinstate';
const ITEM_EDIT_PRIVATE_PATH = 'private';
const ITEM_EDIT_PUBLIC_PATH = 'public';
const ITEM_EDIT_DELETE_PATH = 'delete';
/**
* Routing module that handles the routing for the Edit Item page administrator functionality
*/
@NgModule({
imports: [
RouterModule.forChild([
@@ -20,8 +34,42 @@ import { ItemCollectionMapperComponent } from './item-collection-mapper/item-col
resolve: {
item: ItemPageResolver
}
}
])
},
{
path: ITEM_EDIT_WITHDRAW_PATH,
component: ItemWithdrawComponent,
resolve: {
item: ItemPageResolver
}
},
{
path: ITEM_EDIT_REINSTATE_PATH,
component: ItemReinstateComponent,
resolve: {
item: ItemPageResolver
}
},
{
path: ITEM_EDIT_PRIVATE_PATH,
component: ItemPrivateComponent,
resolve: {
item: ItemPageResolver
}
},
{
path: ITEM_EDIT_PUBLIC_PATH,
component: ItemPublicComponent,
resolve: {
item: ItemPageResolver
}
},
{
path: ITEM_EDIT_DELETE_PATH,
component: ItemDeleteComponent,
resolve: {
item: ItemPageResolver
}
}])
],
providers: [
ItemPageResolver,

View File

@@ -0,0 +1,118 @@
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {Item} from '../../../core/shared/item.model';
import {RouterStub} from '../../../shared/testing/router-stub';
import {of as observableOf} from 'rxjs';
import {RemoteData} from '../../../core/data/remote-data';
import {NotificationsServiceStub} from '../../../shared/testing/notifications-service-stub';
import {CommonModule} from '@angular/common';
import {FormsModule} from '@angular/forms';
import {RouterTestingModule} from '@angular/router/testing';
import {TranslateModule} from '@ngx-translate/core';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
import {ActivatedRoute, Router} from '@angular/router';
import {ItemDataService} from '../../../core/data/item-data.service';
import {NotificationsService} from '../../../shared/notifications/notifications.service';
import {CUSTOM_ELEMENTS_SCHEMA} from '@angular/core';
import {By} from '@angular/platform-browser';
import {ItemDeleteComponent} from './item-delete.component';
import {getItemEditPath} from '../../item-page-routing.module';
import { RestResponse } from '../../../core/cache/response.models';
let comp: ItemDeleteComponent;
let fixture: ComponentFixture<ItemDeleteComponent>;
let mockItem;
let itemPageUrl;
let routerStub;
let mockItemDataService: ItemDataService;
let routeStub;
let notificationsServiceStub;
let successfulRestResponse;
let failRestResponse;
describe('ItemDeleteComponent', () => {
beforeEach(async(() => {
mockItem = Object.assign(new Item(), {
id: 'fake-id',
handle: 'fake/handle',
lastModified: '2018',
isWithdrawn: true
});
itemPageUrl = `fake-url/${mockItem.id}`;
routerStub = Object.assign(new RouterStub(), {
url: `${itemPageUrl}/edit`
});
mockItemDataService = jasmine.createSpyObj('mockItemDataService', {
delete: observableOf(new RestResponse(true, '200'))
});
routeStub = {
data: observableOf({
item: new RemoteData(false, false, true, null, {
id: 'fake-id'
})
})
};
notificationsServiceStub = new NotificationsServiceStub();
TestBed.configureTestingModule({
imports: [CommonModule, FormsModule, RouterTestingModule.withRoutes([]), TranslateModule.forRoot(), NgbModule.forRoot()],
declarations: [ItemDeleteComponent],
providers: [
{provide: ActivatedRoute, useValue: routeStub},
{provide: Router, useValue: routerStub},
{provide: ItemDataService, useValue: mockItemDataService},
{provide: NotificationsService, useValue: notificationsServiceStub},
], schemas: [
CUSTOM_ELEMENTS_SCHEMA
]
}).compileComponents();
}));
beforeEach(() => {
successfulRestResponse = new RestResponse(true, '200');
failRestResponse = new RestResponse(false, '500');
fixture = TestBed.createComponent(ItemDeleteComponent);
comp = fixture.componentInstance;
fixture.detectChanges();
});
it('should render a page with messages based on the \'delete\' messageKey', () => {
const header = fixture.debugElement.query(By.css('h2')).nativeElement;
expect(header.innerHTML).toContain('item.edit.delete.header');
const description = fixture.debugElement.query(By.css('p')).nativeElement;
expect(description.innerHTML).toContain('item.edit.delete.description');
const confirmButton = fixture.debugElement.query(By.css('button.perform-action')).nativeElement;
expect(confirmButton.innerHTML).toContain('item.edit.delete.confirm');
const cancelButton = fixture.debugElement.query(By.css('button.cancel')).nativeElement;
expect(cancelButton.innerHTML).toContain('item.edit.delete.cancel');
});
describe('performAction', () => {
it('should call delete function from the ItemDataService', () => {
spyOn(comp, 'processRestResponse');
comp.performAction();
expect(mockItemDataService.delete).toHaveBeenCalledWith(mockItem.id);
expect(comp.processRestResponse).toHaveBeenCalled();
});
});
describe('processRestResponse', () => {
it('should navigate to the homepage on successful deletion of the item', () => {
comp.processRestResponse(successfulRestResponse);
expect(routerStub.navigate).toHaveBeenCalledWith(['']);
});
});
describe('processRestResponse', () => {
it('should navigate to the item edit page on failed deletion of the item', () => {
comp.processRestResponse(failRestResponse);
expect(routerStub.navigate).toHaveBeenCalledWith([getItemEditPath('fake-id')]);
});
});
})
;

View File

@@ -0,0 +1,43 @@
import {Component} from '@angular/core';
import {first} from 'rxjs/operators';
import {AbstractSimpleItemActionComponent} from '../simple-item-action/abstract-simple-item-action.component';
import {getItemEditPath} from '../../item-page-routing.module';
import { RestResponse } from '../../../core/cache/response.models';
@Component({
selector: 'ds-item-delete',
templateUrl: '../simple-item-action/abstract-simple-item-action.component.html'
})
/**
* Component responsible for rendering the item delete page
*/
export class ItemDeleteComponent extends AbstractSimpleItemActionComponent {
protected messageKey = 'delete';
/**
* Perform the delete action to the item
*/
performAction() {
this.itemDataService.delete(this.item.id).pipe(first()).subscribe(
(response: RestResponse) => {
this.processRestResponse(response);
}
);
}
/**
* Process the RestResponse retrieved from the server.
* When the item is successfully delete, navigate to the homepage, otherwise navigate back to the item edit page
* @param response
*/
processRestResponse(response: RestResponse) {
if (response.isSuccessful) {
this.notificationsService.success(this.translateService.get('item.edit.' + this.messageKey + '.success'));
this.router.navigate(['']);
} else {
this.notificationsService.error(this.translateService.get('item.edit.' + this.messageKey + '.error'));
this.router.navigate([getItemEditPath(this.item.id)]);
}
}
}

View File

@@ -0,0 +1,15 @@
<div class="col-3 float-left d-flex h-100 action-label">
<span class="justify-content-center align-self-center">
{{'item.edit.tabs.status.buttons.' + operation.operationKey + '.label' | translate}}
</span>
</div>
<div *ngIf="!operation.disabled" class="col-9 float-left action-button">
<a class="btn btn-outline-secondary" href="{{operation.operationUrl}}">
{{'item.edit.tabs.status.buttons.' + operation.operationKey + '.button' | translate}}
</a>
</div>
<div *ngIf="operation.disabled" class="col-9 float-left action-button">
<span class="btn btn-danger">
{{'item.edit.tabs.status.buttons.' + operation.operationKey + '.button' | translate}}
</span>
</div>

View File

@@ -0,0 +1,45 @@
import {ItemOperation} from './itemOperation.model';
import {async, TestBed} from '@angular/core/testing';
import {ItemOperationComponent} from './item-operation.component';
import {TranslateModule} from '@ngx-translate/core';
import {By} from '@angular/platform-browser';
describe('ItemOperationComponent', () => {
let itemOperation: ItemOperation;
let fixture;
let comp;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [TranslateModule.forRoot()],
declarations: [ItemOperationComponent]
}).compileComponents();
}));
beforeEach(() => {
itemOperation = new ItemOperation('key1', 'url1');
fixture = TestBed.createComponent(ItemOperationComponent);
comp = fixture.componentInstance;
comp.operation = itemOperation;
fixture.detectChanges();
});
it('should render operation row', () => {
const span = fixture.debugElement.query(By.css('span')).nativeElement;
expect(span.textContent).toContain('item.edit.tabs.status.buttons.key1.label');
const link = fixture.debugElement.query(By.css('a')).nativeElement;
expect(link.href).toContain('url1');
expect(link.textContent).toContain('item.edit.tabs.status.buttons.key1.button');
});
it('should render disabled operation row', () => {
itemOperation.setDisabled(true);
fixture.detectChanges();
const span = fixture.debugElement.query(By.css('span')).nativeElement;
expect(span.textContent).toContain('item.edit.tabs.status.buttons.key1.label');
const span2 = fixture.debugElement.query(By.css('span.btn-danger')).nativeElement;
expect(span2.textContent).toContain('item.edit.tabs.status.buttons.key1.button');
});
});

View File

@@ -0,0 +1,15 @@
import {Component, Input} from '@angular/core';
import {ItemOperation} from './itemOperation.model';
@Component({
selector: 'ds-item-operation',
templateUrl: './item-operation.component.html'
})
/**
* Operation that can be performed on an item
*/
export class ItemOperationComponent {
@Input() operation: ItemOperation;
}

View File

@@ -0,0 +1,25 @@
/**
* Represents an item operation used on the edit item page with a key, an operation URL to which will be navigated
* when performing the action and an option to disable the operation.
*/
export class ItemOperation {
operationKey: string;
operationUrl: string;
disabled: boolean;
constructor(operationKey: string, operationUrl: string) {
this.operationKey = operationKey;
this.operationUrl = operationUrl;
this.setDisabled(false);
}
/**
* Set whether this operation should be disabled
* @param disabled
*/
setDisabled(disabled: boolean): void {
this.disabled = disabled;
}
}

View File

@@ -0,0 +1,105 @@
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {Item} from '../../../core/shared/item.model';
import {RouterStub} from '../../../shared/testing/router-stub';
import {of as observableOf} from 'rxjs';
import {RemoteData} from '../../../core/data/remote-data';
import {NotificationsServiceStub} from '../../../shared/testing/notifications-service-stub';
import {CommonModule} from '@angular/common';
import {FormsModule} from '@angular/forms';
import {RouterTestingModule} from '@angular/router/testing';
import {TranslateModule} from '@ngx-translate/core';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
import {ActivatedRoute, Router} from '@angular/router';
import {ItemDataService} from '../../../core/data/item-data.service';
import {NotificationsService} from '../../../shared/notifications/notifications.service';
import {CUSTOM_ELEMENTS_SCHEMA} from '@angular/core';
import {By} from '@angular/platform-browser';
import {ItemPrivateComponent} from './item-private.component';
import { RestResponse } from '../../../core/cache/response.models';
let comp: ItemPrivateComponent;
let fixture: ComponentFixture<ItemPrivateComponent>;
let mockItem;
let itemPageUrl;
let routerStub;
let mockItemDataService: ItemDataService;
let routeStub;
let notificationsServiceStub;
let successfulRestResponse;
let failRestResponse;
describe('ItemPrivateComponent', () => {
beforeEach(async(() => {
mockItem = Object.assign(new Item(), {
id: 'fake-id',
handle: 'fake/handle',
lastModified: '2018',
isWithdrawn: true
});
itemPageUrl = `fake-url/${mockItem.id}`;
routerStub = Object.assign(new RouterStub(), {
url: `${itemPageUrl}/edit`
});
mockItemDataService = jasmine.createSpyObj('mockItemDataService',{
setDiscoverable: observableOf(new RestResponse(true, '200'))
});
routeStub = {
data: observableOf({
item: new RemoteData(false, false, true, null, {
id: 'fake-id'
})
})
};
notificationsServiceStub = new NotificationsServiceStub();
TestBed.configureTestingModule({
imports: [CommonModule, FormsModule, RouterTestingModule.withRoutes([]), TranslateModule.forRoot(), NgbModule.forRoot()],
declarations: [ItemPrivateComponent],
providers: [
{provide: ActivatedRoute, useValue: routeStub},
{provide: Router, useValue: routerStub},
{provide: ItemDataService, useValue: mockItemDataService},
{provide: NotificationsService, useValue: notificationsServiceStub},
], schemas: [
CUSTOM_ELEMENTS_SCHEMA
]
}).compileComponents();
}));
beforeEach(() => {
successfulRestResponse = new RestResponse(true, '200');
failRestResponse = new RestResponse(false, '500');
fixture = TestBed.createComponent(ItemPrivateComponent);
comp = fixture.componentInstance;
fixture.detectChanges();
});
it('should render a page with messages based on the \'private\' messageKey', () => {
const header = fixture.debugElement.query(By.css('h2')).nativeElement;
expect(header.innerHTML).toContain('item.edit.private.header');
const description = fixture.debugElement.query(By.css('p')).nativeElement;
expect(description.innerHTML).toContain('item.edit.private.description');
const confirmButton = fixture.debugElement.query(By.css('button.perform-action')).nativeElement;
expect(confirmButton.innerHTML).toContain('item.edit.private.confirm');
const cancelButton = fixture.debugElement.query(By.css('button.cancel')).nativeElement;
expect(cancelButton.innerHTML).toContain('item.edit.private.cancel');
});
describe('performAction', () => {
it('should call setDiscoverable function from the ItemDataService', () => {
spyOn(comp, 'processRestResponse');
comp.performAction();
expect(mockItemDataService.setDiscoverable).toHaveBeenCalledWith(mockItem.id, false);
expect(comp.processRestResponse).toHaveBeenCalled();
});
});
})
;

View File

@@ -0,0 +1,30 @@
import {Component} from '@angular/core';
import {first} from 'rxjs/operators';
import {AbstractSimpleItemActionComponent} from '../simple-item-action/abstract-simple-item-action.component';
import {RemoteData} from '../../../core/data/remote-data';
import {Item} from '../../../core/shared/item.model';
import { RestResponse } from '../../../core/cache/response.models';
@Component({
selector: 'ds-item-private',
templateUrl: '../simple-item-action/abstract-simple-item-action.component.html'
})
/**
* Component responsible for rendering the make item private page
*/
export class ItemPrivateComponent extends AbstractSimpleItemActionComponent {
protected messageKey = 'private';
protected predicate = (rd: RemoteData<Item>) => !rd.payload.isDiscoverable;
/**
* Perform the make private action to the item
*/
performAction() {
this.itemDataService.setDiscoverable(this.item.id, false).pipe(first()).subscribe(
(response: RestResponse) => {
this.processRestResponse(response);
}
);
}
}

View File

@@ -0,0 +1,105 @@
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {Item} from '../../../core/shared/item.model';
import {RouterStub} from '../../../shared/testing/router-stub';
import {of as observableOf} from 'rxjs';
import {RemoteData} from '../../../core/data/remote-data';
import {NotificationsServiceStub} from '../../../shared/testing/notifications-service-stub';
import {CommonModule} from '@angular/common';
import {FormsModule} from '@angular/forms';
import {RouterTestingModule} from '@angular/router/testing';
import {TranslateModule} from '@ngx-translate/core';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
import {ActivatedRoute, Router} from '@angular/router';
import {ItemDataService} from '../../../core/data/item-data.service';
import {NotificationsService} from '../../../shared/notifications/notifications.service';
import {CUSTOM_ELEMENTS_SCHEMA} from '@angular/core';
import {By} from '@angular/platform-browser';
import {ItemPublicComponent} from './item-public.component';
import { RestResponse } from '../../../core/cache/response.models';
let comp: ItemPublicComponent;
let fixture: ComponentFixture<ItemPublicComponent>;
let mockItem;
let itemPageUrl;
let routerStub;
let mockItemDataService: ItemDataService;
let routeStub;
let notificationsServiceStub;
let successfulRestResponse;
let failRestResponse;
describe('ItemPublicComponent', () => {
beforeEach(async(() => {
mockItem = Object.assign(new Item(), {
id: 'fake-id',
handle: 'fake/handle',
lastModified: '2018',
isWithdrawn: true
});
itemPageUrl = `fake-url/${mockItem.id}`;
routerStub = Object.assign(new RouterStub(), {
url: `${itemPageUrl}/edit`
});
mockItemDataService = jasmine.createSpyObj('mockItemDataService',{
setDiscoverable: observableOf(new RestResponse(true, '200'))
});
routeStub = {
data: observableOf({
item: new RemoteData(false, false, true, null, {
id: 'fake-id'
})
})
};
notificationsServiceStub = new NotificationsServiceStub();
TestBed.configureTestingModule({
imports: [CommonModule, FormsModule, RouterTestingModule.withRoutes([]), TranslateModule.forRoot(), NgbModule.forRoot()],
declarations: [ItemPublicComponent],
providers: [
{provide: ActivatedRoute, useValue: routeStub},
{provide: Router, useValue: routerStub},
{provide: ItemDataService, useValue: mockItemDataService},
{provide: NotificationsService, useValue: notificationsServiceStub},
], schemas: [
CUSTOM_ELEMENTS_SCHEMA
]
}).compileComponents();
}));
beforeEach(() => {
successfulRestResponse = new RestResponse(true, '200');
failRestResponse = new RestResponse(false, '500');
fixture = TestBed.createComponent(ItemPublicComponent);
comp = fixture.componentInstance;
fixture.detectChanges();
});
it('should render a page with messages based on the \'public\' messageKey', () => {
const header = fixture.debugElement.query(By.css('h2')).nativeElement;
expect(header.innerHTML).toContain('item.edit.public.header');
const description = fixture.debugElement.query(By.css('p')).nativeElement;
expect(description.innerHTML).toContain('item.edit.public.description');
const confirmButton = fixture.debugElement.query(By.css('button.perform-action')).nativeElement;
expect(confirmButton.innerHTML).toContain('item.edit.public.confirm');
const cancelButton = fixture.debugElement.query(By.css('button.cancel')).nativeElement;
expect(cancelButton.innerHTML).toContain('item.edit.public.cancel');
});
describe('performAction', () => {
it('should call setDiscoverable function from the ItemDataService', () => {
spyOn(comp, 'processRestResponse');
comp.performAction();
expect(mockItemDataService.setDiscoverable).toHaveBeenCalledWith(mockItem.id, true);
expect(comp.processRestResponse).toHaveBeenCalled();
});
});
})
;

View File

@@ -0,0 +1,30 @@
import {Component} from '@angular/core';
import {first} from 'rxjs/operators';
import {AbstractSimpleItemActionComponent} from '../simple-item-action/abstract-simple-item-action.component';
import {RemoteData} from '../../../core/data/remote-data';
import {Item} from '../../../core/shared/item.model';
import { RestResponse } from '../../../core/cache/response.models';
@Component({
selector: 'ds-item-public',
templateUrl: '../simple-item-action/abstract-simple-item-action.component.html'
})
/**
* Component responsible for rendering the make item public page
*/
export class ItemPublicComponent extends AbstractSimpleItemActionComponent {
protected messageKey = 'public';
protected predicate = (rd: RemoteData<Item>) => rd.payload.isDiscoverable;
/**
* Perform the make public action to the item
*/
performAction() {
this.itemDataService.setDiscoverable(this.item.id, true).pipe(first()).subscribe(
(response: RestResponse) => {
this.processRestResponse(response);
}
);
}
}

View File

@@ -0,0 +1,105 @@
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {Item} from '../../../core/shared/item.model';
import {RouterStub} from '../../../shared/testing/router-stub';
import {of as observableOf} from 'rxjs';
import {RemoteData} from '../../../core/data/remote-data';
import {NotificationsServiceStub} from '../../../shared/testing/notifications-service-stub';
import {CommonModule} from '@angular/common';
import {FormsModule} from '@angular/forms';
import {RouterTestingModule} from '@angular/router/testing';
import {TranslateModule} from '@ngx-translate/core';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
import {ActivatedRoute, Router} from '@angular/router';
import {ItemDataService} from '../../../core/data/item-data.service';
import {NotificationsService} from '../../../shared/notifications/notifications.service';
import {CUSTOM_ELEMENTS_SCHEMA} from '@angular/core';
import {By} from '@angular/platform-browser';
import {ItemReinstateComponent} from './item-reinstate.component';
import { RestResponse } from '../../../core/cache/response.models';
let comp: ItemReinstateComponent;
let fixture: ComponentFixture<ItemReinstateComponent>;
let mockItem;
let itemPageUrl;
let routerStub;
let mockItemDataService: ItemDataService;
let routeStub;
let notificationsServiceStub;
let successfulRestResponse;
let failRestResponse;
describe('ItemReinstateComponent', () => {
beforeEach(async(() => {
mockItem = Object.assign(new Item(), {
id: 'fake-id',
handle: 'fake/handle',
lastModified: '2018',
isWithdrawn: true
});
itemPageUrl = `fake-url/${mockItem.id}`;
routerStub = Object.assign(new RouterStub(), {
url: `${itemPageUrl}/edit`
});
mockItemDataService = jasmine.createSpyObj('mockItemDataService',{
setWithDrawn: observableOf(new RestResponse(true, '200'))
});
routeStub = {
data: observableOf({
item: new RemoteData(false, false, true, null, {
id: 'fake-id'
})
})
};
notificationsServiceStub = new NotificationsServiceStub();
TestBed.configureTestingModule({
imports: [CommonModule, FormsModule, RouterTestingModule.withRoutes([]), TranslateModule.forRoot(), NgbModule.forRoot()],
declarations: [ItemReinstateComponent],
providers: [
{provide: ActivatedRoute, useValue: routeStub},
{provide: Router, useValue: routerStub},
{provide: ItemDataService, useValue: mockItemDataService},
{provide: NotificationsService, useValue: notificationsServiceStub},
], schemas: [
CUSTOM_ELEMENTS_SCHEMA
]
}).compileComponents();
}));
beforeEach(() => {
successfulRestResponse = new RestResponse(true, '200');
failRestResponse = new RestResponse(false, '500');
fixture = TestBed.createComponent(ItemReinstateComponent);
comp = fixture.componentInstance;
fixture.detectChanges();
});
it('should render a page with messages based on the \'reinstate\' messageKey', () => {
const header = fixture.debugElement.query(By.css('h2')).nativeElement;
expect(header.innerHTML).toContain('item.edit.reinstate.header');
const description = fixture.debugElement.query(By.css('p')).nativeElement;
expect(description.innerHTML).toContain('item.edit.reinstate.description');
const confirmButton = fixture.debugElement.query(By.css('button.perform-action')).nativeElement;
expect(confirmButton.innerHTML).toContain('item.edit.reinstate.confirm');
const cancelButton = fixture.debugElement.query(By.css('button.cancel')).nativeElement;
expect(cancelButton.innerHTML).toContain('item.edit.reinstate.cancel');
});
describe('performAction', () => {
it('should call setWithdrawn function from the ItemDataService', () => {
spyOn(comp, 'processRestResponse');
comp.performAction();
expect(mockItemDataService.setWithDrawn).toHaveBeenCalledWith(mockItem.id, false);
expect(comp.processRestResponse).toHaveBeenCalled();
});
});
})
;

View File

@@ -0,0 +1,30 @@
import {Component} from '@angular/core';
import {first} from 'rxjs/operators';
import {AbstractSimpleItemActionComponent} from '../simple-item-action/abstract-simple-item-action.component';
import {RemoteData} from '../../../core/data/remote-data';
import {Item} from '../../../core/shared/item.model';
import { RestResponse } from '../../../core/cache/response.models';
@Component({
selector: 'ds-item-reinstate',
templateUrl: '../simple-item-action/abstract-simple-item-action.component.html'
})
/**
* Component responsible for rendering the Item Reinstate page
*/
export class ItemReinstateComponent extends AbstractSimpleItemActionComponent {
protected messageKey = 'reinstate';
protected predicate = (rd: RemoteData<Item>) => !rd.payload.isWithdrawn;
/**
* Perform the reinstate action to the item
*/
performAction() {
this.itemDataService.setWithDrawn(this.item.id, false).pipe(first()).subscribe(
(response: RestResponse) => {
this.processRestResponse(response);
}
);
}
}

View File

@@ -15,16 +15,7 @@
<a href="{{getItemPage()}}">{{getItemPage()}}</a>
</div>
<div *ngFor="let actionKey of actionsKeys" class="w-100 pt-3">
<div class="col-3 float-left d-flex h-100 action-label">
<span class="justify-content-center align-self-center">
{{'item.edit.tabs.status.buttons.' + actionKey + '.label' | translate}}
</span>
</div>
<div class="col-9 float-left action-button">
<a class="btn btn-outline-secondary" href="{{actions[actionKey]}}">
{{'item.edit.tabs.status.buttons.' + actionKey + '.button' | translate}}
</a>
</div>
<div *ngFor="let operation of operations" class="w-100 pt-3">
<ds-item-operation [operation]="operation"></ds-item-operation>
</div>
</div>

View File

@@ -10,6 +10,7 @@ import { Router } from '@angular/router';
import { RouterStub } from '../../../shared/testing/router-stub';
import { Item } from '../../../core/shared/item.model';
import { By } from '@angular/platform-browser';
import {CUSTOM_ELEMENTS_SCHEMA} from '@angular/core';
describe('ItemStatusComponent', () => {
let comp: ItemStatusComponent;
@@ -33,7 +34,7 @@ describe('ItemStatusComponent', () => {
providers: [
{ provide: Router, useValue: routerStub },
{ provide: HostWindowService, useValue: new HostWindowServiceStub(0) }
]
], schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
}));

View File

@@ -2,6 +2,7 @@ import { ChangeDetectionStrategy, Component, Input, OnInit } from '@angular/core
import { fadeIn, fadeInOut } from '../../../shared/animations/fade';
import { Item } from '../../../core/shared/item.model';
import { Router } from '@angular/router';
import { ItemOperation } from '../item-operation/itemOperation.model';
@Component({
selector: 'ds-item-status',
@@ -35,11 +36,7 @@ export class ItemStatusComponent implements OnInit {
* The possible actions that can be performed on the item
* key: id value: url to action's component
*/
actions: any;
/**
* The keys of the actions (to loop over)
*/
actionsKeys;
operations: ItemOperation[];
constructor(private router: Router) {
}
@@ -57,10 +54,19 @@ export class ItemStatusComponent implements OnInit {
i18n example: 'item.edit.tabs.status.buttons.<key>.label'
The value is supposed to be a href for the button
*/
this.actions = Object.assign({
mappedCollections: this.getCurrentUrl() + '/mapper'
});
this.actionsKeys = Object.keys(this.actions);
this.operations = [];
this.operations.push(new ItemOperation('mappedCollections', this.getCurrentUrl() + '/mapper'));
if (this.item.isWithdrawn) {
this.operations.push(new ItemOperation('reinstate', this.getCurrentUrl() + '/reinstate'));
} else {
this.operations.push(new ItemOperation('withdraw', this.getCurrentUrl() + '/withdraw'));
}
if (this.item.isDiscoverable) {
this.operations.push(new ItemOperation('private', this.getCurrentUrl() + '/private'));
} else {
this.operations.push(new ItemOperation('public', this.getCurrentUrl() + '/public'));
}
this.operations.push(new ItemOperation('delete', this.getCurrentUrl() + '/delete'));
}
/**

View File

@@ -0,0 +1,105 @@
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {Item} from '../../../core/shared/item.model';
import {RouterStub} from '../../../shared/testing/router-stub';
import {of as observableOf} from 'rxjs';
import {RemoteData} from '../../../core/data/remote-data';
import {NotificationsServiceStub} from '../../../shared/testing/notifications-service-stub';
import {CommonModule} from '@angular/common';
import {FormsModule} from '@angular/forms';
import {RouterTestingModule} from '@angular/router/testing';
import {TranslateModule} from '@ngx-translate/core';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
import {ActivatedRoute, Router} from '@angular/router';
import {ItemDataService} from '../../../core/data/item-data.service';
import {NotificationsService} from '../../../shared/notifications/notifications.service';
import {CUSTOM_ELEMENTS_SCHEMA} from '@angular/core';
import {ItemWithdrawComponent} from './item-withdraw.component';
import {By} from '@angular/platform-browser';
import { RestResponse } from '../../../core/cache/response.models';
let comp: ItemWithdrawComponent;
let fixture: ComponentFixture<ItemWithdrawComponent>;
let mockItem;
let itemPageUrl;
let routerStub;
let mockItemDataService: ItemDataService;
let routeStub;
let notificationsServiceStub;
let successfulRestResponse;
let failRestResponse;
describe('ItemWithdrawComponent', () => {
beforeEach(async(() => {
mockItem = Object.assign(new Item(), {
id: 'fake-id',
handle: 'fake/handle',
lastModified: '2018',
isWithdrawn: true
});
itemPageUrl = `fake-url/${mockItem.id}`;
routerStub = Object.assign(new RouterStub(), {
url: `${itemPageUrl}/edit`
});
mockItemDataService = jasmine.createSpyObj('mockItemDataService',{
setWithDrawn: observableOf(new RestResponse(true, '200'))
});
routeStub = {
data: observableOf({
item: new RemoteData(false, false, true, null, {
id: 'fake-id'
})
})
};
notificationsServiceStub = new NotificationsServiceStub();
TestBed.configureTestingModule({
imports: [CommonModule, FormsModule, RouterTestingModule.withRoutes([]), TranslateModule.forRoot(), NgbModule.forRoot(),],
declarations: [ItemWithdrawComponent],
providers: [
{provide: ActivatedRoute, useValue: routeStub},
{provide: Router, useValue: routerStub},
{provide: ItemDataService, useValue: mockItemDataService},
{provide: NotificationsService, useValue: notificationsServiceStub},
], schemas: [
CUSTOM_ELEMENTS_SCHEMA
]
}).compileComponents();
}));
beforeEach(() => {
successfulRestResponse = new RestResponse(true, '200');
failRestResponse = new RestResponse(false, '500');
fixture = TestBed.createComponent(ItemWithdrawComponent);
comp = fixture.componentInstance;
fixture.detectChanges();
});
it('should render a page with messages based on the \'withdraw\' messageKey', () => {
const header = fixture.debugElement.query(By.css('h2')).nativeElement;
expect(header.innerHTML).toContain('item.edit.withdraw.header');
const description = fixture.debugElement.query(By.css('p')).nativeElement;
expect(description.innerHTML).toContain('item.edit.withdraw.description');
const confirmButton = fixture.debugElement.query(By.css('button.perform-action')).nativeElement;
expect(confirmButton.innerHTML).toContain('item.edit.withdraw.confirm');
const cancelButton = fixture.debugElement.query(By.css('button.cancel')).nativeElement;
expect(cancelButton.innerHTML).toContain('item.edit.withdraw.cancel');
});
describe('performAction', () => {
it('should call setWithdrawn function from the ItemDataService', () => {
spyOn(comp, 'processRestResponse');
comp.performAction();
expect(mockItemDataService.setWithDrawn).toHaveBeenCalledWith(mockItem.id, true);
expect(comp.processRestResponse).toHaveBeenCalled();
});
});
})
;

View File

@@ -0,0 +1,30 @@
import {Component} from '@angular/core';
import {first} from 'rxjs/operators';
import {AbstractSimpleItemActionComponent} from '../simple-item-action/abstract-simple-item-action.component';
import {RemoteData} from '../../../core/data/remote-data';
import {Item} from '../../../core/shared/item.model';
import { RestResponse } from '../../../core/cache/response.models';
@Component({
selector: 'ds-item-withdraw',
templateUrl: '../simple-item-action/abstract-simple-item-action.component.html'
})
/**
* Component responsible for rendering the Item Withdraw page
*/
export class ItemWithdrawComponent extends AbstractSimpleItemActionComponent {
protected messageKey = 'withdraw';
protected predicate = (rd: RemoteData<Item>) => rd.payload.isWithdrawn;
/**
* Perform the withdraw action to the item
*/
performAction() {
this.itemDataService.setWithDrawn(this.item.id, true).pipe(first()).subscribe(
(response: RestResponse) => {
this.processRestResponse(response);
}
);
}
}

View File

@@ -0,0 +1,16 @@
<table id="metadata" class="table table-striped table-hover">
<thead>
<tr>
<th scope="col">{{'item.edit.modify.overview.field'| translate}}</th>
<th scope="col">{{'item.edit.modify.overview.value'| translate}}</th>
<th scope="col">{{'item.edit.modify.overview.language'| translate}}</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let metadatum of metadata" class="metadata-row">
<td>{{metadatum.key}}</td>
<td>{{metadatum.value}}</td>
<td>{{metadatum.language}}</td>
</tr>
</tbody>
</table>

View File

@@ -0,0 +1,55 @@
import {Item} from '../../../core/shared/item.model';
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {ModifyItemOverviewComponent} from './modify-item-overview.component';
import {By} from '@angular/platform-browser';
import {TranslateModule} from '@ngx-translate/core';
let comp: ModifyItemOverviewComponent;
let fixture: ComponentFixture<ModifyItemOverviewComponent>;
const mockItem = Object.assign(new Item(), {
id: 'fake-id',
handle: 'fake/handle',
lastModified: '2018',
metadata: [
{key: 'dc.title', value: 'Mock item title', language: 'en'},
{key: 'dc.contributor.author', value: 'Mayer, Ed', language: ''}
]
});
describe('ModifyItemOverviewComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [TranslateModule.forRoot()],
declarations: [ModifyItemOverviewComponent],
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ModifyItemOverviewComponent);
comp = fixture.componentInstance;
comp.item = mockItem;
fixture.detectChanges();
});
it('should render a table of existing metadata fields in the item', () => {
const metadataRows = fixture.debugElement.queryAll(By.css('tr.metadata-row'));
expect(metadataRows.length).toEqual(2);
const titleRow = metadataRows[0].queryAll(By.css('td'));
expect(titleRow.length).toEqual(3);
expect(titleRow[0].nativeElement.innerHTML).toContain('dc.title');
expect(titleRow[1].nativeElement.innerHTML).toContain('Mock item title');
expect(titleRow[2].nativeElement.innerHTML).toContain('en');
const authorRow = metadataRows[1].queryAll(By.css('td'));
expect(authorRow.length).toEqual(3);
expect(authorRow[0].nativeElement.innerHTML).toContain('dc.contributor.author');
expect(authorRow[1].nativeElement.innerHTML).toContain('Mayer, Ed');
expect(authorRow[2].nativeElement.innerHTML).toEqual('');
});
});

View File

@@ -0,0 +1,20 @@
import {Component, Input, OnInit} from '@angular/core';
import {Item} from '../../../core/shared/item.model';
import {Metadatum} from '../../../core/shared/metadatum.model';
@Component({
selector: 'ds-modify-item-overview',
templateUrl: './modify-item-overview.component.html'
})
/**
* Component responsible for rendering a table containing the metadatavalues from the to be edited item
*/
export class ModifyItemOverviewComponent implements OnInit {
@Input() item: Item;
metadata: Metadatum[];
ngOnInit(): void {
this.metadata = this.item.metadata;
}
}

View File

@@ -0,0 +1,16 @@
<div class="container">
<div class="row">
<div class="col-12">
<h2>{{headerMessage | translate: {id: item.handle} }}</h2>
<p>{{descriptionMessage | translate}}</p>
<ds-modify-item-overview [item]="item"></ds-modify-item-overview>
<button (click)="performAction()" class="btn btn-outline-secondary perform-action">{{confirmMessage | translate}}
</button>
<button [routerLink]="['/items/', item.id, 'edit']" class="btn btn-outline-secondary cancel">
{{cancelMessage| translate}}
</button>
</div>
</div>
</div>

View File

@@ -0,0 +1,142 @@
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {Item} from '../../../core/shared/item.model';
import {RouterStub} from '../../../shared/testing/router-stub';
import {CommonModule} from '@angular/common';
import {RouterTestingModule} from '@angular/router/testing';
import {TranslateModule} from '@ngx-translate/core';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
import {ActivatedRoute, Router} from '@angular/router';
import {NotificationsServiceStub} from '../../../shared/testing/notifications-service-stub';
import {NotificationsService} from '../../../shared/notifications/notifications.service';
import {Component, CUSTOM_ELEMENTS_SCHEMA} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {ItemDataService} from '../../../core/data/item-data.service';
import {RemoteData} from '../../../core/data/remote-data';
import {AbstractSimpleItemActionComponent} from './abstract-simple-item-action.component';
import {By} from '@angular/platform-browser';
import {of as observableOf} from 'rxjs';
import {getItemEditPath} from '../../item-page-routing.module';
import { RestResponse } from '../../../core/cache/response.models';
/**
* Test component that implements the AbstractSimpleItemActionComponent used to test the
* AbstractSimpleItemActionComponent component
*/
@Component({
selector: 'ds-simple-action',
templateUrl: './abstract-simple-item-action.component.html'
})
export class MySimpleItemActionComponent extends AbstractSimpleItemActionComponent {
protected messageKey = 'myEditAction';
protected predicate = (rd: RemoteData<Item>) => rd.payload.isWithdrawn;
performAction() {
// do nothing
}
}
let comp: MySimpleItemActionComponent;
let fixture: ComponentFixture<MySimpleItemActionComponent>;
let mockItem;
let itemPageUrl;
let routerStub;
let mockItemDataService;
let routeStub;
let notificationsServiceStub;
let successfulRestResponse;
let failRestResponse;
describe('AbstractSimpleItemActionComponent', () => {
beforeEach(async(() => {
mockItem = Object.assign(new Item(), {
id: 'fake-id',
handle: 'fake/handle',
lastModified: '2018',
isWithdrawn: true
});
itemPageUrl = `fake-url/${mockItem.id}`;
routerStub = Object.assign(new RouterStub(), {
url: `${itemPageUrl}/edit`
});
mockItemDataService = jasmine.createSpyObj({
findById: observableOf(new RemoteData(false, false, true, undefined, mockItem))
});
routeStub = {
data: observableOf({
item: new RemoteData(false, false, true, null, {
id: 'fake-id'
})
})
};
notificationsServiceStub = new NotificationsServiceStub();
TestBed.configureTestingModule({
imports: [CommonModule, FormsModule, RouterTestingModule.withRoutes([]), TranslateModule.forRoot(), NgbModule.forRoot()],
declarations: [MySimpleItemActionComponent],
providers: [
{provide: ActivatedRoute, useValue: routeStub},
{provide: Router, useValue: routerStub},
{provide: ItemDataService, useValue: mockItemDataService},
{provide: NotificationsService, useValue: notificationsServiceStub},
], schemas: [
CUSTOM_ELEMENTS_SCHEMA
]
}).compileComponents();
}));
beforeEach(() => {
successfulRestResponse = new RestResponse(true, '200');
failRestResponse = new RestResponse(false, '500');
fixture = TestBed.createComponent(MySimpleItemActionComponent);
comp = fixture.componentInstance;
fixture.detectChanges();
});
it('should render a page with messages based on the provided messageKey', () => {
const header = fixture.debugElement.query(By.css('h2')).nativeElement;
expect(header.innerHTML).toContain('item.edit.myEditAction.header');
const description = fixture.debugElement.query(By.css('p')).nativeElement;
expect(description.innerHTML).toContain('item.edit.myEditAction.description');
const confirmButton = fixture.debugElement.query(By.css('button.perform-action')).nativeElement;
expect(confirmButton.innerHTML).toContain('item.edit.myEditAction.confirm');
const cancelButton = fixture.debugElement.query(By.css('button.cancel')).nativeElement;
expect(cancelButton.innerHTML).toContain('item.edit.myEditAction.cancel');
});
it('should perform action when the button is clicked', () => {
spyOn(comp, 'performAction');
const performButton = fixture.debugElement.query(By.css('.perform-action'));
performButton.triggerEventHandler('click', null);
expect(comp.performAction).toHaveBeenCalled();
});
it('should process a RestResponse to navigate and display success notification', () => {
spyOn(notificationsServiceStub, 'success');
comp.processRestResponse(successfulRestResponse);
expect(notificationsServiceStub.success).toHaveBeenCalled();
expect(routerStub.navigate).toHaveBeenCalledWith([getItemEditPath(mockItem.id)]);
});
it('should process a RestResponse to navigate and display success notification', () => {
spyOn(notificationsServiceStub, 'error');
comp.processRestResponse(failRestResponse);
expect(notificationsServiceStub.error).toHaveBeenCalled();
expect(routerStub.navigate).toHaveBeenCalledWith([getItemEditPath(mockItem.id)]);
});
});

View File

@@ -0,0 +1,84 @@
import {Component, OnInit, Predicate} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {NotificationsService} from '../../../shared/notifications/notifications.service';
import {ItemDataService} from '../../../core/data/item-data.service';
import {TranslateService} from '@ngx-translate/core';
import {Item} from '../../../core/shared/item.model';
import {RemoteData} from '../../../core/data/remote-data';
import {Observable} from 'rxjs';
import {getSucceededRemoteData} from '../../../core/shared/operators';
import {first, map} from 'rxjs/operators';
import {findSuccessfulAccordingTo} from '../edit-item-operators';
import {getItemEditPath} from '../../item-page-routing.module';
import { RestResponse } from '../../../core/cache/response.models';
/**
* Component to render and handle simple item edit actions such as withdrawal and reinstatement.
* This component is not meant to be used itself but to be extended.
*/
@Component({
selector: 'ds-simple-action',
templateUrl: './abstract-simple-item-action.component.html'
})
export class AbstractSimpleItemActionComponent implements OnInit {
itemRD$: Observable<RemoteData<Item>>;
item: Item;
protected messageKey: string;
confirmMessage: string;
cancelMessage: string;
headerMessage: string;
descriptionMessage: string;
protected predicate: Predicate<RemoteData<Item>>;
constructor(protected route: ActivatedRoute,
protected router: Router,
protected notificationsService: NotificationsService,
protected itemDataService: ItemDataService,
protected translateService: TranslateService) {
}
ngOnInit(): void {
this.itemRD$ = this.route.data.pipe(
map((data) => data.item),
getSucceededRemoteData()
)as Observable<RemoteData<Item>>;
this.itemRD$.pipe(first()).subscribe((rd) => {
this.item = rd.payload;
}
);
this.confirmMessage = 'item.edit.' + this.messageKey + '.confirm';
this.cancelMessage = 'item.edit.' + this.messageKey + '.cancel';
this.headerMessage = 'item.edit.' + this.messageKey + '.header';
this.descriptionMessage = 'item.edit.' + this.messageKey + '.description';
}
/**
* Perform the operation linked to this action
*/
performAction() {
// Overwrite in subclasses
};
/**
* Process the response obtained during the performAction method and navigate back to the edit page
* @param response from the action in the performAction method
*/
processRestResponse(response: RestResponse) {
if (response.isSuccessful) {
this.itemDataService.findById(this.item.id).pipe(
findSuccessfulAccordingTo(this.predicate)).subscribe(() => {
this.notificationsService.success(this.translateService.get('item.edit.' + this.messageKey + '.success'));
this.router.navigate([getItemEditPath(this.item.id)]);
});
} else {
this.notificationsService.error(this.translateService.get('item.edit.' + this.messageKey + '.error'));
this.router.navigate([getItemEditPath(this.item.id)]);
}
}
}

View File

@@ -5,6 +5,17 @@ import { ItemPageComponent } from './simple/item-page.component';
import { FullItemPageComponent } from './full/full-item-page.component';
import { ItemPageResolver } from './item-page.resolver';
import { AuthenticatedGuard } from '../core/auth/authenticated.guard';
import { getItemModulePath } from '../app-routing.module';
import { URLCombiner } from '../core/url-combiner/url-combiner';
export function getItemPageRoute(itemId: string) {
return new URLCombiner(getItemModulePath(), itemId).toString();
}
export function getItemEditPath(id: string) {
return new URLCombiner(getItemModulePath(),ITEM_EDIT_PATH.replace(/:id/, id)).toString()
}
const ITEM_EDIT_PATH = ':id/edit';
@NgModule({
imports: [
@@ -25,7 +36,7 @@ import { AuthenticatedGuard } from '../core/auth/authenticated.guard';
}
},
{
path: ':id/edit',
path: ITEM_EDIT_PATH,
loadChildren: './edit-item-page/edit-item-page.module#EditItemPageModule',
canActivate: [AuthenticatedGuard]
}

View File

@@ -3,6 +3,10 @@ import { RouterModule } from '@angular/router';
import { PageNotFoundComponent } from './pagenotfound/pagenotfound.component';
const ITEM_MODULE_PATH = 'items';
export function getItemModulePath() {
return `/${ITEM_MODULE_PATH}`;
}
@NgModule({
imports: [
RouterModule.forRoot([
@@ -10,7 +14,7 @@ import { PageNotFoundComponent } from './pagenotfound/pagenotfound.component';
{ path: 'home', loadChildren: './+home-page/home-page.module#HomePageModule' },
{ path: 'communities', loadChildren: './+community-page/community-page.module#CommunityPageModule' },
{ path: 'collections', loadChildren: './+collection-page/collection-page.module#CollectionPageModule' },
{ path: 'items', loadChildren: './+item-page/item-page.module#ItemPageModule' },
{ path: ITEM_MODULE_PATH, loadChildren: './+item-page/item-page.module#ItemPageModule' },
{ path: 'search', loadChildren: './+search-page/search-page.module#SearchPageModule' },
{ path: 'browse', loadChildren: './+browse-by/browse-by.module#BrowseByModule' },
{ path: 'admin', loadChildren: './+admin/admin.module#AdminModule' },

View File

@@ -8,17 +8,36 @@ import { ItemDataService } from './item-data.service';
import { RequestService } from './request.service';
import { HALEndpointService } from '../shared/hal-endpoint.service';
import { ObjectCacheService } from '../cache/object-cache.service';
import { FindAllOptions } from './request.models';
import { FindAllOptions, RestRequest } from './request.models';
import { Observable, of as observableOf } from 'rxjs';
import { RestResponse } from '../cache/response.models';
import { RequestEntry } from './request.reducer';
describe('ItemDataService', () => {
let scheduler: TestScheduler;
let service: ItemDataService;
let bs: BrowseService;
const requestService = {} as RequestService;
const requestService = Object.assign({
generateRequestId(): string {
return scopeID;
},
configure(request: RestRequest) {
// Do nothing
},
getByUUID() {
const requestEntry = new RequestEntry();
requestEntry.response = new RestResponse(true, '200');
return observableOf(requestEntry);
}
}) as RequestService;
const rdbService = {} as RemoteDataBuildService;
const objectCache = {} as ObjectCacheService;
const store = {} as Store<CoreState>;
const halEndpointService = {} as HALEndpointService;
const halEndpointService = {
getEndpoint(linkPath: string): Observable<string> {
return cold('a', {a: itemEndpoint});
}
} as HALEndpointService;
const scopeID = '4af28e99-6a9c-4036-a199-e1b587046d39';
const options = Object.assign(new FindAllOptions(), {
@@ -34,10 +53,12 @@ describe('ItemDataService', () => {
const scopedEndpoint = `${itemBrowseEndpoint}?scope=${scopeID}`;
const serviceEndpoint = `https://rest.api/core/items`;
const browseError = new Error('getBrowseURL failed');
const itemEndpoint = 'https://rest.api/core/items';
const ScopedItemEndpoint = `https://rest.api/core/items/${scopeID}`;
function initMockBrowseService(isSuccessful: boolean) {
const obs = isSuccessful ?
cold('--a-', { a: itemBrowseEndpoint }) :
cold('--a-', {a: itemBrowseEndpoint}) :
cold('--#-', undefined, browseError);
return jasmine.createSpyObj('bs', {
getBrowseURLFor: obs
@@ -65,7 +86,7 @@ describe('ItemDataService', () => {
service = initTestService();
const result = service.getBrowseEndpoint(options);
const expected = cold('--b-', { b: scopedEndpoint });
const expected = cold('--b-', {b: scopedEndpoint});
expect(result).toBeObservable(expected);
});
@@ -83,4 +104,70 @@ describe('ItemDataService', () => {
});
});
});
describe('getItemWithdrawEndpoint', () => {
beforeEach(() => {
scheduler = getTestScheduler();
service = initTestService();
});
it('should return the endpoint to withdraw and reinstate items', () => {
const result = service.getItemWithdrawEndpoint(scopeID);
const expected = cold('a', {a: ScopedItemEndpoint});
expect(result).toBeObservable(expected);
});
it('should setWithDrawn', () => {
const expected = new RestResponse(true, '200');
const result = service.setWithDrawn(scopeID, true);
result.subscribe((v) => expect(v).toEqual(expected));
});
});
describe('getItemDiscoverableEndpoint', () => {
beforeEach(() => {
scheduler = getTestScheduler();
service = initTestService();
});
it('should return the endpoint to make an item private or public', () => {
const result = service.getItemDiscoverableEndpoint(scopeID);
const expected = cold('a', {a: ScopedItemEndpoint});
expect(result).toBeObservable(expected);
});
it('should setDiscoverable', () => {
const expected = new RestResponse(true, '200');
const result = service.setDiscoverable(scopeID, false);
result.subscribe((v) => expect(v).toEqual(expected));
});
});
describe('getItemDeleteEndpoint', () => {
beforeEach(() => {
scheduler = getTestScheduler();
service = initTestService();
});
it('should return the endpoint to make an item private or public', () => {
const result = service.getItemDeleteEndpoint(scopeID);
const expected = cold('a', {a: ScopedItemEndpoint});
expect(result).toBeObservable(expected);
});
it('should delete the item', () => {
const expected = new RestResponse(true, '200');
const result = service.delete(scopeID);
result.subscribe((v) => expect(v).toEqual(expected));
});
});
});

View File

@@ -14,7 +14,14 @@ import { URLCombiner } from '../url-combiner/url-combiner';
import { DataService } from './data.service';
import { RequestService } from './request.service';
import { HALEndpointService } from '../shared/hal-endpoint.service';
import { DeleteRequest, FindAllOptions, MappingCollectionsRequest, PostRequest, RestRequest } from './request.models';
import {
DeleteRequest,
FindAllOptions,
MappingCollectionsRequest,
PatchRequest,
PostRequest,
RestRequest
} from './request.models';
import { ObjectCacheService } from '../cache/object-cache.service';
import { GenericSuccessResponse, RestResponse } from '../cache/response.models';
import { configureRequest, filterSuccessfulResponses, getResponseFromEntry } from '../shared/operators';
@@ -132,4 +139,90 @@ export class ItemDataService extends DataService<NormalizedItem, Item> {
});
}
/**
* Get the endpoint for item withdrawal and reinstatement
* @param itemId
*/
public getItemWithdrawEndpoint(itemId: string): Observable<string> {
return this.halService.getEndpoint(this.linkPath).pipe(
map((endpoint: string) => this.getFindByIDHref(endpoint, itemId))
);
}
/**
* Get the endpoint to make item private and public
* @param itemId
*/
public getItemDiscoverableEndpoint(itemId: string): Observable<string> {
return this.halService.getEndpoint(this.linkPath).pipe(
map((endpoint: string) => this.getFindByIDHref(endpoint, itemId))
);
}
/**
* Get the endpoint to delete the item
* @param itemId
*/
public getItemDeleteEndpoint(itemId: string): Observable<string> {
return this.halService.getEndpoint(this.linkPath).pipe(
map((endpoint: string) => this.getFindByIDHref(endpoint, itemId))
);
}
/**
* Set the isWithdrawn state of an item to a specified state
* @param itemId
* @param withdrawn
*/
public setWithDrawn(itemId: string, withdrawn: boolean) {
const patchOperation = [{
op: 'replace', path: '/withdrawn', value: withdrawn
}];
return this.getItemWithdrawEndpoint(itemId).pipe(
distinctUntilChanged(),
map((endpointURL: string) =>
new PatchRequest(this.requestService.generateRequestId(), endpointURL, patchOperation)
),
configureRequest(this.requestService),
switchMap((request: RestRequest) => this.requestService.getByUUID(request.uuid)),
getResponseFromEntry()
);
}
/**
* Set the isDiscoverable state of an item to a specified state
* @param itemId
* @param discoverable
*/
public setDiscoverable(itemId: string, discoverable: boolean) {
const patchOperation = [{
op: 'replace', path: '/discoverable', value: discoverable
}];
return this.getItemDiscoverableEndpoint(itemId).pipe(
distinctUntilChanged(),
map((endpointURL: string) =>
new PatchRequest(this.requestService.generateRequestId(), endpointURL, patchOperation)
),
configureRequest(this.requestService),
switchMap((request: RestRequest) => this.requestService.getByUUID(request.uuid)),
getResponseFromEntry()
);
}
/**
* Delete the item
* @param itemId
*/
public delete(itemId: string) {
return this.getItemDeleteEndpoint(itemId).pipe(
distinctUntilChanged(),
map((endpointURL: string) =>
new DeleteRequest(this.requestService.generateRequestId(), endpointURL)
),
configureRequest(this.requestService),
switchMap((request: RestRequest) => this.requestService.getByUUID(request.uuid)),
getResponseFromEntry()
);
}
}

View File

@@ -6,10 +6,11 @@ import { RequestEntry } from '../data/request.reducer';
import { RequestService } from '../data/request.service';
import {
configureRequest,
filterSuccessfulResponses,
filterSuccessfulResponses, getAllSucceededRemoteData,
getRemoteDataPayload, getRequestFromRequestHref, getRequestFromRequestUUID,
getResourceLinksFromResponse, getResponseFromEntry,
getResourceLinksFromResponse, getResponseFromEntry, getSucceededRemoteData,
} from './operators';
import { RemoteData } from '../data/remote-data';
describe('Core Module - RxJS Operators', () => {
let scheduler: TestScheduler;
@@ -48,7 +49,7 @@ describe('Core Module - RxJS Operators', () => {
const result = source.pipe(getRequestFromRequestHref(requestService));
const expected = cold('a', { a: new RequestEntry() });
expect(result).toBeObservable(expected)
expect(result).toBeObservable(expected);
});
it('should use the requestService to fetch the request by its self link', () => {
@@ -58,7 +59,7 @@ describe('Core Module - RxJS Operators', () => {
scheduler.schedule(() => source.pipe(getRequestFromRequestHref(requestService)).subscribe());
scheduler.flush();
expect(requestService.getByHref).toHaveBeenCalledWith(testRequestHref)
expect(requestService.getByHref).toHaveBeenCalledWith(testRequestHref);
});
it('shouldn\'t return anything if there is no request matching the self link', () => {
@@ -68,7 +69,7 @@ describe('Core Module - RxJS Operators', () => {
const result = source.pipe(getRequestFromRequestHref(requestService));
const expected = cold('-');
expect(result).toBeObservable(expected)
expect(result).toBeObservable(expected);
});
});
@@ -81,7 +82,7 @@ describe('Core Module - RxJS Operators', () => {
const result = source.pipe(getRequestFromRequestUUID(requestService));
const expected = cold('a', { a: new RequestEntry() });
expect(result).toBeObservable(expected)
expect(result).toBeObservable(expected);
});
it('should use the requestService to fetch the request by its request uuid', () => {
@@ -91,7 +92,7 @@ describe('Core Module - RxJS Operators', () => {
scheduler.schedule(() => source.pipe(getRequestFromRequestUUID(requestService)).subscribe());
scheduler.flush();
expect(requestService.getByUUID).toHaveBeenCalledWith(testRequestUUID)
expect(requestService.getByUUID).toHaveBeenCalledWith(testRequestUUID);
});
it('shouldn\'t return anything if there is no request matching the request uuid', () => {
@@ -101,7 +102,7 @@ describe('Core Module - RxJS Operators', () => {
const result = source.pipe(getRequestFromRequestUUID(requestService));
const expected = cold('-');
expect(result).toBeObservable(expected)
expect(result).toBeObservable(expected);
});
});
@@ -111,7 +112,7 @@ describe('Core Module - RxJS Operators', () => {
const result = source.pipe(filterSuccessfulResponses());
const expected = cold('a--d-', testResponses);
expect(result).toBeObservable(expected)
expect(result).toBeObservable(expected);
});
});
@@ -124,7 +125,7 @@ describe('Core Module - RxJS Operators', () => {
d: testRCEs.d.response.resourceSelfLinks
});
expect(result).toBeObservable(expected)
expect(result).toBeObservable(expected);
});
});
@@ -136,7 +137,7 @@ describe('Core Module - RxJS Operators', () => {
scheduler.schedule(() => source.pipe(configureRequest(requestService)).subscribe());
scheduler.flush();
expect(requestService.configure).toHaveBeenCalledWith(testRequest)
expect(requestService.configure).toHaveBeenCalledWith(testRequest);
});
});
@@ -149,7 +150,7 @@ describe('Core Module - RxJS Operators', () => {
a: testRD.a.payload,
});
expect(result).toBeObservable(expected)
expect(result).toBeObservable(expected);
});
});
@@ -168,4 +169,41 @@ describe('Core Module - RxJS Operators', () => {
expect(result).toBeObservable(expected)
});
});
describe('getSucceededRemoteData', () => {
it('should return the first() hasSucceeded RemoteData Observable', () => {
const testRD = {
a: new RemoteData(false, false, true, null, undefined),
b: new RemoteData(false, false, false, null, 'b'),
c: new RemoteData(false, false, undefined, null, 'c'),
d: new RemoteData(false, false, true, null, 'd'),
e: new RemoteData(false, false, true, null, 'e'),
};
const source = hot('abcde', testRD);
const result = source.pipe(getSucceededRemoteData());
result.subscribe((value) => expect(value)
.toEqual(new RemoteData(false, false, true, null, 'd')));
});
});
describe('getAllSucceededRemoteData', () => {
it('should return all hasSucceeded RemoteData Observables', () => {
const testRD = {
a: new RemoteData(false, false, true, null, undefined),
b: new RemoteData(false, false, false, null, 'b'),
c: new RemoteData(false, false, undefined, null, 'c'),
d: new RemoteData(false, false, true, null, 'd'),
e: new RemoteData(false, false, true, null, 'e'),
};
const source = hot('abcde', testRD);
const result = source.pipe(getAllSucceededRemoteData());
const expected = cold('---de', testRD);
expect(result).toBeObservable(expected);
});
});
});

View File

@@ -62,6 +62,10 @@ export const getSucceededRemoteData = () =>
<T>(source: Observable<RemoteData<T>>): Observable<RemoteData<T>> =>
source.pipe(find((rd: RemoteData<T>) => rd.hasSucceeded), hasValueOperator());
export const getAllSucceededRemoteData = () =>
<T>(source: Observable<RemoteData<T>>): Observable<RemoteData<T>> =>
source.pipe(filter((rd: RemoteData<T>) => rd.hasSucceeded));
export const toDSpaceObjectListRD = () =>
<T extends DSpaceObject>(source: Observable<RemoteData<PaginatedList<SearchResult<T>>>>): Observable<RemoteData<PaginatedList<T>>> =>
source.pipe(

View File

@@ -1,5 +1,8 @@
import { NgModule } from '@angular/core';
import {CUSTOM_ELEMENTS_SCHEMA, NgModule} from '@angular/core';
import { QueryParamsDirectiveStub } from './query-params-directive-stub';
import { MySimpleItemActionComponent } from '../../+item-page/edit-item-page/simple-item-action/abstract-simple-item-action.component.spec';
import {CommonModule} from '@angular/common';
import {SharedModule} from '../shared.module';
import { RouterLinkDirectiveStub } from './router-link-directive-stub';
import { NgComponentOutletDirectiveStub } from './ng-component-outlet-directive-stub';
@@ -10,10 +13,17 @@ import { NgComponentOutletDirectiveStub } from './ng-component-outlet-directive-
* See https://github.com/angular/angular/issues/13590
*/
@NgModule({
imports: [
CommonModule,
SharedModule
],
declarations: [
QueryParamsDirectiveStub,
MySimpleItemActionComponent,
RouterLinkDirectiveStub,
NgComponentOutletDirectiveStub
], schemas: [
CUSTOM_ELEMENTS_SCHEMA
]
})
export class TestModule {}