@@ -25,116 +25,119 @@ export const handleUpdateNextStepButton = () => {
2525 }
2626} ;
2727
28- export const handleUpdateInformation = ( ) => {
28+ export const handleUpdateInformation = async ( ) => {
2929 if ( window . location . href . endsWith ( 'update.php' ) ) {
3030 const installedVersion = document . getElementById ( 'phpmyfaq-update-installed-version' ) ;
3131
32- fetch ( '../../api/setup/check' , {
33- method : 'POST' ,
34- headers : {
35- Accept : 'application/json, text/plain, */*' ,
36- 'Content-Type' : 'application/json' ,
37- } ,
38- body : installedVersion . value ,
39- } )
40- . then ( async ( response ) => {
41- if ( response . ok ) {
42- return response . json ( ) ;
43- }
44- throw new Error ( 'Network response was not ok: ' , { cause : { response } } ) ;
45- } )
46- . then ( ( data ) => {
47- const button = document . getElementById ( 'phpmyfaq-update-next-step-button' ) ;
48- const alert = document . getElementById ( 'phpmyfaq-update-check-success' ) ;
49-
50- alert . classList . remove ( 'd-none' ) ;
51-
52- button . classList . remove ( 'disabled' ) ;
53- button . disabled = false ;
54- } )
55- . catch ( async ( error ) => {
56- const errorMessage = await error . cause . response . json ( ) ;
57- const alert = document . getElementById ( 'phpmyfaq-update-check-alert' ) ;
58- const alertResult = document . getElementById ( 'phpmyfaq-update-check-result' ) ;
59-
60- alert . classList . remove ( 'd-none' ) ;
61- alertResult . innerText = errorMessage . message ;
32+ try {
33+ const response = await fetch ( '../../api/setup/check' , {
34+ method : 'POST' ,
35+ headers : {
36+ Accept : 'application/json, text/plain, */*' ,
37+ 'Content-Type' : 'application/json' ,
38+ } ,
39+ body : installedVersion . value ,
6240 } ) ;
41+
42+ if ( ! response . ok ) {
43+ let errorMessage = 'Network response was not ok.' ;
44+ if ( response . status === 404 ) {
45+ errorMessage = 'The requested resource was not found on the server. Please check your server configuration.' ;
46+ } else if ( response . status === 409 ) {
47+ errorMessage = 'Maintenance mode is not enabled. Please enable it first.' ;
48+ }
49+ throw new Error ( errorMessage ) ;
50+ }
51+
52+ const button = document . getElementById ( 'phpmyfaq-update-next-step-button' ) ;
53+ const alert = document . getElementById ( 'phpmyfaq-update-check-success' ) ;
54+
55+ alert . classList . remove ( 'd-none' ) ;
56+ button . classList . remove ( 'disabled' ) ;
57+ button . disabled = false ;
58+ } catch ( errorMessage ) {
59+ const alert = document . getElementById ( 'phpmyfaq-update-check-alert' ) ;
60+ const alertResult = document . getElementById ( 'phpmyfaq-update-check-result' ) ;
61+
62+ alert . classList . remove ( 'd-none' ) ;
63+ alertResult . innerText = errorMessage ;
64+ }
6365 }
6466} ;
6567
66- export const handleConfigBackup = ( ) => {
68+ export const handleConfigBackup = async ( ) => {
6769 if ( window . location . href . endsWith ( 'update.php?step=2' ) ) {
6870 const installedVersion = document . getElementById ( 'phpmyfaq-update-installed-version' ) ;
6971
70- fetch ( '../../api/setup/backup' , {
71- method : 'POST' ,
72- headers : {
73- Accept : 'application/json, text/plain, */*' ,
74- 'Content-Type' : 'application/json' ,
75- } ,
76- body : installedVersion . value ,
77- } )
78- . then ( async ( response ) => {
79- if ( response . ok ) {
80- return response . json ( ) ;
81- }
82- throw new Error ( 'Network response was not ok: ' , { cause : { response } } ) ;
83- } )
84- . then ( ( data ) => {
85- const downloadLink = document . getElementById ( 'phpmyfaq-update-backup-download-link' ) ;
86- downloadLink . href = data . backupFile ;
87- } )
88- . catch ( async ( error ) => {
89- const errorMessage = await error . cause . response . json ( ) ;
90- return errorMessage . error ;
72+ try {
73+ const response = await fetch ( '../../api/setup/backup' , {
74+ method : 'POST' ,
75+ headers : {
76+ Accept : 'application/json, text/plain, */*' ,
77+ 'Content-Type' : 'application/json' ,
78+ } ,
79+ body : installedVersion . value ,
9180 } ) ;
81+
82+ if ( ! response . ok ) {
83+ throw new Error ( 'Network response was not ok' ) ;
84+ }
85+
86+ const data = await response . json ( ) ;
87+ const downloadLink = document . getElementById ( 'phpmyfaq-update-backup-download-link' ) ;
88+ downloadLink . href = data . backupFile ;
89+ } catch ( error ) {
90+ const errorMessage =
91+ error . cause && error . cause . response ? await error . cause . response . json ( ) : { error : 'Unknown error' } ;
92+ return errorMessage . error ;
93+ }
9294 }
9395} ;
9496
9597export const handleDatabaseUpdate = async ( ) => {
9698 if ( window . location . href . endsWith ( 'update.php?step=3' ) ) {
9799 const installedVersion = document . getElementById ( 'phpmyfaq-update-installed-version' ) ;
98100
99- await fetch ( '../../api/setup/update-database' , {
100- method : 'POST' ,
101- headers : {
102- Accept : 'application/json, text/plain, */*' ,
103- 'Content-Type' : 'application/json' ,
104- } ,
105- body : installedVersion . value ,
106- } )
107- . then ( async ( response ) => {
108- const progressBarInstallation = document . getElementById ( 'result-update' ) ;
109- const reader = response . body . getReader ( ) ;
110-
111- function pump ( ) {
112- return reader . read ( ) . then ( ( { done, value } ) => {
113- const decodedValue = new TextDecoder ( ) . decode ( value ) ;
114-
115- if ( done ) {
116- progressBarInstallation . style . width = '100%' ;
117- progressBarInstallation . innerText = '100%' ;
118- progressBarInstallation . classList . remove ( 'progress-bar-animated' ) ;
119- const alert = document . getElementById ( 'phpmyfaq-update-database-success' ) ;
120- alert . classList . remove ( 'd-none' ) ;
121- return ;
122- } else {
123- progressBarInstallation . style . width = JSON . parse ( decodedValue ) . progress ;
124- progressBarInstallation . innerText = JSON . parse ( decodedValue ) . progress ;
125- }
126-
127- return pump ( ) ;
128- } ) ;
129- }
130- return pump ( ) ;
131- } )
132- . catch ( async ( error ) => {
133- const errorMessage = await error . cause . response . json ( ) ;
134- const alert = document . getElementById ( 'phpmyfaq-update-database-error' ) ;
135-
136- alert . classList . remove ( 'd-none' ) ;
137- alert . innerHTML = errorMessage . error ;
101+ try {
102+ const response = await fetch ( '../../api/setup/update-database' , {
103+ method : 'POST' ,
104+ headers : {
105+ Accept : 'application/json, text/plain, */*' ,
106+ 'Content-Type' : 'application/json' ,
107+ } ,
108+ body : installedVersion . value ,
138109 } ) ;
110+
111+ const progressBarInstallation = document . getElementById ( 'result-update' ) ;
112+ const reader = response . body . getReader ( ) ;
113+
114+ async function pump ( ) {
115+ const { done, value } = await reader . read ( ) ;
116+ const decodedValue = new TextDecoder ( ) . decode ( value ) ;
117+
118+ if ( done ) {
119+ progressBarInstallation . style . width = '100%' ;
120+ progressBarInstallation . innerText = '100%' ;
121+ progressBarInstallation . classList . remove ( 'progress-bar-animated' ) ;
122+ const alert = document . getElementById ( 'phpmyfaq-update-database-success' ) ;
123+ alert . classList . remove ( 'd-none' ) ;
124+ return ;
125+ } else {
126+ progressBarInstallation . style . width = JSON . parse ( decodedValue ) . progress ;
127+ progressBarInstallation . innerText = JSON . parse ( decodedValue ) . progress ;
128+ }
129+
130+ await pump ( ) ;
131+ }
132+
133+ await pump ( ) ;
134+ } catch ( error ) {
135+ const errorMessage =
136+ error . cause && error . cause . response ? await error . cause . response . json ( ) : { error : 'Unknown error' } ;
137+ const alert = document . getElementById ( 'phpmyfaq-update-database-error' ) ;
138+
139+ alert . classList . remove ( 'd-none' ) ;
140+ alert . innerHTML = errorMessage . error ;
141+ }
139142 }
140143} ;
0 commit comments