2020-03-25 03:20:17 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
2020-06-10 16:46:46 +00:00
|
|
|
import { SETTINGS_STORE_NAME, USER_STORE_NAME } from '@woocommerce/data';
|
2020-03-25 03:20:17 +00:00
|
|
|
|
2019-03-29 02:45:19 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-03-25 03:20:17 +00:00
|
|
|
import { DEFAULT_ACTIONABLE_STATUSES } from 'analytics/settings/config';
|
2019-10-07 11:51:25 +00:00
|
|
|
import { getSetting } from '@woocommerce/wc-admin-settings';
|
2020-05-28 08:52:25 +00:00
|
|
|
import { QUERY_DEFAULTS } from 'wc-api/constants';
|
2020-06-22 14:53:36 +00:00
|
|
|
import { getUnreadNotesCount } from './panels/inbox/utils';
|
2019-03-29 02:45:19 +00:00
|
|
|
|
|
|
|
export function getUnreadNotes( select ) {
|
2020-06-22 14:53:36 +00:00
|
|
|
const { getNotes, getNotesError, isGetNotesRequesting } = select(
|
|
|
|
'wc-api'
|
|
|
|
);
|
2020-06-10 16:46:46 +00:00
|
|
|
|
|
|
|
const { getCurrentUser } = select( USER_STORE_NAME );
|
|
|
|
const userData = getCurrentUser();
|
|
|
|
const lastRead = parseInt(
|
|
|
|
userData &&
|
2020-06-22 14:53:36 +00:00
|
|
|
userData.woocommerce_meta &&
|
|
|
|
userData.woocommerce_meta.activity_panel_inbox_last_read,
|
2020-06-10 16:46:46 +00:00
|
|
|
10
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( ! lastRead ) {
|
2019-09-23 21:47:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
2020-06-22 14:53:36 +00:00
|
|
|
|
|
|
|
// @todo This method would be more performant if we ask only for 1 item per page with status "unactioned".
|
|
|
|
// This change should be applied after having pagination implemented.
|
2019-03-29 02:45:19 +00:00
|
|
|
const notesQuery = {
|
|
|
|
page: 1,
|
2020-06-22 14:53:36 +00:00
|
|
|
per_page: QUERY_DEFAULTS.pageSize,
|
2020-07-09 04:49:13 +00:00
|
|
|
status: 'unactioned',
|
2020-05-28 08:52:25 +00:00
|
|
|
type: QUERY_DEFAULTS.noteTypes,
|
2019-04-01 02:53:34 +00:00
|
|
|
orderby: 'date',
|
|
|
|
order: 'desc',
|
2019-03-29 02:45:19 +00:00
|
|
|
};
|
|
|
|
|
2020-06-22 14:53:36 +00:00
|
|
|
// Disable eslint rule requiring `latestNotes` to be defined below because the next two statements
|
2020-02-14 02:23:21 +00:00
|
|
|
// depend on `getNotes` to have been called.
|
|
|
|
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
|
2020-06-22 14:53:36 +00:00
|
|
|
const latestNotes = getNotes( notesQuery );
|
2019-04-09 07:50:00 +00:00
|
|
|
const isError = Boolean( getNotesError( notesQuery ) );
|
|
|
|
const isRequesting = isGetNotesRequesting( notesQuery );
|
|
|
|
|
|
|
|
if ( isError || isRequesting ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-06-22 14:53:36 +00:00
|
|
|
const unreadNotesCount = getUnreadNotesCount( latestNotes, lastRead );
|
|
|
|
|
|
|
|
return unreadNotesCount > 0;
|
2019-03-29 02:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getUnreadOrders( select ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
const {
|
|
|
|
getItems,
|
|
|
|
getItemsTotalCount,
|
|
|
|
getItemsError,
|
|
|
|
isGetItemsRequesting,
|
|
|
|
} = select( 'wc-api' );
|
2020-03-25 03:20:17 +00:00
|
|
|
const { getSetting: getMutableSetting } = select( SETTINGS_STORE_NAME );
|
2019-10-07 11:51:25 +00:00
|
|
|
const {
|
|
|
|
woocommerce_actionable_order_statuses: orderStatuses = DEFAULT_ACTIONABLE_STATUSES,
|
2020-03-25 03:20:17 +00:00
|
|
|
} = getMutableSetting( 'wc_admin', 'wcAdminSettings', {} );
|
2019-03-29 02:45:19 +00:00
|
|
|
|
|
|
|
if ( ! orderStatuses.length ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ordersQuery = {
|
|
|
|
page: 1,
|
2019-07-16 21:03:15 +00:00
|
|
|
per_page: 1, // Core endpoint requires per_page > 0.
|
|
|
|
status: orderStatuses,
|
2019-07-17 15:56:13 +00:00
|
|
|
_fields: [ 'id' ],
|
2019-03-29 02:45:19 +00:00
|
|
|
};
|
|
|
|
|
2019-07-17 15:56:13 +00:00
|
|
|
getItems( 'orders', ordersQuery );
|
2020-02-14 02:23:21 +00:00
|
|
|
|
|
|
|
// Disable eslint rule requiring `latestNote` 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
|
2019-07-16 21:03:15 +00:00
|
|
|
const totalOrders = getItemsTotalCount( 'orders', ordersQuery );
|
|
|
|
const isError = Boolean( getItemsError( 'orders', ordersQuery ) );
|
|
|
|
const isRequesting = isGetItemsRequesting( 'orders', ordersQuery );
|
2019-03-29 02:45:19 +00:00
|
|
|
|
2019-04-09 07:50:00 +00:00
|
|
|
if ( isError || isRequesting ) {
|
|
|
|
return null;
|
2019-03-29 02:45:19 +00:00
|
|
|
}
|
2019-04-09 07:50:00 +00:00
|
|
|
|
|
|
|
return totalOrders > 0;
|
2019-03-29 02:45:19 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 09:40:10 +00:00
|
|
|
export function getUnapprovedReviews( select ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
const {
|
|
|
|
getReviewsTotalCount,
|
|
|
|
getReviewsError,
|
|
|
|
isGetReviewsRequesting,
|
|
|
|
} = select( 'wc-api' );
|
2019-10-07 11:51:25 +00:00
|
|
|
const reviewsEnabled = getSetting( 'reviewsEnabled' );
|
2020-02-14 02:23:21 +00:00
|
|
|
if ( reviewsEnabled === 'yes' ) {
|
2019-04-30 09:40:10 +00:00
|
|
|
const actionableReviewsQuery = {
|
2019-03-29 02:45:19 +00:00
|
|
|
page: 1,
|
2019-04-30 09:40:10 +00:00
|
|
|
// @todo we are not using this review, so when the endpoint supports it,
|
|
|
|
// it could be replaced with `per_page: 0`
|
2019-03-29 02:45:19 +00:00
|
|
|
per_page: 1,
|
2019-04-30 09:40:10 +00:00
|
|
|
status: 'hold',
|
2019-03-29 02:45:19 +00:00
|
|
|
};
|
2020-02-14 02:23:21 +00:00
|
|
|
const totalActionableReviews = getReviewsTotalCount(
|
|
|
|
actionableReviewsQuery
|
|
|
|
);
|
|
|
|
const isActionableReviewsError = Boolean(
|
|
|
|
getReviewsError( actionableReviewsQuery )
|
|
|
|
);
|
|
|
|
const isActionableReviewsRequesting = isGetReviewsRequesting(
|
|
|
|
actionableReviewsQuery
|
|
|
|
);
|
2019-04-01 18:43:48 +00:00
|
|
|
|
2019-04-30 09:40:10 +00:00
|
|
|
if ( ! isActionableReviewsError && ! isActionableReviewsRequesting ) {
|
|
|
|
return totalActionableReviews > 0;
|
2019-03-29 02:45:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-30 09:40:10 +00:00
|
|
|
return false;
|
2019-03-29 02:45:19 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 22:22:32 +00:00
|
|
|
export function getUnreadStock() {
|
|
|
|
return getSetting( 'hasLowStock', false );
|
2019-03-29 02:45:19 +00:00
|
|
|
}
|