2022-08-03 10:09:54 +00:00

94 lines
2.3 KiB
TypeScript

import { Injectable } from '@angular/core';
import {
ActivatedRouteSnapshot,
Resolve,
Router,
RouterStateSnapshot,
} from '@angular/router';
import { catchError, Observable, throwError } from 'rxjs';
import { Loosing } from '../models/loosing';
import { LoosingPagination } from '../models/loosing-pagination';
import { LoosingReport } from '../models/loosing-report';
import { LoosingService } from '../services/loosing.service';
@Injectable({
providedIn: 'root',
})
export class LoosingResolver implements Resolve<any> {
/**
* Constructor
*/
constructor(
private _loosingService: LoosingService,
private _router: Router
) {}
// -----------------------------------------------------------------------------------------------------
// @ Public methods
// -----------------------------------------------------------------------------------------------------
/**
* Resolver
*
* @param route
* @param state
*/
resolve(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<LoosingReport[] | undefined> {
return this._loosingService
.getLoosingById(
route.paramMap.get('id') as string,
state.root.queryParamMap.get('type') as string
)
.pipe(
// Error here means the requested product is not available
catchError((error) => {
// Log the error
console.error(error);
// Get the parent url
const parentUrl = state.url.split('/').slice(0, -1).join('/');
// Navigate to there
this._router.navigateByUrl(parentUrl);
// Throw an error
return throwError(error);
})
);
}
}
@Injectable({
providedIn: 'root',
})
export class LoosingsResolver implements Resolve<any> {
/**
* Constructor
*/
constructor(private _loosingService: LoosingService) {}
// -----------------------------------------------------------------------------------------------------
// @ Public methods
// -----------------------------------------------------------------------------------------------------
/**
* Resolver
*
* @param route
* @param state
*/
resolve(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<{
pagination: LoosingPagination;
loosings: Loosing[];
}> {
return this._loosingService.getLoosings();
}
}