@@ -12,9 +12,57 @@ export function initSystem_ajax() {
1212 'csrf_token' : csrfToken
1313 } , function ( data ) {
1414 var response = JSON . parse ( data ) ;
15+
16+ if ( action === 'reboot' ) {
17+ const reconnectModalEl = $ ( '#system-reconnect' ) ;
18+ const modal = new bootstrap . Modal ( reconnectModalEl ) ;
19+ modal . show ( ) ;
20+ handleReconnect ( reconnectModalEl ) ;
21+ }
1522 } ) ;
1623 } ) ;
1724
25+ function handleReconnect ( modal ) {
26+ const secondsEl = $ ( modal ) . find ( '#system-reconnect-seconds' ) ;
27+
28+ let countdownInterval ;
29+ const attemptSeconds = 20 ;
30+
31+ const attemptReconnect = async ( ) => {
32+ console . log ( 'attempting reconnect' ) ;
33+ $ ( secondsEl ) . text ( '...' ) ;
34+
35+ const startCountdown = ( e ) => {
36+ console . log ( 'still rebooting - start countdown' , e ) ;
37+ clearInterval ( countdownInterval ) ;
38+ let countdownInt = attemptSeconds ;
39+ countdownInterval = setInterval ( ( ) => {
40+ console . log ( 'decrement countdownInt' ) ;
41+ if ( countdownInt === 0 ) attemptReconnect ( ) ;
42+ $ ( secondsEl ) . text ( countdownInt ) ;
43+ if ( countdownInt > 0 ) countdownInt -- ;
44+ } , 1000 ) ;
45+ }
46+
47+ try {
48+ // fetch to url and get status
49+ const checkUrl = window . location . origin ;
50+ const response = await fetch ( checkUrl ) ;
51+
52+ if ( response . status === 200 ) {
53+ console . log ( 'reconnected - reload the page' ) ;
54+ window . location . reload ( ) ;
55+ } else {
56+ startCountdown ( ) ;
57+ }
58+ } catch ( e ) {
59+ startCountdown ( e ) ;
60+ }
61+ } ;
62+
63+ attemptReconnect ( ) ;
64+ }
65+
1866 $ ( '#js-system-reset-confirm' ) . on ( 'click' , function ( e ) {
1967 var progressText = $ ( '#js-system-reset-confirm' ) . attr ( 'data-message' ) ;
2068 var successHtml = $ ( '#system-reset-message' ) . attr ( 'data-message' ) ;
0 commit comments