1313 * @link https://www.phpmyfaq.de
1414 * @since 2023-07-11
1515 */
16+
1617import { addElement } from '../../../../assets/src/utils' ;
18+ import { fetchHealthCheck } from '../api' ;
1719
1820export const handleCheckForUpdates = ( ) => {
1921 const checkHealthButton = document . getElementById ( 'pmf-button-check-health' ) ;
@@ -24,37 +26,29 @@ export const handleCheckForUpdates = () => {
2426
2527 // Health Check
2628 if ( checkHealthButton ) {
27- checkHealthButton . addEventListener ( 'click' , ( event ) => {
29+ checkHealthButton . addEventListener ( 'click' , async ( event ) => {
2830 event . preventDefault ( ) ;
29- fetch ( window . location . pathname + 'api/health-check' , {
30- method : 'POST' ,
31- headers : {
32- Accept : 'application/json, text/plain, */*' ,
33- 'Content-Type' : 'application/json' ,
34- } ,
35- } )
36- . then ( async ( response ) => {
37- if ( response . ok ) {
38- return response . json ( ) ;
39- }
40- throw new Error ( 'Network response was not ok: ' , { cause : { response } } ) ;
41- } )
42- . then ( ( response ) => {
43- const result = document . getElementById ( 'result-check-health' ) ;
44- const card = document . getElementById ( 'pmf-update-step-health-check' ) ;
45- if ( result ) {
46- card . classList . add ( 'text-bg-success' ) ;
47- if ( response . success === 'ok' ) {
48- result . replaceWith ( addElement ( 'p' , { innerText : response . message } ) ) ;
49- } else {
50- result . replaceWith ( addElement ( 'p' , { innerText : response . message } ) ) ;
51- }
52- }
53- } )
54- . catch ( async ( error ) => {
31+ try {
32+ const responseData = await fetchHealthCheck ( ) ;
33+ const result = document . getElementById ( 'result-check-health' ) ;
34+ const card = document . getElementById ( 'pmf-update-step-health-check' ) ;
35+
36+ if ( responseData . success ) {
37+ card . classList . add ( 'text-bg-success' ) ;
38+ result . replaceWith ( addElement ( 'p' , { innerText : responseData . success } ) ) ;
39+ }
40+ if ( responseData . error ) {
41+ card . classList . add ( 'text-bg-danger' ) ;
42+ result . replaceWith ( addElement ( 'p' , { innerText : responseData . error } ) ) ;
43+ }
44+ } catch ( error ) {
45+ if ( error . cause && error . cause . response ) {
5546 const errorMessage = await error . cause . response . json ( ) ;
5647 console . error ( errorMessage ) ;
57- } ) ;
48+ } else {
49+ console . error ( error . message ) ;
50+ }
51+ }
5852 } ) ;
5953 }
6054
0 commit comments