Skip to content

Commit

Permalink
Merge pull request #749 from atmire/Edit-collection-item-template
Browse files Browse the repository at this point in the history
Edit collection item template
  • Loading branch information
tdonohue authored Jul 2, 2020
2 parents 0eb7eb6 + 92e47de commit b731301
Show file tree
Hide file tree
Showing 38 changed files with 962 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ describe('MetadataFieldFormComponent', () => {
/* tslint:disable:no-empty */
const registryServiceStub = {
getActiveMetadataField: () => observableOf(undefined),
createOrUpdateMetadataField: (field: MetadataField) => observableOf(field),
createMetadataField: (field: MetadataField) => observableOf(field),
updateMetadataField: (field: MetadataField) => observableOf(field),
cancelEditMetadataField: () => {},
cancelEditMetadataSchema: () => {},
clearMetadataFieldRequests: () => observableOf(undefined)
Expand Down Expand Up @@ -75,7 +76,6 @@ describe('MetadataFieldFormComponent', () => {
const scopeNote = 'fakeScopeNote';

const expected = Object.assign(new MetadataField(), {
schema: metadataSchema,
element: element,
qualifier: qualifier,
scopeNote: scopeNote
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,17 @@ export class MetadataFieldFormComponent implements OnInit, OnDestroy {
this.registryService.getActiveMetadataField().pipe(take(1)).subscribe(
(field) => {
const values = {
schema: this.metadataSchema,
element: this.element.value,
qualifier: this.qualifier.value,
scopeNote: this.scopeNote.value
};
if (field == null) {
this.registryService.createOrUpdateMetadataField(Object.assign(new MetadataField(), values)).subscribe((newField) => {
this.registryService.createMetadataField(Object.assign(new MetadataField(), values), this.metadataSchema).subscribe((newField) => {
this.submitForm.emit(newField);
});
} else {
this.registryService.createOrUpdateMetadataField(Object.assign(new MetadataField(), field, {
this.registryService.updateMetadataField(Object.assign(new MetadataField(), field, {
id: field.id,
schema: this.metadataSchema,
element: (values.element ? values.element : field.element),
qualifier: (values.qualifier ? values.qualifier : field.qualifier),
scopeNote: (values.scopeNote ? values.scopeNote : field.scopeNote)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('MetadataSchemaComponent', () => {
element: 'contributor',
qualifier: 'advisor',
scopeNote: null,
schema: mockSchemasList[0]
schema: createSuccessfulRemoteDataObject$(mockSchemasList[0])
},
{
id: 2,
Expand All @@ -73,7 +73,7 @@ describe('MetadataSchemaComponent', () => {
element: 'contributor',
qualifier: 'author',
scopeNote: null,
schema: mockSchemasList[0]
schema: createSuccessfulRemoteDataObject$(mockSchemasList[0])
},
{
id: 3,
Expand All @@ -85,7 +85,7 @@ describe('MetadataSchemaComponent', () => {
element: 'contributor',
qualifier: 'editor',
scopeNote: 'test scope note',
schema: mockSchemasList[1]
schema: createSuccessfulRemoteDataObject$(mockSchemasList[1])
},
{
id: 4,
Expand All @@ -97,15 +97,15 @@ describe('MetadataSchemaComponent', () => {
element: 'contributor',
qualifier: 'illustrator',
scopeNote: null,
schema: mockSchemasList[1]
schema: createSuccessfulRemoteDataObject$(mockSchemasList[1])
}
];
const mockSchemas = createSuccessfulRemoteDataObject$(new PaginatedList(null, mockSchemasList));
/* tslint:disable:no-empty */
const registryServiceStub = {
getMetadataSchemas: () => mockSchemas,
getMetadataFieldsBySchema: (schema: MetadataSchema) => createSuccessfulRemoteDataObject$(new PaginatedList(null, mockFieldsList.filter((value) => value.schema === schema))),
getMetadataSchemaByName: (schemaName: string) => createSuccessfulRemoteDataObject$(mockSchemasList.filter((value) => value.prefix === schemaName)[0]),
getMetadataFieldsBySchema: (schema: MetadataSchema) => createSuccessfulRemoteDataObject$(new PaginatedList(null, mockFieldsList.filter((value) => value.id === 3 || value.id === 4))),
getMetadataSchemaByPrefix: (schemaName: string) => createSuccessfulRemoteDataObject$(mockSchemasList.filter((value) => value.prefix === schemaName)[0]),
getActiveMetadataField: () => observableOf(undefined),
getSelectedMetadataFields: () => observableOf([]),
editMetadataField: (schema) => {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { getFirstSucceededRemoteDataPayload } from '../../../core/shared/operato
import { toFindListOptions } from '../../../shared/pagination/pagination.utils';
import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject';
import { combineLatest } from 'rxjs/internal/observable/combineLatest';
import { followLink } from '../../../shared/utils/follow-link-config.model';

@Component({
selector: 'ds-metadata-schema',
Expand Down Expand Up @@ -71,7 +72,7 @@ export class MetadataSchemaComponent implements OnInit {
* @param params
*/
initialize(params) {
this.metadataSchema$ = this.registryService.getMetadataSchemaByName(params.schemaName).pipe(getFirstSucceededRemoteDataPayload());
this.metadataSchema$ = this.registryService.getMetadataSchemaByPrefix(params.schemaName).pipe(getFirstSucceededRemoteDataPayload());
this.updateFields();
}

Expand All @@ -91,7 +92,7 @@ export class MetadataSchemaComponent implements OnInit {
this.metadataFields$ = combineLatest(this.metadataSchema$, this.needsUpdate$).pipe(
switchMap(([schema, update]: [MetadataSchema, boolean]) => {
if (update) {
return this.registryService.getMetadataFieldsBySchema(schema, toFindListOptions(this.config));
return this.registryService.getMetadataFieldsBySchema(schema, toFindListOptions(this.config), followLink('schema'));
}
})
);
Expand Down
15 changes: 15 additions & 0 deletions src/app/+collection-page/collection-page-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import { CreateCollectionPageGuard } from './create-collection-page/create-colle
import { DeleteCollectionPageComponent } from './delete-collection-page/delete-collection-page.component';
import { URLCombiner } from '../core/url-combiner/url-combiner';
import { getCollectionModulePath } from '../app-routing.module';
import { EditItemTemplatePageComponent } from './edit-item-template-page/edit-item-template-page.component';
import { ItemTemplatePageResolver } from './edit-item-template-page/item-template-page.resolver';
import { CollectionItemMapperComponent } from './collection-item-mapper/collection-item-mapper.component';
import { CollectionBreadcrumbResolver } from '../core/breadcrumbs/collection-breadcrumb.resolver';
import { DSOBreadcrumbsService } from '../core/breadcrumbs/dso-breadcrumbs.service';
import { LinkService } from '../core/cache/builders/link.service';
import { I18nBreadcrumbResolver } from '../core/breadcrumbs/i18n-breadcrumb.resolver';

export const COLLECTION_PARENT_PARAMETER = 'parent';

Expand All @@ -30,6 +33,7 @@ export function getCollectionCreatePath() {

const COLLECTION_CREATE_PATH = 'create';
const COLLECTION_EDIT_PATH = 'edit';
const ITEMTEMPLATE_PATH = 'itemtemplate';

@NgModule({
imports: [
Expand Down Expand Up @@ -58,6 +62,16 @@ const COLLECTION_EDIT_PATH = 'edit';
component: DeleteCollectionPageComponent,
canActivate: [AuthenticatedGuard],
},
{
path: ITEMTEMPLATE_PATH,
component: EditItemTemplatePageComponent,
canActivate: [AuthenticatedGuard],
resolve: {
item: ItemTemplatePageResolver,
breadcrumb: I18nBreadcrumbResolver
},
data: { title: 'collection.edit.template.title', breadcrumbKey: 'collection.edit.template' }
},
{
path: '',
component: CollectionPageComponent,
Expand All @@ -75,6 +89,7 @@ const COLLECTION_EDIT_PATH = 'edit';
],
providers: [
CollectionPageResolver,
ItemTemplatePageResolver,
CollectionBreadcrumbResolver,
DSOBreadcrumbsService,
LinkService,
Expand Down
6 changes: 5 additions & 1 deletion src/app/+collection-page/collection-page.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { CollectionPageRoutingModule } from './collection-page-routing.module';
import { CreateCollectionPageComponent } from './create-collection-page/create-collection-page.component';
import { CollectionFormComponent } from './collection-form/collection-form.component';
import { DeleteCollectionPageComponent } from './delete-collection-page/delete-collection-page.component';
import { EditItemTemplatePageComponent } from './edit-item-template-page/edit-item-template-page.component';
import { EditItemPageModule } from '../+item-page/edit-item-page/edit-item-page.module';
import { CollectionItemMapperComponent } from './collection-item-mapper/collection-item-mapper.component';
import { SearchService } from '../core/shared/search/search.service';
import { StatisticsModule } from '../statistics/statistics.module';
Expand All @@ -17,13 +19,15 @@ import { StatisticsModule } from '../statistics/statistics.module';
CommonModule,
SharedModule,
CollectionPageRoutingModule,
StatisticsModule.forRoot()
StatisticsModule.forRoot(),
EditItemPageModule
],
declarations: [
CollectionPageComponent,
CreateCollectionPageComponent,
DeleteCollectionPageComponent,
CollectionFormComponent,
EditItemTemplatePageComponent,
CollectionItemMapperComponent
],
exports: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
<div class="container-fluid mb-2" *ngVar="(itemTemplateRD$ | async) as itemTemplateRD">
<label>{{ 'collection.edit.template.label' | translate}}</label>
<div class="button-row">
<button *ngIf="!itemTemplateRD?.payload" class="btn btn-success" (click)="addItemTemplate()">
<i class="fas fa-plus"></i>
<span class="d-none d-sm-inline">&nbsp;{{"collection.edit.template.add-button" | translate}}</span>
</button>
<button *ngIf="itemTemplateRD?.payload" class="btn btn-danger" (click)="deleteItemTemplate()">
<i class="fas fa-trash-alt"></i>
<span class="d-none d-sm-inline">&nbsp;{{"collection.edit.template.delete-button" | translate}}</span>
</button>
<button *ngIf="itemTemplateRD?.payload" class="btn btn-primary"
[routerLink]="'/collections/' + (dsoRD$ | async)?.payload.uuid + '/itemtemplate'">
<i class="fas fa-edit"></i>
<span class="d-none d-sm-inline">&nbsp;{{"collection.edit.template.edit-button" | translate}}</span>
</button>
</div>
</div>
<ds-collection-form (submitForm)="onSubmit($event)"
[dso]="(dsoRD$ | async)?.payload"
(finish)="navigateToHomePage()"></ds-collection-form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,66 @@ import { SharedModule } from '../../../shared/shared.module';
import { CommonModule } from '@angular/common';
import { RouterTestingModule } from '@angular/router/testing';
import { CollectionDataService } from '../../../core/data/collection-data.service';
import { ActivatedRoute } from '@angular/router';
import { ActivatedRoute, Router } from '@angular/router';
import { of as observableOf } from 'rxjs/internal/observable/of';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { CollectionMetadataComponent } from './collection-metadata.component';
import { NotificationsService } from '../../../shared/notifications/notifications.service';
import { NotificationsServiceStub } from '../../../shared/testing/notifications-service.stub';
import { Item } from '../../../core/shared/item.model';
import { ItemTemplateDataService } from '../../../core/data/item-template-data.service';
import { Collection } from '../../../core/shared/collection.model';
import { ObjectCacheService } from '../../../core/cache/object-cache.service';
import { RequestService } from '../../../core/data/request.service';
import { createSuccessfulRemoteDataObject, createSuccessfulRemoteDataObject$ } from '../../../shared/remote-data.utils';

describe('CollectionMetadataComponent', () => {
let comp: CollectionMetadataComponent;
let fixture: ComponentFixture<CollectionMetadataComponent>;
let router: Router;
let itemTemplateService: ItemTemplateDataService;

const template = Object.assign(new Item(), {
_links: {
self: { href: 'template-selflink' }
}
});
const collection = Object.assign(new Collection(), {
uuid: 'collection-id',
id: 'collection-id',
name: 'Fake Collection',
_links: {
self: { href: 'collection-selflink' }
}
});

const itemTemplateServiceStub = Object.assign({
findByCollectionID: () => createSuccessfulRemoteDataObject$(template),
create: () => createSuccessfulRemoteDataObject$(template),
deleteByCollectionID: () => observableOf(true)
});

const notificationsService = jasmine.createSpyObj('notificationsService', {
success: {},
error: {}
});
const objectCache = jasmine.createSpyObj('objectCache', {
remove: {}
});
const requestService = jasmine.createSpyObj('requestService', {
removeByHrefSubstring: {}
});

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [TranslateModule.forRoot(), SharedModule, CommonModule, RouterTestingModule],
declarations: [CollectionMetadataComponent],
providers: [
{ provide: CollectionDataService, useValue: {} },
{ provide: ActivatedRoute, useValue: { parent: { data: observableOf({ dso: { payload: {} } }) } } },
{ provide: NotificationsService, useValue: new NotificationsServiceStub() }
{ provide: ItemTemplateDataService, useValue: itemTemplateServiceStub },
{ provide: ActivatedRoute, useValue: { parent: { data: observableOf({ dso: createSuccessfulRemoteDataObject(collection) }) } } },
{ provide: NotificationsService, useValue: notificationsService },
{ provide: ObjectCacheService, useValue: objectCache },
{ provide: RequestService, useValue: requestService }
],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents();
Expand All @@ -31,12 +72,51 @@ describe('CollectionMetadataComponent', () => {
beforeEach(() => {
fixture = TestBed.createComponent(CollectionMetadataComponent);
comp = fixture.componentInstance;
router = (comp as any).router;
itemTemplateService = (comp as any).itemTemplateService;
fixture.detectChanges();
});

describe('frontendURL', () => {
it('should have the right frontendURL set', () => {
expect((comp as any).frontendURL).toEqual('/collections/');
})
});
});

describe('addItemTemplate', () => {
it('should navigate to the collection\'s itemtemplate page', () => {
spyOn(router, 'navigate');
comp.addItemTemplate();
expect(router.navigate).toHaveBeenCalledWith(['collections', collection.uuid, 'itemtemplate']);
});
});

describe('deleteItemTemplate', () => {
describe('when delete returns a success', () => {
beforeEach(() => {
spyOn(itemTemplateService, 'deleteByCollectionID').and.returnValue(observableOf(true));
comp.deleteItemTemplate();
});

it('should display a success notification', () => {
expect(notificationsService.success).toHaveBeenCalled();
});

it('should reset related object and request cache', () => {
expect(objectCache.remove).toHaveBeenCalledWith(template.self);
expect(requestService.removeByHrefSubstring).toHaveBeenCalledWith(collection.self);
});
});

describe('when delete returns a failure', () => {
beforeEach(() => {
spyOn(itemTemplateService, 'deleteByCollectionID').and.returnValue(observableOf(false));
comp.deleteItemTemplate();
});

it('should display an error notification', () => {
expect(notificationsService.error).toHaveBeenCalled();
});
});
});
});
Loading

0 comments on commit b731301

Please sign in to comment.