This commit is contained in:
tiago.kayaya
2022-02-17 10:48:37 +01:00
34 changed files with 333 additions and 285 deletions
+50 -15
View File
@@ -1,6 +1,7 @@
import { Injectable } from '@angular/core';
import { NavigationEnd, Router, NavigationStart } from '@angular/router';
import { NavigationEnd, Router, NavigationStart, NavigationExtras } from '@angular/router';
import { LocalstoreService } from '../store/localstore.service';
import { Location } from '@angular/common'
@Injectable({
providedIn: 'root'
@@ -8,11 +9,15 @@ import { NavigationEnd, Router, NavigationStart } from '@angular/router';
export class RouteService {
history: any = [];
isGoBack = false;
constructor(
private router: Router,
private LocalstoreService: LocalstoreService,
private Location: Location
) {
this.history = this.LocalstoreService.get('history', [])
this.router.events.forEach((event) => {
if (event instanceof NavigationEnd) {
@@ -20,6 +25,7 @@ export class RouteService {
if(event.url != lastRoute) {
this.history.push(event.url)
this.reduceHistory()
}
}
@@ -30,27 +36,56 @@ export class RouteService {
}
/**
* @param url [string] incase no history to go back
* @param option [Object] some options to the url
*/
goBack(url = null, option: object = {}) {
console.log(url);
* @param url [string] incase no history to go back */
goBack(url = null) {
if(this.history.length >= 2) {
console.log(this.history);
this.history.pop();
const goTo = this.history.pop();
this.isGoBack = true;
this.router.navigate([goTo]);
const url = this.history.pop();
this.goTo(url)
} else if(url) {
this.goTo({url})
this.goTo(url)
}
this.reduceHistory()
}
goTo({url}) {
this.router.navigate([url]);
goTo(url) {
let navigationExtras: NavigationExtras = {
queryParams: this.queryParams(url)
}
const urlObject = new URL('http://localhost:8100'+url);
this.router.navigate([urlObject.pathname], navigationExtras);
}
reduceHistory() {
if(this.history.length >= 15) {
this.history = this.history.slice(5, this.history.length)
}
this.LocalstoreService.set('history', this.history)
}
queryParams(url) {
const urlObject = new URL('http://localhost:8100'+url);
const paramsString = urlObject.search;
let searchParams: any = new URLSearchParams(paramsString);
let params = {}
for (let [key, value] of searchParams) {
params[key] = value
}
console.log('params', params)
return params
}
}