feat: enhance app configuration and dashboard component with HTTP interceptors and improved data loading #4
This commit is contained in:
@@ -2,8 +2,11 @@ import {
|
|||||||
ApplicationConfig,
|
ApplicationConfig,
|
||||||
provideZoneChangeDetection,
|
provideZoneChangeDetection,
|
||||||
importProvidersFrom,
|
importProvidersFrom,
|
||||||
|
provideAppInitializer,
|
||||||
|
inject,
|
||||||
} from '@angular/core';
|
} from '@angular/core';
|
||||||
import {
|
import {
|
||||||
|
HTTP_INTERCEPTORS,
|
||||||
HttpClient,
|
HttpClient,
|
||||||
provideHttpClient,
|
provideHttpClient,
|
||||||
withInterceptorsFromDi,
|
withInterceptorsFromDi,
|
||||||
@@ -38,7 +41,9 @@ import { adapterFactory } from 'angular-calendar/date-adapters/date-fns';
|
|||||||
// code view
|
// code view
|
||||||
import { provideHighlightOptions } from 'ngx-highlightjs';
|
import { provideHighlightOptions } from 'ngx-highlightjs';
|
||||||
import 'highlight.js/styles/atom-one-dark.min.css';
|
import 'highlight.js/styles/atom-one-dark.min.css';
|
||||||
import { OAuthModule, provideOAuthClient } from 'angular-oauth2-oidc';
|
import { DefaultOAuthInterceptor, OAuthModule, provideOAuthClient } from 'angular-oauth2-oidc';
|
||||||
|
import { AppConfigService } from './services/config.service';
|
||||||
|
import { ApiEndpointInterceptor } from './services/http.interceptor';
|
||||||
|
|
||||||
export function HttpLoaderFactory(http: HttpClient): any {
|
export function HttpLoaderFactory(http: HttpClient): any {
|
||||||
return new TranslateHttpLoader(http, './assets/i18n/', '.json');
|
return new TranslateHttpLoader(http, './assets/i18n/', '.json');
|
||||||
@@ -66,6 +71,18 @@ export const appConfig: ApplicationConfig = {
|
|||||||
}),
|
}),
|
||||||
withComponentInputBinding()
|
withComponentInputBinding()
|
||||||
),
|
),
|
||||||
|
provideAppInitializer(() => inject(AppConfigService).loadConfig()),
|
||||||
|
{
|
||||||
|
provide: HTTP_INTERCEPTORS,
|
||||||
|
useClass: DefaultOAuthInterceptor,
|
||||||
|
multi: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provide: HTTP_INTERCEPTORS,
|
||||||
|
useClass: ApiEndpointInterceptor,
|
||||||
|
multi: true,
|
||||||
|
},
|
||||||
|
|
||||||
provideHttpClient(withInterceptorsFromDi()),
|
provideHttpClient(withInterceptorsFromDi()),
|
||||||
provideOAuthClient({
|
provideOAuthClient({
|
||||||
resourceServer: {
|
resourceServer: {
|
||||||
|
|||||||
@@ -1,14 +1,25 @@
|
|||||||
import { Component } from '@angular/core';
|
import { JsonPipe } from '@angular/common';
|
||||||
|
import { HttpClient } from '@angular/common/http';
|
||||||
|
import { Component, signal } from '@angular/core';
|
||||||
|
|
||||||
// components
|
// components
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-dashboard-main',
|
selector: 'app-dashboard-main',
|
||||||
imports: [
|
imports: [
|
||||||
|
JsonPipe
|
||||||
],
|
],
|
||||||
template: ''
|
template: '<div>{{ data() | json }}</div>',
|
||||||
})
|
})
|
||||||
export class AppDashboardMainComponent {
|
export class AppDashboardMainComponent {
|
||||||
constructor() { }
|
|
||||||
|
data = signal({});
|
||||||
|
|
||||||
|
constructor(httpClient: HttpClient) {
|
||||||
|
httpClient.get('/api/product')
|
||||||
|
.subscribe(data => {
|
||||||
|
this.data.set(data);
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ export class AppConfigService {
|
|||||||
|
|
||||||
loadConfig(): Observable<any> {
|
loadConfig(): Observable<any> {
|
||||||
return new Observable(observer => {
|
return new Observable(observer => {
|
||||||
fetch('/config.json')
|
fetch('/assets/config.json')
|
||||||
.then(async response => {
|
.then(async response => {
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`Could not load config.json: ${response.statusText}`);
|
throw new Error(`Could not load config.json: ${response.statusText}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user