1+ import { Component , OnInit } from '@angular/core' ;
2+ import { CommonModule } from '@angular/common' ;
3+ import { HttpClient } from '@angular/common/http' ;
4+ import { DomSanitizer , SafeHtml } from '@angular/platform-browser' ;
5+
6+ @Component ( {
7+ selector : 'library-alerts-pannel' ,
8+ standalone : true ,
9+ imports : [ CommonModule ] ,
10+ templateUrl : './library-alerts-pannel.component.html' ,
11+ styleUrls : [ './library-alerts-pannel.component.scss' ] ,
12+ } )
13+ export class LibraryAlertsPannelComponent implements OnInit {
14+ showAlertInfo = false ;
15+ showAlertEmergency = false ;
16+
17+ infoMessage : SafeHtml = '' ;
18+ emergencyMessage : SafeHtml = '' ;
19+
20+ constructor (
21+ private http : HttpClient ,
22+ private sanitizer : DomSanitizer
23+ ) { }
24+
25+ ngOnInit ( ) : void {
26+ this . getAlerts ( ) ;
27+ }
28+
29+ private getAlerts ( ) : void {
30+ this . http
31+ . get < any > ( 'http://127.0.0.1:5000/data/website_alerts/' )
32+ . subscribe ( {
33+ next : ( data ) => {
34+ this . showAlertInfo = data ?. informational ?. active === true ;
35+ this . showAlertEmergency = data ?. emergency ?. active === true ;
36+
37+ this . infoMessage = this . sanitizer . bypassSecurityTrustHtml (
38+ this . decodeHtmlEntitiesDeep ( data ?. informational ?. message )
39+ ) ;
40+
41+ this . emergencyMessage = this . sanitizer . bypassSecurityTrustHtml (
42+ this . decodeHtmlEntitiesDeep ( data ?. emergency ?. message )
43+ ) ;
44+ } ,
45+ error : ( err ) => {
46+ console . error ( 'Error fetching alerts:' , err ) ;
47+ } ,
48+ } ) ;
49+ }
50+
51+ closeAlert ( type : 'info' | 'emergency' ) : void {
52+ if ( type === 'info' ) {
53+ this . showAlertInfo = false ;
54+ }
55+
56+ if ( type === 'emergency' ) {
57+ this . showAlertEmergency = false ;
58+ }
59+ }
60+
61+ private decodeHtmlEntitiesDeep ( str ?: string ) : string {
62+ if ( ! str ) return '' ;
63+
64+ let previous : string ;
65+ let current = str ;
66+
67+ do {
68+ previous = current ;
69+ const textarea = document . createElement ( 'textarea' ) ;
70+ textarea . innerHTML = current ;
71+ current = textarea . value ;
72+ } while ( current !== previous ) ;
73+
74+ return current ;
75+ }
76+ }
0 commit comments