2019-10-08 22:02:26 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { getResourcePrefix } from '../utils';
|
|
|
|
import { NAMESPACE } from '../constants';
|
|
|
|
|
|
|
|
function update( resourceNames, data, fetch = apiFetch ) {
|
|
|
|
return [ ...initiateExport( resourceNames, data, fetch ) ];
|
|
|
|
}
|
|
|
|
|
|
|
|
function initiateExport( resourceNames, data, fetch ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
const filteredNames = resourceNames.filter( ( name ) => {
|
2019-10-08 22:02:26 +00:00
|
|
|
return name.startsWith( 'report-export-' );
|
|
|
|
} );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
return filteredNames.map( async ( resourceName ) => {
|
2019-10-08 22:02:26 +00:00
|
|
|
const prefix = getResourcePrefix( resourceName );
|
|
|
|
const reportType = prefix.split( '-' ).pop();
|
|
|
|
const url = NAMESPACE + '/reports/' + reportType + '/export';
|
|
|
|
|
|
|
|
try {
|
|
|
|
const result = await fetch( {
|
|
|
|
path: url,
|
|
|
|
method: 'POST',
|
|
|
|
data: {
|
|
|
|
report_args: data[ resourceName ],
|
|
|
|
email: true,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
return {
|
|
|
|
[ resourceName ]: { [ result.status ]: result.message },
|
|
|
|
};
|
|
|
|
} catch ( error ) {
|
|
|
|
return { [ resourceName ]: { error } };
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
update,
|
|
|
|
};
|