| 1 |
/** |
| 2 |
* Statistics dashboard fetch wrapper + escaping helper. |
| 3 |
* |
| 4 |
* Every statistics request goes through lpStatsFetch so the localized globals |
| 5 |
* (lpDataAdmin for REST root/nonce, lpAdminStatisticSettings for config) are |
| 6 |
* read in exactly one file, and lpFetchAPI's blind spots are normalized here: |
| 7 |
* it never rejects on HTTP error codes, so anything but status 'success' |
| 8 |
* is routed to the error callback. |
| 9 |
* |
| 10 |
* @since 4.4.2 |
| 11 |
* @version 1.0.0 |
| 12 |
*/ |
| 13 |
|
| 14 |
import * as lpUtils from 'lpAssetsJsPath/utils.js'; |
| 15 |
import { lpStatsState, LP_STATS_RANGE_RESOLVED } from './state.js'; |
| 16 |
|
| 17 |
export const getStatsConfig = () => window.lpAdminStatisticSettings || {}; |
| 18 |
|
| 19 |
export const getStatsI18n = ( key, fallback = '' ) => { |
| 20 |
const { i18n = {} } = getStatsConfig(); |
| 21 |
return i18n[ key ] || fallback; |
| 22 |
}; |
| 23 |
|
| 24 |
/** |
| 25 |
* Fetch a statistics endpoint with the global filter state applied. |
| 26 |
* |
| 27 |
* @param {string} endpoint Route below the statistics namespace, e.g. 'filter-options'. |
| 28 |
* @param {Object} extraArgs Query args merged over the state (tab-specific params). |
| 29 |
* @param {Object} functions { before, success, error, completed } — success only |
| 30 |
* fires on status 'success'; error receives an Error. |
| 31 |
*/ |
| 32 |
export const lpStatsFetch = ( endpoint, extraArgs = {}, functions = {} ) => { |
| 33 |
const lpDataAdmin = window.lpDataAdmin || {}; |
| 34 |
const restNamespace = getStatsConfig().restNamespace || 'lp/v1/statistics'; |
| 35 |
const url = lpUtils.lpAddQueryArgs( |
| 36 |
`${ lpDataAdmin.lp_rest_url || '/wp-json/' }${ restNamespace }/${ endpoint }`, |
| 37 |
{ ...lpStatsState.get(), ...extraArgs } |
| 38 |
); |
| 39 |
|
| 40 |
const onError = |
| 41 |
'function' === typeof functions.error |
| 42 |
? functions.error |
| 43 |
: ( err ) => console.error( 'LP Statistics:', err ); |
| 44 |
|
| 45 |
lpUtils.lpFetchAPI( |
| 46 |
url, |
| 47 |
{ headers: { 'X-WP-Nonce': lpDataAdmin.nonce || '' } }, |
| 48 |
{ |
| 49 |
...functions, |
| 50 |
success: ( response ) => { |
| 51 |
if ( response && 'success' === response.status ) { |
| 52 |
// Broadcast the server-resolved range so the filter bar can |
| 53 |
// reconcile its toggle label ( fixes the past-midnight case ). |
| 54 |
const range = response.data && response.data.range; |
| 55 |
if ( range && range.label ) { |
| 56 |
document.dispatchEvent( |
| 57 |
new CustomEvent( LP_STATS_RANGE_RESOLVED, { detail: range } ) |
| 58 |
); |
| 59 |
} |
| 60 |
if ( 'function' === typeof functions.success ) { |
| 61 |
functions.success( response ); |
| 62 |
} |
| 63 |
} else { |
| 64 |
onError( |
| 65 |
new Error( |
| 66 |
( response && response.message ) || |
| 67 |
getStatsI18n( 'loadError', 'Request failed.' ) |
| 68 |
) |
| 69 |
); |
| 70 |
} |
| 71 |
}, |
| 72 |
error: onError, |
| 73 |
} |
| 74 |
); |
| 75 |
}; |
| 76 |
|