feat(ui): add ngx-toastr with zoneless compatibility
Add ngx-toastr and a proxy-based ToastrService wrapper that manually triggers appRef.tick() to ensure toast notifications render correctly in a zoneless change detection environment.
This commit is contained in:
@@ -27,6 +27,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"styles": [
|
"styles": [
|
||||||
|
"node_modules/ngx-toastr/toastr.css",
|
||||||
"src/styles.css"
|
"src/styles.css"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
+63
-10
@@ -1,12 +1,49 @@
|
|||||||
import { ApplicationConfig, provideBrowserGlobalErrorListeners, provideZonelessChangeDetection, APP_INITIALIZER } from '@angular/core';
|
import {
|
||||||
|
ApplicationConfig,
|
||||||
|
provideBrowserGlobalErrorListeners,
|
||||||
|
provideZonelessChangeDetection,
|
||||||
|
APP_INITIALIZER,
|
||||||
|
importProvidersFrom,
|
||||||
|
ApplicationRef,
|
||||||
|
Injector,
|
||||||
|
NgZone,
|
||||||
|
} from '@angular/core';
|
||||||
import { provideRouter } from '@angular/router';
|
import { provideRouter } from '@angular/router';
|
||||||
import { provideAnimations } from '@angular/platform-browser/animations';
|
import { provideAnimations } from '@angular/platform-browser/animations';
|
||||||
import { provideHttpClient } from '@angular/common/http';
|
import { provideHttpClient } from '@angular/common/http';
|
||||||
import { TOAST_CONFIG, DefaultGlobalConfig } from 'ngx-toastr';
|
import { DomSanitizer } from '@angular/platform-browser';
|
||||||
|
import { Overlay, ToastrModule, ToastrService, TOAST_CONFIG } from 'ngx-toastr';
|
||||||
|
|
||||||
import { routes } from './app.routes';
|
import { routes } from './app.routes';
|
||||||
import { AuthService } from './services/auth/auth.service';
|
import { AuthService } from './services/auth/auth.service';
|
||||||
|
|
||||||
|
/** The type of the injected TOAST_CONFIG value (the `{ default, config }` toast token). */
|
||||||
|
type ToastConfigToken = ConstructorParameters<typeof ToastrService>[0];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds the real ToastrService wrapped so every method forces an `ApplicationRef.tick()`.
|
||||||
|
* ngx-toastr v14 attaches each toast to ApplicationRef without rendering it; under this app's
|
||||||
|
* ZONELESS change detection there is no zone tick to render it, so without this no toast
|
||||||
|
* appears. `tick()` renders the just-attached overlay view.
|
||||||
|
*/
|
||||||
|
function zonelessToastr(
|
||||||
|
real: ToastrService,
|
||||||
|
appRef: ApplicationRef,
|
||||||
|
): ToastrService {
|
||||||
|
return new Proxy(real, {
|
||||||
|
get(target, prop, receiver) {
|
||||||
|
const value = Reflect.get(target, prop, receiver);
|
||||||
|
return typeof value === 'function'
|
||||||
|
? (...args: unknown[]) => {
|
||||||
|
const result = (value as (...a: unknown[]) => unknown).apply(target, args);
|
||||||
|
appRef.tick();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
: value;
|
||||||
|
},
|
||||||
|
}) as ToastrService;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* On every page load the HttpOnly `rag_session` cookie is validated with `GET /me`
|
* On every page load the HttpOnly `rag_session` cookie is validated with `GET /me`
|
||||||
* before the first route activates; a 401 drops the authenticated state so the
|
* before the first route activates; a 401 drops the authenticated state so the
|
||||||
@@ -23,18 +60,34 @@ export const appConfig: ApplicationConfig = {
|
|||||||
provideRouter(routes),
|
provideRouter(routes),
|
||||||
provideAnimations(),
|
provideAnimations(),
|
||||||
provideHttpClient(),
|
provideHttpClient(),
|
||||||
|
// Registers ngx-toastr's Toast component + TOAST_CONFIG.
|
||||||
|
importProvidersFrom(
|
||||||
|
ToastrModule.forRoot({
|
||||||
|
timeOut: 4000,
|
||||||
|
positionClass: 'toast-bottom-left',
|
||||||
|
preventDuplicates: true,
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
{
|
||||||
|
// Override the library's root ToastrService with a zoneless-aware wrapper so toasts
|
||||||
|
// actually render (see zonelessToastr()).
|
||||||
|
provide: ToastrService,
|
||||||
|
useFactory: (
|
||||||
|
appRef: ApplicationRef,
|
||||||
|
injector: Injector,
|
||||||
|
config: ToastConfigToken,
|
||||||
|
overlay: Overlay,
|
||||||
|
sanitizer: DomSanitizer,
|
||||||
|
zone: NgZone,
|
||||||
|
) =>
|
||||||
|
zonelessToastr(new ToastrService(config, overlay, injector, sanitizer, zone), appRef),
|
||||||
|
deps: [ApplicationRef, Injector, TOAST_CONFIG, Overlay, DomSanitizer, NgZone],
|
||||||
|
},
|
||||||
{
|
{
|
||||||
provide: APP_INITIALIZER,
|
provide: APP_INITIALIZER,
|
||||||
useFactory: initializeAuth,
|
useFactory: initializeAuth,
|
||||||
deps: [AuthService],
|
deps: [AuthService],
|
||||||
multi: true,
|
multi: true,
|
||||||
},
|
},
|
||||||
{
|
],
|
||||||
provide: TOAST_CONFIG,
|
|
||||||
useValue: {
|
|
||||||
default: DefaultGlobalConfig,
|
|
||||||
config: {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user