2020-11-06 01:21:05 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { SETTINGS_STORE_NAME, ITEMS_STORE_NAME } from '@woocommerce/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { DEFAULT_ACTIONABLE_STATUSES } from '../../../analytics/settings/config';
|
|
|
|
|
2020-11-06 16:50:24 +00:00
|
|
|
export function getUnreadOrders( select, orderStatuses ) {
|
2020-11-06 01:21:05 +00:00
|
|
|
const { getItemsTotalCount, getItemsError, isResolving } = select(
|
|
|
|
ITEMS_STORE_NAME
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( ! orderStatuses.length ) {
|
2020-11-11 20:28:44 +00:00
|
|
|
return 0;
|
2020-11-06 01:21:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ordersQuery = {
|
|
|
|
page: 1,
|
|
|
|
per_page: 1, // Core endpoint requires per_page > 0.
|
|
|
|
status: orderStatuses,
|
|
|
|
_fields: [ 'id' ],
|
|
|
|
};
|
|
|
|
|
2020-11-18 18:59:46 +00:00
|
|
|
const defaultValue = null;
|
|
|
|
|
2020-11-25 18:51:15 +00:00
|
|
|
// Disable eslint rule requiring `totalOrders` to be defined below because the next two statements
|
2020-11-06 01:21:05 +00:00
|
|
|
// depend on `getItemsTotalCount` to have been called.
|
|
|
|
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
|
2020-11-18 18:59:46 +00:00
|
|
|
const totalOrders = getItemsTotalCount(
|
|
|
|
'orders',
|
|
|
|
ordersQuery,
|
|
|
|
defaultValue
|
|
|
|
);
|
2020-11-06 01:21:05 +00:00
|
|
|
const isError = Boolean( getItemsError( 'orders', ordersQuery ) );
|
|
|
|
const isRequesting = isResolving( 'getItemsTotalCount', [
|
|
|
|
'orders',
|
|
|
|
ordersQuery,
|
2020-11-18 18:59:46 +00:00
|
|
|
defaultValue,
|
2020-11-06 01:21:05 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
if ( isError || isRequesting ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalOrders;
|
|
|
|
}
|
2020-11-06 16:50:24 +00:00
|
|
|
|
|
|
|
export function getOrderStatuses( select ) {
|
|
|
|
const { getSetting: getMutableSetting } = select( SETTINGS_STORE_NAME );
|
|
|
|
const {
|
|
|
|
woocommerce_actionable_order_statuses: orderStatuses = DEFAULT_ACTIONABLE_STATUSES,
|
|
|
|
} = getMutableSetting( 'wc_admin', 'wcAdminSettings', {} );
|
|
|
|
return orderStatuses;
|
|
|
|
}
|
2020-11-25 18:51:15 +00:00
|
|
|
|
|
|
|
export function getLowStockCount( select ) {
|
|
|
|
const { getItemsTotalCount, getItemsError, isResolving } = select(
|
|
|
|
ITEMS_STORE_NAME
|
|
|
|
);
|
|
|
|
|
|
|
|
const productsQuery = {
|
|
|
|
page: 1,
|
|
|
|
per_page: 1,
|
|
|
|
low_in_stock: true,
|
|
|
|
status: 'publish',
|
|
|
|
_fields: [ 'id' ],
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultValue = 0;
|
|
|
|
|
|
|
|
// Disable eslint rule requiring `totalLowStockProducts` to be defined below because the next two statements
|
|
|
|
// depend on `getItemsTotalCount` to have been called.
|
|
|
|
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
|
|
|
|
const totalLowStockProducts = getItemsTotalCount(
|
|
|
|
'products',
|
|
|
|
productsQuery,
|
|
|
|
defaultValue
|
|
|
|
);
|
|
|
|
|
|
|
|
const isError = Boolean( getItemsError( 'products', productsQuery ) );
|
|
|
|
const isRequesting = isResolving( 'getItemsTotalCount', [
|
|
|
|
'products',
|
|
|
|
productsQuery,
|
|
|
|
defaultValue,
|
|
|
|
] );
|
|
|
|
|
|
|
|
if ( isError || isRequesting ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalLowStockProducts;
|
|
|
|
}
|