| 1 |
import { lpSetLoadingEl } from '../../utils.js'; |
| 2 |
import Toastify from 'toastify-js'; |
| 3 |
|
| 4 |
/** |
| 5 |
* JS Recover order |
| 6 |
* |
| 7 |
* @since 4.0.0 |
| 8 |
* @version 1.0.1 |
| 9 |
*/ |
| 10 |
const recoverOrder = () => { |
| 11 |
const toastify = Toastify( { |
| 12 |
gravity: lpData.toast.gravity, // `top` or `bottom` |
| 13 |
position: lpData.toast.position, // `left`, `center` or `right` |
| 14 |
close: lpData.toast.close == 1, |
| 15 |
className: `${ lpData.toast.classPrefix }`, |
| 16 |
stopOnFocus: lpData.toast.stopOnFocus == 1, |
| 17 |
duration: lpData.toast.duration, |
| 18 |
} ); |
| 19 |
|
| 20 |
// Events |
| 21 |
document.addEventListener( 'submit', ( e ) => { |
| 22 |
const target = e.target; |
| 23 |
if ( target.classList.contains( 'lp-order-recover' ) ) { |
| 24 |
e.preventDefault(); |
| 25 |
ajaxRecover( target ); |
| 26 |
} |
| 27 |
} ); |
| 28 |
|
| 29 |
const ajaxRecover = ( form ) => { |
| 30 |
const status = 'error'; |
| 31 |
const btnSubmit = form.querySelector( '.button-recover-order' ); |
| 32 |
if ( ! btnSubmit ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
lpSetLoadingEl( btnSubmit, 1 ); |
| 37 |
|
| 38 |
const url = new URL( window.location.href ); |
| 39 |
fetch( url, { |
| 40 |
method: 'POST', |
| 41 |
body: new FormData( form ), |
| 42 |
} ) |
| 43 |
.then( ( response ) => { |
| 44 |
return response.json(); |
| 45 |
} ) |
| 46 |
.then( ( res ) => { |
| 47 |
const { status, data: { redirect }, message } = res; |
| 48 |
|
| 49 |
if ( status === 'success' ) { |
| 50 |
toastify.options.text = message; |
| 51 |
toastify.options.className += ` ${ status }`; |
| 52 |
toastify.showToast(); |
| 53 |
if ( redirect ) { |
| 54 |
window.location.href = redirect; |
| 55 |
} |
| 56 |
btnSubmit.remove(); |
| 57 |
} else { |
| 58 |
lpSetLoadingEl( btnSubmit, 0 ); |
| 59 |
throw new Error( message ); |
| 60 |
} |
| 61 |
} ).finally( () => { |
| 62 |
|
| 63 |
} ).catch( ( err ) => { |
| 64 |
toastify.options.text = err.message; |
| 65 |
toastify.options.className += ` ${ status }`; |
| 66 |
toastify.showToast(); |
| 67 |
} ); |
| 68 |
}; |
| 69 |
}; |
| 70 |
|
| 71 |
export default recoverOrder; |
| 72 |
|
| 73 |
|