Skip to content

Commit d3d3353

Browse files
authored
Merge pull request DSpace#2308 from 4Science/DURACOM-155
Edit section in sidebar menu: edit page of selected item / collection / community does not refresh
2 parents c903df8 + ce517ad commit d3d3353

8 files changed

Lines changed: 94 additions & 52 deletions

File tree

src/app/collection-page/collection-form/collection-form.component.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Input, OnInit } from '@angular/core';
1+
import { ChangeDetectorRef, Component, Input, OnChanges, OnInit, SimpleChange, SimpleChanges } from '@angular/core';
22

33
import { Observable } from 'rxjs';
44
import { TranslateService } from '@ngx-translate/core';
@@ -31,7 +31,7 @@ import { NONE_ENTITY_TYPE } from '../../core/shared/item-relationships/item-type
3131
styleUrls: ['../../shared/comcol/comcol-forms/comcol-form/comcol-form.component.scss'],
3232
templateUrl: '../../shared/comcol/comcol-forms/comcol-form/comcol-form.component.html'
3333
})
34-
export class CollectionFormComponent extends ComColFormComponent<Collection> implements OnInit {
34+
export class CollectionFormComponent extends ComColFormComponent<Collection> implements OnInit, OnChanges {
3535
/**
3636
* @type {Collection} A new collection when a collection is being created, an existing Input collection when a collection is being edited
3737
*/
@@ -61,12 +61,23 @@ export class CollectionFormComponent extends ComColFormComponent<Collection> imp
6161
protected dsoService: CommunityDataService,
6262
protected requestService: RequestService,
6363
protected objectCache: ObjectCacheService,
64-
protected entityTypeService: EntityTypeDataService) {
64+
protected entityTypeService: EntityTypeDataService,
65+
protected chd: ChangeDetectorRef) {
6566
super(formService, translate, notificationsService, authService, requestService, objectCache);
6667
}
6768

68-
ngOnInit() {
69+
/**
70+
* Detect changes to the dso and initialize the form,
71+
* if the dso changes, exists and it is not the first change
72+
*/
73+
ngOnChanges(changes: SimpleChanges) {
74+
const dsoChange: SimpleChange = changes.dso;
75+
if (this.dso && dsoChange && !dsoChange.isFirstChange()) {
76+
this.initializeForm();
77+
}
78+
}
6979

80+
initializeForm() {
7081
let currentRelationshipValue: MetadataValue[];
7182
if (this.dso && this.dso.metadata) {
7283
currentRelationshipValue = this.dso.metadata['dspace.entity.type'];
@@ -96,6 +107,7 @@ export class CollectionFormComponent extends ComColFormComponent<Collection> imp
96107
this.formModel = [...collectionFormModels, this.entityTypeSelection];
97108

98109
super.ngOnInit();
110+
this.chd.detectChanges();
99111
});
100112

101113
}

src/app/collection-page/edit-collection-page/collection-metadata/collection-metadata.component.spec.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { SharedModule } from '../../../shared/shared.module';
44
import { CommonModule } from '@angular/common';
55
import { RouterTestingModule } from '@angular/router/testing';
66
import { CollectionDataService } from '../../../core/data/collection-data.service';
7-
import { ActivatedRoute, Router } from '@angular/router';
7+
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
88
import { of as observableOf } from 'rxjs';
99
import { NO_ERRORS_SCHEMA } from '@angular/core';
1010
import { CollectionMetadataComponent } from './collection-metadata.component';
@@ -52,6 +52,11 @@ describe('CollectionMetadataComponent', () => {
5252
setStaleByHrefSubstring: {}
5353
});
5454

55+
const routerMock = {
56+
events: observableOf(new NavigationEnd(1, 'url', 'url')),
57+
navigate: jasmine.createSpy('navigate'),
58+
};
59+
5560
beforeEach(waitForAsync(() => {
5661
TestBed.configureTestingModule({
5762
imports: [TranslateModule.forRoot(), SharedModule, CommonModule, RouterTestingModule],
@@ -62,6 +67,7 @@ describe('CollectionMetadataComponent', () => {
6267
{ provide: ActivatedRoute, useValue: { parent: { data: observableOf({ dso: createSuccessfulRemoteDataObject(collection) }) } } },
6368
{ provide: NotificationsService, useValue: notificationsService },
6469
{ provide: RequestService, useValue: requestService },
70+
{ provide: Router, useValue: routerMock}
6571
],
6672
schemas: [NO_ERRORS_SCHEMA]
6773
}).compileComponents();
@@ -70,8 +76,11 @@ describe('CollectionMetadataComponent', () => {
7076
beforeEach(() => {
7177
fixture = TestBed.createComponent(CollectionMetadataComponent);
7278
comp = fixture.componentInstance;
73-
router = (comp as any).router;
7479
itemTemplateService = (comp as any).itemTemplateService;
80+
spyOn(comp, 'ngOnInit');
81+
spyOn(comp, 'initTemplateItem');
82+
83+
routerMock.events = observableOf(new NavigationEnd(1, 'url', 'url'));
7584
fixture.detectChanges();
7685
});
7786

@@ -83,9 +92,8 @@ describe('CollectionMetadataComponent', () => {
8392

8493
describe('addItemTemplate', () => {
8594
it('should navigate to the collection\'s itemtemplate page', () => {
86-
spyOn(router, 'navigate');
8795
comp.addItemTemplate();
88-
expect(router.navigate).toHaveBeenCalledWith([getCollectionItemTemplateRoute(collection.uuid)]);
96+
expect(routerMock.navigate).toHaveBeenCalledWith([getCollectionItemTemplateRoute(collection.uuid)]);
8997
});
9098
});
9199

src/app/collection-page/edit-collection-page/collection-metadata/collection-metadata.component.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Component } from '@angular/core';
1+
import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
22
import { ComcolMetadataComponent } from '../../../shared/comcol/comcol-forms/edit-comcol-page/comcol-metadata/comcol-metadata.component';
33
import { Collection } from '../../../core/shared/collection.model';
44
import { CollectionDataService } from '../../../core/data/collection-data.service';
5-
import { ActivatedRoute, Router } from '@angular/router';
5+
import { ActivatedRoute, NavigationEnd, Router, Scroll } from '@angular/router';
66
import { ItemTemplateDataService } from '../../../core/data/item-template-data.service';
77
import { combineLatest as combineLatestObservable, Observable } from 'rxjs';
88
import { RemoteData } from '../../../core/data/remote-data';
@@ -23,7 +23,7 @@ import { hasValue } from '../../../shared/empty.util';
2323
selector: 'ds-collection-metadata',
2424
templateUrl: './collection-metadata.component.html',
2525
})
26-
export class CollectionMetadataComponent extends ComcolMetadataComponent<Collection> {
26+
export class CollectionMetadataComponent extends ComcolMetadataComponent<Collection> implements OnInit {
2727
protected frontendURL = '/collections/';
2828
protected type = Collection.type;
2929

@@ -40,13 +40,27 @@ export class CollectionMetadataComponent extends ComcolMetadataComponent<Collect
4040
protected notificationsService: NotificationsService,
4141
protected translate: TranslateService,
4242
protected requestService: RequestService,
43+
protected chd: ChangeDetectorRef
4344
) {
4445
super(collectionDataService, router, route, notificationsService, translate);
4546
}
4647

48+
/**
49+
* Cheking if the navigation is done and if so, initialize the collection's item template,
50+
* to ensure that the item template is always up to date.
51+
* Check when a NavigationEnd event (URL change) or a Scroll event followed by a NavigationEnd event (refresh event), occurs
52+
*/
4753
ngOnInit(): void {
48-
super.ngOnInit();
49-
this.initTemplateItem();
54+
this.router.events.subscribe((event) => {
55+
if (
56+
event instanceof NavigationEnd ||
57+
(event instanceof Scroll && event.routerEvent instanceof NavigationEnd)
58+
) {
59+
super.ngOnInit();
60+
this.initTemplateItem();
61+
this.chd.detectChanges();
62+
}
63+
});
5064
}
5165

5266
/**

src/app/community-page/community-form/community-form.component.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Input } from '@angular/core';
1+
import { Component, Input, OnChanges, SimpleChange, SimpleChanges } from '@angular/core';
22
import {
33
DynamicFormControlModel,
44
DynamicFormService,
@@ -23,7 +23,7 @@ import { environment } from '../../../environments/environment';
2323
styleUrls: ['../../shared/comcol/comcol-forms/comcol-form/comcol-form.component.scss'],
2424
templateUrl: '../../shared/comcol/comcol-forms/comcol-form/comcol-form.component.html'
2525
})
26-
export class CommunityFormComponent extends ComColFormComponent<Community> {
26+
export class CommunityFormComponent extends ComColFormComponent<Community> implements OnChanges {
2727
/**
2828
* @type {Community} A new community when a community is being created, an existing Input community when a community is being edited
2929
*/
@@ -81,4 +81,11 @@ export class CommunityFormComponent extends ComColFormComponent<Community> {
8181
protected objectCache: ObjectCacheService) {
8282
super(formService, translate, notificationsService, authService, requestService, objectCache);
8383
}
84+
85+
ngOnChanges(changes: SimpleChanges) {
86+
const dsoChange: SimpleChange = changes.dso;
87+
if (this.dso && dsoChange && !dsoChange.isFirstChange()) {
88+
super.ngOnInit();
89+
}
90+
}
8491
}

src/app/item-page/edit-item-page/item-status/item-status.component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { fadeIn, fadeInOut } from '../../../shared/animations/fade';
33
import { Item } from '../../../core/shared/item.model';
44
import { ActivatedRoute } from '@angular/router';
55
import { ItemOperation } from '../item-operation/itemOperation.model';
6-
import { distinctUntilChanged, first, map, mergeMap, switchMap, toArray } from 'rxjs/operators';
6+
import { distinctUntilChanged, map, mergeMap, switchMap, toArray } from 'rxjs/operators';
77
import { BehaviorSubject, Observable, Subscription } from 'rxjs';
88
import { RemoteData } from '../../../core/data/remote-data';
99
import { getItemEditRoute, getItemPageRoute } from '../../item-page-routing-paths';
@@ -82,7 +82,6 @@ export class ItemStatusComponent implements OnInit {
8282
ngOnInit(): void {
8383
this.itemRD$ = this.route.parent.data.pipe(map((data) => data.dso));
8484
this.itemRD$.pipe(
85-
first(),
8685
map((data: RemoteData<Item>) => data.payload)
8786
).subscribe((item: Item) => {
8887
this.statusData = Object.assign({

src/app/shared/comcol/comcol-forms/comcol-form/comcol-form.component.ts

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -127,39 +127,41 @@ export class ComColFormComponent<T extends Collection | Community> implements On
127127
}
128128

129129
ngOnInit(): void {
130-
this.formModel.forEach(
131-
(fieldModel: DynamicInputModel) => {
132-
fieldModel.value = this.dso.firstMetadataValue(fieldModel.name);
133-
}
134-
);
135-
this.formGroup = this.formService.createFormGroup(this.formModel);
130+
if (hasValue(this.formModel)) {
131+
this.formModel.forEach(
132+
(fieldModel: DynamicInputModel) => {
133+
fieldModel.value = this.dso.firstMetadataValue(fieldModel.name);
134+
}
135+
);
136+
this.formGroup = this.formService.createFormGroup(this.formModel);
136137

137-
this.updateFieldTranslations();
138-
this.translate.onLangChange
139-
.subscribe(() => {
140-
this.updateFieldTranslations();
141-
});
138+
this.updateFieldTranslations();
139+
this.translate.onLangChange
140+
.subscribe(() => {
141+
this.updateFieldTranslations();
142+
});
142143

143-
if (hasValue(this.dso.id)) {
144-
this.subs.push(
145-
observableCombineLatest([
146-
this.dsoService.getLogoEndpoint(this.dso.id),
147-
this.dso.logo
148-
]).subscribe(([href, logoRD]: [string, RemoteData<Bitstream>]) => {
149-
this.uploadFilesOptions.url = href;
150-
this.uploadFilesOptions.authToken = this.authService.buildAuthHeader();
151-
// If the object already contains a logo, send out a PUT request instead of POST for setting a new logo
152-
if (hasValue(logoRD.payload)) {
153-
this.uploadFilesOptions.method = RestRequestMethod.PUT;
154-
}
155-
this.initializedUploaderOptions.next(true);
156-
})
157-
);
158-
} else {
159-
// Set a placeholder URL to not break the uploader component. This will be replaced once the object is created.
160-
this.uploadFilesOptions.url = 'placeholder';
161-
this.uploadFilesOptions.authToken = this.authService.buildAuthHeader();
162-
this.initializedUploaderOptions.next(true);
144+
if (hasValue(this.dso.id)) {
145+
this.subs.push(
146+
observableCombineLatest([
147+
this.dsoService.getLogoEndpoint(this.dso.id),
148+
this.dso.logo
149+
]).subscribe(([href, logoRD]: [string, RemoteData<Bitstream>]) => {
150+
this.uploadFilesOptions.url = href;
151+
this.uploadFilesOptions.authToken = this.authService.buildAuthHeader();
152+
// If the object already contains a logo, send out a PUT request instead of POST for setting a new logo
153+
if (hasValue(logoRD.payload)) {
154+
this.uploadFilesOptions.method = RestRequestMethod.PUT;
155+
}
156+
this.initializedUploaderOptions.next(true);
157+
})
158+
);
159+
} else {
160+
// Set a placeholder URL to not break the uploader component. This will be replaced once the object is created.
161+
this.uploadFilesOptions.url = 'placeholder';
162+
this.uploadFilesOptions.authToken = this.authService.buildAuthHeader();
163+
this.initializedUploaderOptions.next(true);
164+
}
163165
}
164166
}
165167

src/app/shared/comcol/comcol-forms/edit-comcol-page/comcol-metadata/comcol-metadata.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { DSpaceObject } from '../../../../../core/shared/dspace-object.model';
33
import { Observable } from 'rxjs';
44
import { RemoteData } from '../../../../../core/data/remote-data';
55
import { ActivatedRoute, Router } from '@angular/router';
6-
import { first, map, take } from 'rxjs/operators';
6+
import { map, take } from 'rxjs/operators';
77
import { getFirstCompletedRemoteData, getFirstSucceededRemoteData } from '../../../../../core/shared/operators';
88
import { hasValue, isEmpty } from '../../../../empty.util';
99
import { ResourceType } from '../../../../../core/shared/resource-type';
@@ -42,7 +42,7 @@ export class ComcolMetadataComponent<TDomain extends Community | Collection> imp
4242
}
4343

4444
ngOnInit(): void {
45-
this.dsoRD$ = this.route.parent.data.pipe(first(), map((data) => data.dso));
45+
this.dsoRD$ = this.route.parent.data.pipe(map((data) => data.dso));
4646
}
4747

4848
/**

src/app/shared/comcol/comcol-forms/edit-comcol-page/edit-comcol-page.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component, OnInit } from '@angular/core';
22

33
import { Observable } from 'rxjs';
4-
import { first, map } from 'rxjs/operators';
4+
import { map } from 'rxjs/operators';
55

66
import { ActivatedRoute, Router } from '@angular/router';
77
import { RemoteData } from '../../../../core/data/remote-data';
@@ -53,7 +53,7 @@ export class EditComColPageComponent<TDomain extends DSpaceObject> implements On
5353
this.pages = this.route.routeConfig.children
5454
.map((child: any) => child.path)
5555
.filter((path: string) => isNotEmpty(path)); // ignore reroutes
56-
this.dsoRD$ = this.route.data.pipe(first(), map((data) => data.dso));
56+
this.dsoRD$ = this.route.data.pipe(map((data) => data.dso));
5757
}
5858

5959
/**

0 commit comments

Comments
 (0)