Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { TemplateForm } from '../../own-submissions/models/template-form';
import { Router } from '@angular/router';
import { LocalizeRouterService } from '../../../locale/localize-router.service';
import { DeleteOwnDocumentService } from '../../../shared/service/delete-own-document.service';
import { HistoryService } from '../../../shared/service/history.service';
import { BrowserService } from '../../../shared/service/browser.service';
import { DocumentPermissionService } from '../service/document-permission.service';
import { FormService } from '../../../shared/service/form.service';
import { Form } from '../../../shared/model/Form';
Expand Down Expand Up @@ -101,7 +101,7 @@ export class DocumentComponent implements AfterViewInit, OnChanges, OnInit, OnDe
private router: Router,
private localizeRouterService: LocalizeRouterService,
private deleteDocumentService: DeleteOwnDocumentService,
private historyService: HistoryService,
private browserService: BrowserService,
private documentPermissionService: DocumentPermissionService,
private formService: FormService
) { }
Expand Down Expand Up @@ -343,15 +343,11 @@ export class DocumentComponent implements AfterViewInit, OnChanges, OnInit, OnDe
return;
}

if(this.historyService.isFirstLoad()) {
this.browserService.goBack(() => {
this.router.navigate(
this.localizeRouterService.translateRoute(['/vihko/home/'])
);
} else {
this.router.navigate(
this.localizeRouterService.translateRoute([this.historyService.getPrevious()])
);
}
});
}
}

Expand Down
27 changes: 18 additions & 9 deletions projects/laji/src/app/shared/service/history.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const MAX_HISTORY = 30;
@Injectable({providedIn: 'root'})
export class HistoryService implements OnDestroy {
private history: string[] = [];
private hasBack = false;
private anyPageLoaded = false;
private isInitialLoad = false;
private routeSub?: Subscription;

constructor(
Expand All @@ -23,13 +24,25 @@ export class HistoryService implements OnDestroy {
this.routeSub = this.router.events.pipe(
filter(Util.eventIsNavigationEnd)
).subscribe(({urlAfterRedirects}: NavigationEnd) => {
if (this.router.getCurrentNavigation()?.extras.replaceUrl
|| urlAfterRedirects.match(/\/user\/(login|logout)/)
if (!this.anyPageLoaded) {
this.anyPageLoaded = true;
this.isInitialLoad = true;
} else {
this.isInitialLoad = false;
}

if (urlAfterRedirects.match(/\/user\/(login|logout)/)
|| this.history[this.history.length - 1] === urlAfterRedirects) {
return;
}

if (this.router.currentNavigation()?.extras.replaceUrl) {
this.history.pop();
this.history = [...this.history, urlAfterRedirects];
return;
}

if (this.history[this.history.length - 2] === urlAfterRedirects) {
this.hasBack = true;
this.history.pop();
} else {
this.history = [...this.history, urlAfterRedirects];
Expand All @@ -54,11 +67,7 @@ export class HistoryService implements OnDestroy {
return this.history.length > 1;
}

public getPrevious(): string {
return this.history[this.history.length - 2] || '';
}

public isFirstLoad() {
return !(this.hasPrevious() || this.hasBack);
return this.isInitialLoad;
}
}