Refactor itemtype loading
All checks were successful
FarmMaps.Develop/FarmMapsLib/develop This commit looks good

This commit is contained in:
Willem Dantuma 2020-02-11 18:55:43 +01:00
parent 52170590e3
commit b83aca7969
5 changed files with 83 additions and 47 deletions

View File

@ -65,10 +65,11 @@ export class AppCommonServiceModule {
ngModule: AppCommonServiceModule,
providers: [
AppConfig,
ItemTypeService,
{
provide: APP_INITIALIZER,
useFactory: appConfigFactory,
deps: [Injector, AppConfig, OAuthService, AuthConfigFactory, OAuthStorage],
deps: [Injector, AppConfig, OAuthService, AuthConfigFactory, OAuthStorage,ItemTypeService],
multi: true
},
{

View File

@ -1,7 +1,8 @@
export interface IItemType {
icon?: string;
viewer?: string;
editor?: string;
isFolder?: boolean;
iconColor?: string;
}
export interface IItemType {
icon?: string;
viewer?: string;
editor?: string;
isFolder?: boolean;
iconColor?: string;
extraAttributes?: string;
}

View File

@ -6,12 +6,13 @@ import { IItem } from '../models/item';
import { IItemTask } from '../models/itemTask';
import { HttpClient, HttpParams } from "@angular/common/http";
import { AppConfig } from "../shared/app.config";
import {ItemTypeService} from './itemtype.service';
@Injectable({
providedIn: 'root',
})
export class ItemService {
constructor(public httpClient: HttpClient, public appConfig: AppConfig) {
constructor(public httpClient: HttpClient, public appConfig: AppConfig,private itemTypeService:ItemTypeService) {
}
ApiEndpoint() {
@ -37,7 +38,13 @@ export class ItemService {
if (searchTags) params = params.append("t", searchTags);
if (startDate) params = params.append("sd", startDate.toISOString());
if (endDate) params = params.append("ed", endDate.toISOString());
if (itemType) params = params.append("it", itemType);
if (itemType) {
params = params.append("it", itemType);
let extraAttributes = this.itemTypeService.getExtraAttributes(itemType);
if(extraAttributes) {
params = params.append("da", extraAttributes);
}
}
if (parentCode) params = params.append("pc", parentCode);
if (dataFilter) params = params.append("df", dataFilter);
if (level) params = params.append("lvl", dataFilter);

View File

@ -1,20 +1,24 @@
import { Injectable } from '@angular/core';
import {IItemTypes} from '../models/item.types'
import {IItem} from '../models/item'
import {ItemService} from '../services/item.service';
import {AppConfig} from '../shared/app.config';
import {HttpClient, HttpXhrBackend} from '@angular/common/http';
@Injectable({
providedIn: 'root',
})
export class ItemTypeService {
public itemTypes: IItemTypes;
private httpClient: HttpClient;
constructor(itemService:ItemService) {
itemService.getItemTypes().subscribe((itemTypes) => {
this.itemTypes = itemTypes;
});
constructor(xhrBackend: HttpXhrBackend) {
this.httpClient = new HttpClient(xhrBackend);
}
// itemService.getItemTypes().subscribe((itemTypes) => {
// this.itemTypes = itemTypes;
// });
getIcon(itemType: string) {
var icon = "fa fa-file-o";
if (this.itemTypes[itemType]) icon = this.itemTypes[itemType].icon;
@ -27,6 +31,12 @@ export class ItemTypeService {
return color;
}
getExtraAttributes(itemType: string) {
var extraAttributes = null;
if (this.itemTypes[itemType]) extraAttributes = this.itemTypes[itemType].extraAttributes;
return extraAttributes;
}
hasViewer(item: IItem) {
let itemType: string = item.itemType;
if (this.itemTypes[itemType]) return this.itemTypes[itemType].viewer !== undefined;
@ -43,4 +53,16 @@ export class ItemTypeService {
let itemType: string = item.itemType;
return itemType == "vnd.farmmaps.itemtype.geotiff.processed" || itemType == "vnd.farmmaps.itemtype.layer" || itemType == "vnd.farmmaps.itemtype.shape.processed";
}
public load(config:AppConfig): Promise<any> {
var url = `${ config.getConfig("apiEndPoint")}/api/v1/itemtypes/`
return this.httpClient.get(url)
.toPromise()
.then((itemTypes:IItemTypes) => {
this.itemTypes = itemTypes;
//return data;
})
.catch(error => this.itemTypes = null);
};
}

View File

@ -3,44 +3,49 @@ import { Location} from '@angular/common';
import { Router,UrlSerializer } from '@angular/router';
import { AuthConfig, OAuthService, JwksValidationHandler, OAuthErrorEvent, OAuthStorage } from 'angular-oauth2-oidc';
import { AppConfig } from "./app.config";
import {ItemTypeService} from '../services/itemtype.service';
import { IAuthconfigFactory } from './authconfigFactory';
export function appConfigFactory(injector:Injector, appConfig: AppConfig, oauthService: OAuthService, authconfigFactory:IAuthconfigFactory,authStorage:OAuthStorage): () => Promise<any> {
export function appConfigFactory(injector:Injector, appConfig: AppConfig, oauthService: OAuthService, authconfigFactory:IAuthconfigFactory,authStorage:OAuthStorage,itemtypeService:ItemTypeService): () => Promise<any> {
return (): Promise<any> => {
return appConfig.load().then(() => {
oauthService.events.subscribe((event) => {
console.debug(event.type);
if (event.type == 'token_error' || event.type == 'silent_refresh_timeout') {
let e = event as OAuthErrorEvent;
let p = e.params as any;
if (event.type == 'silent_refresh_timeout' || (p.error && p.error == 'login_required')) {
let router = injector.get(Router);
console.debug("Session expired");
router.navigate(['loggedout'], { queryParams: { redirectTo: router.url } });
}
}
});
oauthService.configure(authconfigFactory.getAuthConfig(appConfig));
oauthService.setStorage(authStorage);
oauthService.tokenValidationHandler = new JwksValidationHandler();
oauthService.tokenValidationHandler.validateAtHash = function () {
return new Promise<boolean>((res) => { res(true); })
};
oauthService.setupAutomaticSilentRefresh();
let location = injector.get(Location);
let router = injector.get(Router);
let urlPath = location.path();
oauthService.loadDiscoveryDocument().then(() => {
oauthService.tryLogin({
onTokenReceived: (info) => {
urlPath = info.state;
return new Promise((resolve,reject) => {
appConfig.load().then(() => {
oauthService.events.subscribe((event) => {
console.debug(event.type);
if (event.type == 'token_error' || event.type == 'silent_refresh_timeout') {
let e = event as OAuthErrorEvent;
let p = e.params as any;
if (event.type == 'silent_refresh_timeout' || (p.error && p.error == 'login_required')) {
let router = injector.get(Router);
console.debug("Session expired");
router.navigate(['loggedout'], { queryParams: { redirectTo: router.url } });
}
}
}).then(() => {
router.navigateByUrl(urlPath);
});
})
});
oauthService.configure(authconfigFactory.getAuthConfig(appConfig));
oauthService.setStorage(authStorage);
oauthService.tokenValidationHandler = new JwksValidationHandler();
oauthService.tokenValidationHandler.validateAtHash = function () {
return new Promise<boolean>((res) => { res(true); })
};
oauthService.setupAutomaticSilentRefresh();
let location = injector.get(Location);
let router = injector.get(Router);
let urlPath = location.path();
oauthService.loadDiscoveryDocument().then(() => {
oauthService.tryLogin({
onTokenReceived: (info) => {
urlPath = info.state;
}
}).then(() => {
router.navigateByUrl(urlPath);
});
})
}).then(() => {
itemtypeService.load(appConfig).then(() => resolve()).catch(() => reject());
});
});
}
}