| 1 |
import lpModalOverlay from '../../../../utils/lp-modal-overlay'; |
| 2 |
import handleAjax from '../../../../utils/handle-ajax-api'; |
| 3 |
|
| 4 |
const $ = jQuery; |
| 5 |
|
| 6 |
const elToolUpgradeDB = $( '#lp-tool-upgrade-db' ); |
| 7 |
|
| 8 |
const upgradeDB = () => { |
| 9 |
let isUpgrading = 0; |
| 10 |
const elWrapperTermsUpgrade = elToolUpgradeDB.find( '.wrapper-terms-upgrade' ); |
| 11 |
const elStatusUpgrade = elToolUpgradeDB.find( '.wrapper-lp-status-upgrade' ); |
| 12 |
const elWrapperUpgradeMessage = elToolUpgradeDB.find( '.wrapper-lp-upgrade-message' ); |
| 13 |
let checkValidBeforeUpgrade = null; |
| 14 |
|
| 15 |
if ( elWrapperTermsUpgrade.length ) { // Show Terms Upgrade. |
| 16 |
lpModalOverlay.setContentModal( elWrapperTermsUpgrade.html() ); |
| 17 |
|
| 18 |
const elTermUpdate = lpModalOverlay.elLPOverlay.find( '.terms-upgrade' ); |
| 19 |
const elLPAgreeTerm = elTermUpdate.find( 'input[name=lp-agree-term]' ); |
| 20 |
const elTermMessage = elTermUpdate.find( '.error' ); |
| 21 |
const elMessageUpgrading = $( 'input[name=message-when-upgrading]' ).val(); |
| 22 |
|
| 23 |
checkValidBeforeUpgrade = function() { |
| 24 |
elTermMessage.hide(); |
| 25 |
elTermMessage.removeClass( 'learn-press-message' ); |
| 26 |
|
| 27 |
if ( elLPAgreeTerm.is( ':checked' ) ) { |
| 28 |
handleAjax( '/lp/v1/database/agree_terms', { agree_terms: 1 }, {} ); |
| 29 |
|
| 30 |
lpModalOverlay.elFooter.find( '.learn-press-notice' ).remove(); |
| 31 |
lpModalOverlay.elFooter.prepend( '<span class="learn-press-notice">' + elMessageUpgrading + '</span>' ); |
| 32 |
lpModalOverlay.setContentModal( elStatusUpgrade.html() ); |
| 33 |
|
| 34 |
return true; |
| 35 |
} |
| 36 |
|
| 37 |
elTermMessage.show(); |
| 38 |
elTermMessage.addClass( 'learn-press-message' ); |
| 39 |
lpModalOverlay.elMainContent.animate( { |
| 40 |
scrollTop: elTermMessage.offset().top, |
| 41 |
} ); |
| 42 |
|
| 43 |
return false; |
| 44 |
}; |
| 45 |
} else { // Show Steps Upgrade. |
| 46 |
lpModalOverlay.setContentModal( elStatusUpgrade.html() ); |
| 47 |
checkValidBeforeUpgrade = function() { |
| 48 |
return true; |
| 49 |
}; |
| 50 |
} |
| 51 |
|
| 52 |
lpModalOverlay.setTitleModal( elToolUpgradeDB.find( 'h2' ).html() ); |
| 53 |
lpModalOverlay.elBtnYes.text( 'Upgrade' ); |
| 54 |
lpModalOverlay.elBtnYes.show(); |
| 55 |
lpModalOverlay.elBtnNo.text( 'close' ); |
| 56 |
lpModalOverlay.callBackYes = function() { |
| 57 |
if ( ! checkValidBeforeUpgrade() ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
isUpgrading = 1; |
| 62 |
|
| 63 |
lpModalOverlay.elBtnYes.hide(); |
| 64 |
lpModalOverlay.elBtnNo.hide(); |
| 65 |
|
| 66 |
const urlHandle = '/lp/v1/database/upgrade'; |
| 67 |
const elGroupStep = lpModalOverlay.elLPOverlay.find( '.lp-group-step' ); |
| 68 |
const elItemSteps = elToolUpgradeDB.find( '.lp-item-step' ); |
| 69 |
|
| 70 |
// Get params. |
| 71 |
const steps = []; |
| 72 |
|
| 73 |
$.each( elItemSteps, function( i, el ) { |
| 74 |
const elItemStepsTmp = $( el ); |
| 75 |
|
| 76 |
if ( ! elItemStepsTmp.hasClass( 'completed' ) ) { |
| 77 |
const step = elItemStepsTmp.find( 'input' ).val(); |
| 78 |
steps.push( step ); |
| 79 |
} |
| 80 |
} ); |
| 81 |
|
| 82 |
const params = { |
| 83 |
steps, |
| 84 |
step: steps[ 0 ], |
| 85 |
}; |
| 86 |
|
| 87 |
let elItemStepCurrent = null; |
| 88 |
|
| 89 |
// Show progress when upgrading. |
| 90 |
const showProgress = ( stepCurrent, percent ) => { |
| 91 |
elItemStepCurrent = elGroupStep.find( 'input[value=' + stepCurrent + ']' ).closest( '.lp-item-step' ); |
| 92 |
elItemStepCurrent.addClass( 'running' ); |
| 93 |
|
| 94 |
if ( 100 === percent ) { |
| 95 |
elItemStepCurrent.removeClass( 'running' ).addClass( 'completed' ); |
| 96 |
} |
| 97 |
|
| 98 |
elItemStepCurrent.find( '.progress-bar' ).css( 'width', percent + '%' ); |
| 99 |
elItemStepCurrent.find( '.percent' ).text( percent + '%' ); |
| 100 |
}; |
| 101 |
|
| 102 |
// Scroll to step current. |
| 103 |
const scrollToStepCurrent = ( stepCurrent ) => { |
| 104 |
elItemStepCurrent = elGroupStep.find( 'input[value=' + stepCurrent + ']' ).closest( '.lp-item-step' ); |
| 105 |
|
| 106 |
const offset = elItemStepCurrent.offset().top - lpModalOverlay.elMainContent.offset().top + |
| 107 |
lpModalOverlay.elMainContent.scrollTop(); |
| 108 |
|
| 109 |
lpModalOverlay.elMainContent.stop().animate( { |
| 110 |
scrollTop: offset, |
| 111 |
}, 600 ); |
| 112 |
}; |
| 113 |
|
| 114 |
showProgress( steps[ 0 ], 0.1 ); |
| 115 |
|
| 116 |
const funcCallBack = { |
| 117 |
success: ( res ) => { |
| 118 |
showProgress( params.step, res.percent ); |
| 119 |
|
| 120 |
if ( params.step !== res.name ) { |
| 121 |
showProgress( res.name, 0.1 ); |
| 122 |
} |
| 123 |
|
| 124 |
scrollToStepCurrent( params.step ); |
| 125 |
|
| 126 |
if ( 'success' === res.status ) { |
| 127 |
params.step = res.name; |
| 128 |
params.data = res.data; |
| 129 |
|
| 130 |
setTimeout( () => { |
| 131 |
handleAjax( urlHandle, params, funcCallBack ); |
| 132 |
}, 800 ); |
| 133 |
} else if ( 'finished' === res.status ) { |
| 134 |
isUpgrading = 0; |
| 135 |
elItemStepCurrent.removeClass( 'running' ).addClass( 'completed' ); |
| 136 |
setTimeout( () => { |
| 137 |
lpModalOverlay.setContentModal( elWrapperUpgradeMessage.html() ); |
| 138 |
}, 1000 ); |
| 139 |
lpModalOverlay.elFooter.find( '.learn-press-notice' ).remove(); |
| 140 |
lpModalOverlay.elBtnNo.show(); |
| 141 |
lpModalOverlay.elBtnNo.on( 'click', () => { |
| 142 |
window.location.reload(); |
| 143 |
} ); |
| 144 |
} else { |
| 145 |
isUpgrading = 0; |
| 146 |
lpModalOverlay.elFooter.find( '.learn-press-notice' ).remove(); |
| 147 |
elItemStepCurrent.removeClass( 'running' ).addClass( 'error' ); |
| 148 |
lpModalOverlay.setContentModal( elWrapperUpgradeMessage.html(), function() { |
| 149 |
lpModalOverlay.elBtnYes.text( 'Retry' ).show(); |
| 150 |
lpModalOverlay.callBackYes = () => { |
| 151 |
window.location.href = lpGlobalSettings.siteurl + '/wp-admin/admin.php?page=learn-press-tools&tab=database&action=upgrade-db'; |
| 152 |
}; |
| 153 |
lpModalOverlay.elBtnNo.show(); |
| 154 |
|
| 155 |
if ( ! res.message ) { |
| 156 |
res.message = 'Upgrade not success! Please clear cache, restart sever then retry or contact to LP to help'; |
| 157 |
} |
| 158 |
|
| 159 |
lpModalOverlay.elMainContent.find( '.learn-press-message' ).addClass( 'error' ).html( res.message ); |
| 160 |
} ); |
| 161 |
} |
| 162 |
}, |
| 163 |
error: ( err ) => { |
| 164 |
isUpgrading = 0; |
| 165 |
lpModalOverlay.setContentModal( elWrapperUpgradeMessage.html(), function() { |
| 166 |
lpModalOverlay.elBtnYes.text( 'Retry' ).show(); |
| 167 |
lpModalOverlay.callBackYes = () => { |
| 168 |
window.location.location = 'wp-admin/admin.php?page=learn-press-tools&tab=database&action=upgrade-db'; |
| 169 |
}; |
| 170 |
lpModalOverlay.elBtnNo.show(); |
| 171 |
|
| 172 |
if ( ! err.message ) { |
| 173 |
err.message = 'Upgrade not success! Something wrong. Please clear cache, restart sever then retry or contact to LP to help'; |
| 174 |
} |
| 175 |
|
| 176 |
lpModalOverlay.elMainContent.find( '.learn-press-message' ).addClass( 'error' ).html( err.message ); |
| 177 |
} ); |
| 178 |
}, |
| 179 |
completed: () => { |
| 180 |
|
| 181 |
}, |
| 182 |
}; |
| 183 |
|
| 184 |
handleAjax( urlHandle, params, funcCallBack ); |
| 185 |
}; |
| 186 |
|
| 187 |
// Show confirm if, within upgrading, the user reload the page. |
| 188 |
window.onbeforeunload = function() { |
| 189 |
if ( isUpgrading ) { |
| 190 |
return 'LP is upgrading Database. Are you want to reload page?'; |
| 191 |
} |
| 192 |
}; |
| 193 |
|
| 194 |
// Show confirm if, within upgrading, the user close the page. |
| 195 |
window.onclose = function() { |
| 196 |
if ( isUpgrading ) { |
| 197 |
return 'LP is upgrading Database. Are you want to close page?'; |
| 198 |
} |
| 199 |
}; |
| 200 |
}; |
| 201 |
|
| 202 |
const getStepsUpgradeStatus = () => { |
| 203 |
if ( ! elToolUpgradeDB.length ) { |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
// initial LP Modal Overlay |
| 208 |
if ( ! lpModalOverlay.init() ) { |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
const elWrapperStatusUpgrade = $( '.wrapper-lp-status-upgrade' ); |
| 213 |
const urlHandle = '/lp/v1/database/get_steps'; |
| 214 |
|
| 215 |
// Show dialog upgrade database. |
| 216 |
const queryString = window.location.search; |
| 217 |
const urlParams = new URLSearchParams( queryString ); |
| 218 |
const action = urlParams.get( 'action' ); |
| 219 |
|
| 220 |
if ( 'upgrade-db' === action ) { |
| 221 |
lpModalOverlay.elLPOverlay.show(); |
| 222 |
lpModalOverlay.setTitleModal( elToolUpgradeDB.find( 'h2' ).html() ); |
| 223 |
lpModalOverlay.setContentModal( $( '.wrapper-lp-loading' ).html() ); |
| 224 |
} |
| 225 |
|
| 226 |
const funcCallBack = { |
| 227 |
success: ( res ) => { |
| 228 |
const { steps_completed, steps_default } = res; |
| 229 |
|
| 230 |
if ( undefined === steps_completed || undefined === steps_default ) { |
| 231 |
console.log( 'invalid steps_completed and steps_default' ); |
| 232 |
return false; |
| 233 |
} |
| 234 |
|
| 235 |
// Render show Steps. |
| 236 |
let htmlStep = ''; |
| 237 |
for ( const k_gr_steps in steps_default ) { |
| 238 |
const step_group = steps_default[ k_gr_steps ]; |
| 239 |
const steps = step_group.steps; |
| 240 |
|
| 241 |
htmlStep = '<div class="lp-group-step">'; |
| 242 |
htmlStep += '<h3>' + step_group.label + '</h3>'; |
| 243 |
|
| 244 |
for ( const k_step in steps ) { |
| 245 |
const step = steps[ k_step ]; |
| 246 |
let completed = ''; |
| 247 |
|
| 248 |
if ( undefined !== steps_completed[ k_step ] ) { |
| 249 |
completed = 'completed'; |
| 250 |
} |
| 251 |
|
| 252 |
htmlStep += '<div class="lp-item-step ' + completed + '">'; |
| 253 |
htmlStep += '<div class="lp-item-step-left"><input type="hidden" name="lp_steps_upgrade_db[]" value="' + step.name + '" /></div>'; |
| 254 |
htmlStep += '<div class="lp-item-step-right">'; |
| 255 |
htmlStep += '<label for=""><strong></strong>' + step.label + '</label>'; |
| 256 |
htmlStep += '<div class="description">' + step.description + '</div>'; |
| 257 |
htmlStep += '<div class="percent"></div>'; |
| 258 |
htmlStep += '<span class="progress-bar"></span>'; |
| 259 |
htmlStep += '</div>'; |
| 260 |
htmlStep += '</div>'; |
| 261 |
} |
| 262 |
|
| 263 |
htmlStep += '</div>'; |
| 264 |
|
| 265 |
elWrapperStatusUpgrade.append( htmlStep ); |
| 266 |
|
| 267 |
const elBtnUpgradeDB = $( '.lp-btn-upgrade-db' ); |
| 268 |
|
| 269 |
if ( 'upgrade-db' === action ) { |
| 270 |
upgradeDB(); |
| 271 |
} |
| 272 |
|
| 273 |
elBtnUpgradeDB.on( 'click', function() { |
| 274 |
lpModalOverlay.elLPOverlay.show(); |
| 275 |
upgradeDB(); |
| 276 |
} ); |
| 277 |
} |
| 278 |
}, |
| 279 |
error: ( err ) => { |
| 280 |
|
| 281 |
}, |
| 282 |
completed: () => { |
| 283 |
|
| 284 |
}, |
| 285 |
}; |
| 286 |
|
| 287 |
handleAjax( urlHandle, {}, funcCallBack ); |
| 288 |
}; |
| 289 |
|
| 290 |
export default getStepsUpgradeStatus; |
| 291 |
|