44class App {
55 constructor ( ) {
66 this . csrfToken = this . getCsrfToken ( ) ;
7+ this . appBase = this . getAppBase ( ) ;
78 this . init ( ) ;
89 }
910
@@ -18,16 +19,38 @@ class App {
1819 return meta ? meta . getAttribute ( 'content' ) : '' ;
1920 }
2021
22+ getAppBase ( ) {
23+ const rawBase = window . APP_BASE || '' ;
24+ if ( typeof rawBase !== 'string' || rawBase === '/' ) {
25+ return '' ;
26+ }
27+
28+ return rawBase . endsWith ( '/' ) ? rawBase . slice ( 0 , - 1 ) : rawBase ;
29+ }
30+
31+ toRoute ( path ) {
32+ const normalizedPath = path . startsWith ( '/' ) ? path : `/${ path } ` ;
33+ return `${ this . appBase } ${ normalizedPath } ` ;
34+ }
35+
2136 initAlerts ( ) {
2237 const alerts = document . querySelectorAll ( '.alert' ) ;
2338 alerts . forEach ( ( alert ) => {
39+ if ( alert . dataset . bound === '1' ) {
40+ return ;
41+ }
42+
43+ alert . dataset . bound = '1' ;
2444 const closeBtn = document . createElement ( 'button' ) ;
2545 closeBtn . className = 'alert-close' ;
2646 closeBtn . setAttribute ( 'type' , 'button' ) ;
2747 closeBtn . setAttribute ( 'aria-label' , 'Cerrar alerta' ) ;
2848 closeBtn . textContent = 'x' ;
2949 closeBtn . addEventListener ( 'click' , ( ) => this . closeElement ( alert ) ) ;
30- alert . appendChild ( closeBtn ) ;
50+
51+ if ( ! alert . querySelector ( '.alert-close' ) ) {
52+ alert . appendChild ( closeBtn ) ;
53+ }
3154
3255 setTimeout ( ( ) => this . closeElement ( alert ) , 5000 ) ;
3356 } ) ;
@@ -67,7 +90,7 @@ class App {
6790
6891 button . addEventListener ( 'click' , ( ) => {
6992 this . showNotification (
70- ' La funcionalidad de cambio de contrasena se conecta desde la API / change-password.' ,
93+ ` La funcionalidad se conecta al endpoint ${ this . toRoute ( '/api.php?path= change-password' ) } .` ,
7194 'info'
7295 ) ;
7396 } ) ;
@@ -95,7 +118,7 @@ class App {
95118 const response = await fetch ( url , mergedOptions ) ;
96119
97120 if ( response . status === 401 ) {
98- window . location . href = '/login.php' ;
121+ window . location . href = this . toRoute ( '/login.php' ) ;
99122 return null ;
100123 }
101124
0 commit comments