diff --git a/angular.json b/angular.json index b8b9030..b521570 100644 --- a/angular.json +++ b/angular.json @@ -27,6 +27,7 @@ } ], "styles": [ + "node_modules/ngx-toastr/toastr.css", "src/styles.css" ] }, diff --git a/src/app/app.config.ts b/src/app/app.config.ts index 5726eaa..e6c2a2b 100644 --- a/src/app/app.config.ts +++ b/src/app/app.config.ts @@ -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 { provideAnimations } from '@angular/platform-browser/animations'; 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 { AuthService } from './services/auth/auth.service'; +/** The type of the injected TOAST_CONFIG value (the `{ default, config }` toast token). */ +type ToastConfigToken = ConstructorParameters[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` * before the first route activates; a 401 drops the authenticated state so the @@ -23,18 +60,34 @@ export const appConfig: ApplicationConfig = { provideRouter(routes), provideAnimations(), 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, useFactory: initializeAuth, deps: [AuthService], multi: true, }, - { - provide: TOAST_CONFIG, - useValue: { - default: DefaultGlobalConfig, - config: {} - } - } - ] + ], };