From 737becd51f6ea6ae3823352e5302ab88c72fb547 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Popovics=20Andr=C3=A1s?= Date: Mon, 18 Jun 2018 11:54:19 +0100 Subject: [PATCH] [ADF-3190] Fix rerendering issue with content-meta-data (#3494) * Fix rerendering issue with content-meta-data * Fix indentation --- .../content-metadata.component.spec.ts | 40 ++--------------- .../content-metadata.component.ts | 43 +++++++++---------- 2 files changed, 25 insertions(+), 58 deletions(-) diff --git a/lib/content-services/content-metadata/components/content-metadata/content-metadata.component.spec.ts b/lib/content-services/content-metadata/components/content-metadata/content-metadata.component.spec.ts index ee66a1dc26..5991333162 100644 --- a/lib/content-services/content-metadata/components/content-metadata/content-metadata.component.spec.ts +++ b/lib/content-services/content-metadata/components/content-metadata/content-metadata.component.spec.ts @@ -23,20 +23,12 @@ import { By } from '@angular/platform-browser'; import { MinimalNodeEntryEntity } from 'alfresco-js-api'; import { ContentMetadataComponent } from './content-metadata.component'; import { ContentMetadataService } from '../../services/content-metadata.service'; -import { - CardViewBaseItemModel, - CardViewComponent, - CardViewUpdateService, - NodesApiService, - LogService, - setupTestBed - } from '@alfresco/adf-core'; +import { CardViewBaseItemModel, CardViewComponent, CardViewUpdateService, NodesApiService, LogService, setupTestBed } from '@alfresco/adf-core'; import { ErrorObservable } from 'rxjs/observable/ErrorObservable'; import { Observable } from 'rxjs/Observable'; import { ContentTestingModule } from '../../../testing/content.testing.module'; describe('ContentMetadataComponent', () => { - let component: ContentMetadataComponent; let fixture: ComponentFixture; let node: MinimalNodeEntryEntity; @@ -45,9 +37,7 @@ describe('ContentMetadataComponent', () => { setupTestBed({ imports: [ContentTestingModule], - providers: [ - { provide: LogService, useValue: { error: jasmine.createSpy('error') } } - ] + providers: [{ provide: LogService, useValue: { error: jasmine.createSpy('error') } }] }); beforeEach(() => { @@ -81,7 +71,6 @@ describe('ContentMetadataComponent', () => { }); describe('Default input param values', () => { - it('should have editable input param as false by default', () => { expect(component.editable).toBe(false); }); @@ -96,7 +85,6 @@ describe('ContentMetadataComponent', () => { }); describe('Folder', () => { - it('should show the folder node', () => { component.expanded = false; fixture.detectChanges(); @@ -112,7 +100,6 @@ describe('ContentMetadataComponent', () => { }); describe('Saving', () => { - it('should save the node on itemUpdate', () => { const property = { key: 'property-key', value: 'original-value' }, updateService: CardViewUpdateService = fixture.debugElement.injector.get(CardViewUpdateService), @@ -139,7 +126,7 @@ describe('ContentMetadataComponent', () => { updateService.update(property, 'updated-value'); fixture.whenStable().then(() => { - expect(component.node).toBe(expectedNode); + expect(component.node).toEqual(expectedNode); }); })); @@ -160,9 +147,7 @@ describe('ContentMetadataComponent', () => { }); describe('Properties loading', () => { - - let expectedNode, - contentMetadataService: ContentMetadataService; + let expectedNode, contentMetadataService: ContentMetadataService; beforeEach(() => { expectedNode = Object.assign({}, node, { name: 'some-modified-value' }); @@ -248,22 +233,5 @@ describe('ContentMetadataComponent', () => { expect(basicPropertiesComponent.displayEmpty).toBe(false); }); })); - - it('should be performed again if property updating occured, since the originally passed node has changed, so the previously calculated properties', () => { - const property = { key: 'property-key', value: 'original-value' }, - updateService = fixture.debugElement.injector.get(CardViewUpdateService), - nodesApiService = TestBed.get(NodesApiService); - - spyOn(nodesApiService, 'updateNode').and.callFake(() => Observable.of(node)); - spyOn(contentMetadataService, 'getBasicProperties'); - component.ngOnChanges({ node: new SimpleChange(null, node, true) }); - updateService.update(property, 'updated-value'); - - component.ngOnChanges({ expanded: new SimpleChange(false, true, false) }); - component.ngOnChanges({ expanded: new SimpleChange(true, false, false) }); - component.ngOnChanges({ expanded: new SimpleChange(false, true, false) }); - - expect(contentMetadataService.getBasicProperties).toHaveBeenCalledTimes(2); - }); }); }); diff --git a/lib/content-services/content-metadata/components/content-metadata/content-metadata.component.ts b/lib/content-services/content-metadata/components/content-metadata/content-metadata.component.ts index 109f2e99ef..c3230c769e 100644 --- a/lib/content-services/content-metadata/components/content-metadata/content-metadata.component.ts +++ b/lib/content-services/content-metadata/components/content-metadata/content-metadata.component.ts @@ -15,7 +15,7 @@ * limitations under the License. */ -import { Component, Input, OnChanges, OnInit, SimpleChanges, SimpleChange, ViewEncapsulation } from '@angular/core'; +import { Component, Input, OnChanges, OnInit, SimpleChanges, ViewEncapsulation } from '@angular/core'; import { MinimalNodeEntryEntity } from 'alfresco-js-api'; import { Observable } from 'rxjs/Observable'; import { CardViewItem, NodesApiService, LogService, CardViewUpdateService, AlfrescoApiService } from '@alfresco/adf-core'; @@ -30,7 +30,6 @@ import { CardViewGroup } from '../../interfaces/content-metadata.interfaces'; encapsulation: ViewEncapsulation.None }) export class ContentMetadataComponent implements OnChanges, OnInit { - /** (required) The node entity to fetch metadata about */ @Input() node: MinimalNodeEntryEntity; @@ -57,35 +56,35 @@ export class ContentMetadataComponent implements OnChanges, OnInit { @Input() preset: string; - nodeHasBeenUpdated: boolean = false; + componentInited: boolean = false; basicProperties$: Observable; groupedProperties$: Observable; - constructor(private contentMetadataService: ContentMetadataService, - private cardViewUpdateService: CardViewUpdateService, - private nodesApiService: NodesApiService, - private logService: LogService, - private alfrescoApiService: AlfrescoApiService) {} + constructor( + private contentMetadataService: ContentMetadataService, + private cardViewUpdateService: CardViewUpdateService, + private nodesApiService: NodesApiService, + private logService: LogService, + private alfrescoApiService: AlfrescoApiService + ) {} ngOnInit() { - this.cardViewUpdateService.itemUpdated$ - .switchMap(this.saveNode.bind(this)) - .subscribe( - (node) => { - this.nodeHasBeenUpdated = true; - this.node = node; - this.alfrescoApiService.nodeUpdated.next(node); - }, - error => this.logService.error(error) - ); + this.cardViewUpdateService.itemUpdated$.switchMap(this.saveNode.bind(this)).subscribe( + updatedNode => { + Object.assign(this.node, updatedNode); + this.alfrescoApiService.nodeUpdated.next(this.node); + }, + error => this.logService.error(error) + ); + + this.componentInited = true; } ngOnChanges(changes: SimpleChanges) { - const nodeChange: SimpleChange = changes['node']; - if (nodeChange || this.nodeHasBeenUpdated) { - const node = nodeChange && nodeChange.currentValue || this.node; - this.nodeHasBeenUpdated = false; + const changedNode: MinimalNodeEntryEntity = changes['node'] && changes['node'].currentValue; + if (!this.componentInited || (changedNode && changedNode !== this.node)) { + const node = changedNode || this.node; this.basicProperties$ = this.contentMetadataService.getBasicProperties(node); this.groupedProperties$ = this.contentMetadataService.getGroupedProperties(node, this.preset); }