import { NgModule, NgModuleFactoryLoader, SystemJsNgModuleLoader, APP_INITIALIZER } from '@angular/core';
import { RouterModule, UrlSegment, UrlMatchResult } from '@angular/router';
import { AppComponent } from './app.component';
import { StyleGuideComponent } from './debug-tools/style-guide/style-guide.component';
import { DxaBaseComponent } from './dxa/dxa-base/dxa-base.component';
import { DxaModule } from './dxa/dxa.module';
import { CoreModule } from './core/core.module';
import { KeycloakService, KeycloakAngularModule } from 'keycloak-angular';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { ContentJsonComponent } from './debug-tools/content-json/content-json.component';
import { AppConfig } from './app.config';
import { AgmCoreModule } from '@agm/core';
import { AppAuthorizationGuard } from './core/guards/app-authorization-guard';
import { AppAuthenticationGuard } from './core/guards/app-authentication-guard';
import { OpcShortLinkGuard } from './core/guards/opc-shortlink-guard';
import { PdpResolverService } from './core/services/pdp-resolver.service';
import { CookieService } from 'ngx-cookie-service';
export function initializeApp(appConfig: AppConfig, keycloak: KeycloakService) {
return (): Promise<any> => {
return new Promise(async (resolve, reject) => {
try {
await appConfig.load();
if (document.cookie.includes('SKFCOMLOGGEDIN=yes')) {
const keyCloakConfigOnLoad = AppConfig.settings.keycloakConfig;
const keycloakJson = JSON.parse(JSON.stringify(keyCloakConfigOnLoad));
keycloakJson.initOptions = {
'checkLoginIframe': false,
'onLoad' : 'check-sso'
};
await keycloak.init(keycloakJson);
keycloak.isLoggedIn().then( res => {
if(!res) {
document.cookie = `SKFCOMLOGGEDIN=no; domain=${AppConfig.settings.cookiesConfig.domain}; path=${AppConfig.settings.cookiesConfig.path}`;
}
});
} else {
await keycloak.init(AppConfig.settings.keycloakConfig);
}
resolve();
} catch (error) {
reject(error);
}
});
};
}
@NgModule({
declarations: [
AppComponent,
StyleGuideComponent,
ContentJsonComponent,
],
imports: [
DxaModule,
CoreModule,
KeycloakAngularModule,
FormsModule,
ReactiveFormsModule,
AgmCoreModule.forRoot({
apiKey: 'AIzaSyDxkOx-ZmsN2OTfotZkoa9xdJokKu3kV1I',
libraries: ['places']
}),
RouterModule.forRoot([
{
matcher: distributorMatcher,
component: DxaBaseComponent,
canActivate: [AppAuthorizationGuard]
},
{
path: '',
redirectTo: '/group',
pathMatch: 'full'
},
{
path: 'style-guide',
component: StyleGuideComponent
},
{
path: 'productcatalogue',
component: DxaBaseComponent,
canActivate: [OpcShortLinkGuard]
},
{
matcher: myskfMatcher,
component: DxaBaseComponent,
canActivate: [AppAuthenticationGuard]
},
{
path: '**',
component: DxaBaseComponent,
resolve: { isPDP: PdpResolverService }
}
],
{
anchorScrolling: 'enabled'
})
],
providers: [
{ provide: NgModuleFactoryLoader, useClass: SystemJsNgModuleLoader },
AppConfig,
AppAuthorizationGuard,
AppAuthenticationGuard,
OpcShortLinkGuard,
{
provide: APP_INITIALIZER,
useFactory: initializeApp,
deps: [AppConfig, KeycloakService],
multi: true
},
CookieService,
],
bootstrap: [AppComponent]
})
export class AppModule { }
/** Check if last part of URL is a product designation.
* Assumed rule: designation has a prefix 'productid-'.
*/
export function productMatcher(url: UrlSegment[]): UrlMatchResult {
let isPDP = null;
let inProducts;
if (url && url.length > 0) {
const lastUrlSegment = url[url.length - 1].path;
isPDP = lastUrlSegment.startsWith('productid-') ? ({ consumed: url }) : null;
inProducts = url.find(product => product.path.toLowerCase() === 'products');
}
return inProducts && isPDP;
}
export function urlPartMatcher(url: UrlSegment[], urlPart: string): UrlMatchResult {
let match;
if (url && url.length > 0) {
match = url.find(part => part.path.toLowerCase() === urlPart) ? ({ consumed: url }) : null;
}
return match;
}
export function distributorMatcher(url: UrlSegment[]): UrlMatchResult {
return urlPartMatcher(url, "distributor-hub")
}
export function myskfMatcher(url: UrlSegment[]): UrlMatchResult {
return urlPartMatcher(url, "myskf")
}