diff --git a/src/app/app.component.spec.ts b/src/app/app.component.spec.ts index cbab798f1e..6b8ec5a4bc 100644 --- a/src/app/app.component.spec.ts +++ b/src/app/app.component.spec.ts @@ -34,6 +34,10 @@ import { Angulartics2GoogleAnalytics } from 'angulartics2/ga'; import { AngularticsMock } from './shared/mocks/mock-angulartics.service'; import { AuthServiceMock } from './shared/mocks/mock-auth.service'; import { AuthService } from './core/auth/auth.service'; +import { RouteService } from './shared/services/route.service'; +import { ActivatedRoute, Router } from '@angular/router'; +import { MockActivatedRoute } from './shared/mocks/mock-active-router'; +import { MockRouter } from './shared/mocks/mock-router'; let comp: AppComponent; let fixture: ComponentFixture; @@ -62,7 +66,10 @@ describe('App component', () => { { provide: MetadataService, useValue: new MockMetadataService() }, { provide: Angulartics2GoogleAnalytics, useValue: new AngularticsMock() }, { provide: AuthService, useValue: new AuthServiceMock() }, - AppComponent + { provide: ActivatedRoute, useValue: new MockActivatedRoute() }, + { provide: Router, useValue: new MockRouter() }, + AppComponent, + RouteService ], schemas: [CUSTOM_ELEMENTS_SCHEMA] }) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 1c1a47cf12..f98f0185df 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -13,6 +13,7 @@ import { NativeWindowRef, NativeWindowService } from './shared/services/window.s import { isAuthenticated } from './core/auth/selectors'; import { AuthService } from './core/auth/auth.service'; import { Angulartics2GoogleAnalytics } from 'angulartics2/ga'; +import { RouteService } from './shared/services/route.service'; @Component({ selector: 'ds-app', @@ -30,7 +31,8 @@ export class AppComponent implements OnInit { private store: Store, private metadata: MetadataService, private angulartics2GoogleAnalytics: Angulartics2GoogleAnalytics, - private authService: AuthService + private authService: AuthService, + private routeService: RouteService ) { // this language will be used as a fallback when a translation isn't found in the current language translate.setDefaultLang('en'); @@ -39,6 +41,8 @@ export class AppComponent implements OnInit { metadata.listenForRouteChange(); + routeService.saveRouting(); + if (config.debug) { console.info(config); } diff --git a/src/app/shared/chips/models/chips.model.ts b/src/app/shared/chips/models/chips.model.ts index e133a416f4..92a1b18fb9 100644 --- a/src/app/shared/chips/models/chips.model.ts +++ b/src/app/shared/chips/models/chips.model.ts @@ -2,6 +2,7 @@ import { findIndex, isEqual, isObject } from 'lodash'; import { BehaviorSubject } from 'rxjs/BehaviorSubject'; import { ChipsItem, ChipsItemIcon } from './chips-item.model'; import { hasValue, isNotEmpty } from '../../empty.util'; +import { PLACEHOLDER_PARENT_METADATA } from '../../form/builder/ds-dynamic-form-ui/models/dynamic-group/dynamic-group.model'; export interface IconsConfig { withAuthority?: { @@ -83,6 +84,14 @@ export class Chips { return this._items.length > 0; } + private hasPlaceholder(value) { + if (isObject(value)) { + return value.value === PLACEHOLDER_PARENT_METADATA; + } else { + return value === PLACEHOLDER_PARENT_METADATA; + } + } + public remove(chipsItem: ChipsItem): void { const index = findIndex(this._items, {id: chipsItem.id}); this._items.splice(index, 1); @@ -119,7 +128,7 @@ export class Chips { config = (configIndex !== -1) ? this.iconsConfig[configIndex].config : defaultConfig; - if (hasValue(value) && isNotEmpty(config)) { + if (hasValue(value) && isNotEmpty(config) && !this.hasPlaceholder(value)) { let icon: ChipsItemIcon; const hasAuthority: boolean = !!(isObject(value) && ((value.hasOwnProperty('authority') && value.authority) || (value.hasOwnProperty('id') && value.id))); diff --git a/src/app/shared/mocks/mock-router.ts b/src/app/shared/mocks/mock-router.ts index 054c63d4c0..cf9a522f07 100644 --- a/src/app/shared/mocks/mock-router.ts +++ b/src/app/shared/mocks/mock-router.ts @@ -1,4 +1,8 @@ +import { Observable } from 'rxjs/Observable'; + export class MockRouter { + public events = Observable.of({}); + // noinspection TypeScriptUnresolvedFunction navigate = jasmine.createSpy('navigate'); } diff --git a/src/app/shared/services/route.service.spec.ts b/src/app/shared/services/route.service.spec.ts index b134771b3e..6ec9ef8d53 100644 --- a/src/app/shared/services/route.service.spec.ts +++ b/src/app/shared/services/route.service.spec.ts @@ -1,7 +1,9 @@ import { RouteService } from './route.service'; import { async, TestBed } from '@angular/core/testing'; -import { ActivatedRoute, convertToParamMap, Params } from '@angular/router'; +import { ActivatedRoute, convertToParamMap, Params, Router } from '@angular/router'; import { Observable } from 'rxjs/Observable'; +import 'rxjs/add/observable/of' +import { MockRouter } from '../mocks/mock-router'; describe('RouteService', () => { let service: RouteService; @@ -28,12 +30,13 @@ describe('RouteService', () => { queryParamMap: Observable.of(convertToParamMap(paramObject)) }, }, + { provide: Router, useValue: new MockRouter() }, ] }); })); beforeEach(() => { - service = new RouteService(TestBed.get(ActivatedRoute)); + service = new RouteService(TestBed.get(ActivatedRoute), TestBed.get(Router)); }); describe('hasQueryParam', () => { diff --git a/src/app/shared/services/route.service.ts b/src/app/shared/services/route.service.ts index aa683a6403..5b6ad44aba 100644 --- a/src/app/shared/services/route.service.ts +++ b/src/app/shared/services/route.service.ts @@ -1,15 +1,14 @@ import { Injectable } from '@angular/core'; import { Observable } from 'rxjs/Observable'; -import { - ActivatedRoute, convertToParamMap, NavigationExtras, Params, - Router, -} from '@angular/router'; -import { isNotEmpty } from '../empty.util'; +import { ActivatedRoute, NavigationEnd, Params, Router } from '@angular/router'; +import { filter } from 'rxjs/operators'; @Injectable() export class RouteService { - constructor(private route: ActivatedRoute) { + private history = []; + + constructor(private route: ActivatedRoute, private router: Router) { } getQueryParameterValues(paramName: string): Observable { @@ -40,4 +39,22 @@ export class RouteService { return params; }).distinctUntilChanged(); } + + public saveRouting(): void { + this.router.events + .pipe(filter((event) => event instanceof NavigationEnd)) + .subscribe(({urlAfterRedirects}: NavigationEnd) => { + this.history = [...this.history, urlAfterRedirects]; + console.log(this.history); + }); + } + + public getHistory(): string[] { + return this.history; + } + + public getPreviousUrl(): string { + return this.history[this.history.length - 2] || ''; + } + } diff --git a/src/app/submission/submission.service.ts b/src/app/submission/submission.service.ts index cef39c9a52..7eedd55ab6 100644 --- a/src/app/submission/submission.service.ts +++ b/src/app/submission/submission.service.ts @@ -1,4 +1,6 @@ import { Inject, Injectable } from '@angular/core'; +import { Router } from '@angular/router'; + import { Observable } from 'rxjs/Observable'; import { Subscription } from 'rxjs/Subscription'; import { Store } from '@ngrx/store'; @@ -17,10 +19,10 @@ import { GLOBAL_CONFIG } from '../../config'; import { HttpHeaders } from '@angular/common/http'; import { HttpOptions } from '../core/dspace-rest-v2/dspace-rest-v2.service'; import { SubmissionRestService } from './submission-rest.service'; -import { Router } from '@angular/router'; import { SectionDataObject } from './sections/models/section-data.model'; import { SubmissionScopeType } from '../core/submission/submission-scope-type'; import { SubmissionObject } from '../core/submission/models/submission-object.model'; +import { RouteService } from '../shared/services/route.service'; @Injectable() export class SubmissionService { @@ -31,6 +33,7 @@ export class SubmissionService { constructor(@Inject(GLOBAL_CONFIG) protected EnvConfig: GlobalConfig, protected restService: SubmissionRestService, protected router: Router, + protected routeService: RouteService, protected store: Store) { } @@ -191,7 +194,12 @@ export class SubmissionService { } redirectToMyDSpace() { - this.router.navigate(['/mydspace']); + const previousUrl = this.routeService.getPreviousUrl(); + if (isEmpty(previousUrl)) { + this.router.navigate(['/mydspace']); + } else { + this.router.navigateByUrl(previousUrl); + } } retrieveSubmission(submissionId): Observable {