| 1 |
export default function HTTP( options ) { |
| 2 |
const $ = window.jQuery || jQuery; |
| 3 |
const $VueHTTP = Vue.http; |
| 4 |
|
| 5 |
options = $.extend( { |
| 6 |
ns: 'LPRequest', |
| 7 |
store: false, |
| 8 |
}, options || {} ); |
| 9 |
|
| 10 |
let $publishingAction = null; |
| 11 |
|
| 12 |
LP.Request = function( payload ) { |
| 13 |
$publishingAction = $( '#publishing-action' ); |
| 14 |
|
| 15 |
payload.id = options.store.getters.id; |
| 16 |
payload.nonce = options.store.getters.nonce; |
| 17 |
payload[ 'lp-ajax' ] = options.store.getters.action; |
| 18 |
//payload.code = options.store.getters.code; |
| 19 |
|
| 20 |
$publishingAction.find( '#publish' ).addClass( 'disabled' ); |
| 21 |
$publishingAction.find( '.spinner' ).addClass( 'is-active' ); |
| 22 |
$publishingAction.addClass( 'code-' + payload.code ); |
| 23 |
|
| 24 |
return $VueHTTP.post( options.store.getters.urlAjax, |
| 25 |
payload, |
| 26 |
{ |
| 27 |
emulateJSON: true, |
| 28 |
params: { |
| 29 |
namespace: options.ns, |
| 30 |
code: payload.code, |
| 31 |
}, |
| 32 |
} ); |
| 33 |
}; |
| 34 |
|
| 35 |
$VueHTTP.interceptors.push( function( request, next ) { |
| 36 |
if ( request.params.namespace !== options.ns ) { |
| 37 |
next(); |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
options.store.dispatch( 'newRequest' ); |
| 42 |
|
| 43 |
next( function( response ) { |
| 44 |
if ( ! jQuery.isPlainObject( response.body ) ) { |
| 45 |
response.body = LP.parseJSON( response.body ); |
| 46 |
} |
| 47 |
|
| 48 |
const body = response.body; |
| 49 |
const result = body.success || false; |
| 50 |
|
| 51 |
if ( result ) { |
| 52 |
options.store.dispatch( 'requestCompleted', 'successful' ); |
| 53 |
} else { |
| 54 |
options.store.dispatch( 'requestCompleted', 'failed' ); |
| 55 |
} |
| 56 |
|
| 57 |
$publishingAction.removeClass( 'code-' + request.params.code ); |
| 58 |
|
| 59 |
if ( ! $publishingAction.attr( 'class' ) ) { |
| 60 |
$publishingAction.find( '#publish' ).removeClass( 'disabled' ); |
| 61 |
$publishingAction.find( '.spinner' ).removeClass( 'is-active' ); |
| 62 |
} |
| 63 |
} ); |
| 64 |
} ); |
| 65 |
} |
| 66 |
|