| 1 |
import * as lpUtils from 'lpAssetsJsPath/utils.js'; |
| 2 |
import * as lpToastify from 'lpAssetsJsPath/lpToastify'; |
| 3 |
|
| 4 |
export const ExportOrdersToCSV = () => { |
| 5 |
const handleExport = ( args ) => { |
| 6 |
const { e, target } = args; |
| 7 |
e.preventDefault(); |
| 8 |
lpUtils.lpSetLoadingEl( target, true ); |
| 9 |
let dataSend = JSON.parse( target.dataset.send ); |
| 10 |
|
| 11 |
const callBack = { |
| 12 |
success: ( response ) => { |
| 13 |
const { message, status, data } = response; |
| 14 |
|
| 15 |
if ( status === 'success' ) { |
| 16 |
const maxPage = data?.max_page; |
| 17 |
const nextPage = data?.next_page; |
| 18 |
const done = data?.done; |
| 19 |
const downloadUrl = data?.download_url; |
| 20 |
|
| 21 |
if ( !! maxPage && !! nextPage && nextPage <= maxPage ) { |
| 22 |
dataSend = { ...dataSend, paged: nextPage }; |
| 23 |
window.lpAJAXG.fetchAJAX( dataSend, callBack ); |
| 24 |
} else if ( !! done ) { |
| 25 |
lpToastify.show( message, 'success' ); |
| 26 |
|
| 27 |
const link = document.createElement( 'a' ); |
| 28 |
link.href = downloadUrl; |
| 29 |
link.target = '_self'; |
| 30 |
document.body.appendChild( link ); |
| 31 |
link.click(); |
| 32 |
document.body.removeChild( link ); |
| 33 |
} |
| 34 |
} else { |
| 35 |
throw message; |
| 36 |
} |
| 37 |
}, |
| 38 |
error: ( error ) => { |
| 39 |
lpToastify.show( error, 'error' ); |
| 40 |
}, |
| 41 |
completed: () => { |
| 42 |
lpUtils.lpSetLoadingEl( target, false ); |
| 43 |
}, |
| 44 |
}; |
| 45 |
|
| 46 |
window.lpAJAXG.fetchAJAX( dataSend, callBack ); |
| 47 |
}; |
| 48 |
|
| 49 |
lpUtils.eventHandlers( 'click', [ |
| 50 |
{ |
| 51 |
selector: '.lp-btn-export-order-to-csv', |
| 52 |
class: this, |
| 53 |
callBack: handleExport, |
| 54 |
}, |
| 55 |
] ); |
| 56 |
}; |
| 57 |
|