25 lines
800 B
TypeScript
25 lines
800 B
TypeScript
import { RedirectCommand, ResolveFn, Router } from '@angular/router';
|
|
import { Chain } from '../models/Chain';
|
|
import { ChainDAO } from '../dao/ChainDAO';
|
|
import { inject } from '@angular/core';
|
|
|
|
export const chainResolver: ResolveFn<Chain> = async (route, _) => {
|
|
const chainDAO = inject(ChainDAO);
|
|
const router = inject(Router);
|
|
const chainID = (<{ id: string }>route.params).id;
|
|
let chain = new Chain('', null);
|
|
try {
|
|
const results = await chainDAO.findBy({ id: Number(chainID) });
|
|
if (!results[0]) {
|
|
throw new Error('The search for chain on edit did not find any match');
|
|
}
|
|
chain = results[0];
|
|
} catch (e) {
|
|
console.error(e);
|
|
return new RedirectCommand(router.parseUrl('settings/chains'), {
|
|
skipLocationChange: true,
|
|
});
|
|
}
|
|
return chain;
|
|
};
|