2020-03-25 03:20:17 +00:00
|
|
|
/**
|
2020-08-13 02:05:22 +00:00
|
|
|
* External dependencies
|
2020-03-25 03:20:17 +00:00
|
|
|
*/
|
2020-08-11 00:16:27 +00:00
|
|
|
import {
|
2020-08-13 13:23:38 +00:00
|
|
|
NOTES_STORE_NAME,
|
2020-08-11 00:16:27 +00:00
|
|
|
USER_STORE_NAME,
|
2020-08-20 23:37:41 +00:00
|
|
|
QUERY_DEFAULTS,
|
2020-08-11 00:16:27 +00:00
|
|
|
} from '@woocommerce/data';
|
2020-08-13 02:05:22 +00:00
|
|
|
import { getSetting } from '@woocommerce/wc-admin-settings';
|
2020-03-25 03:20:17 +00:00
|
|
|
|
2019-03-29 02:45:19 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-10-06 13:13:32 +00:00
|
|
|
import { getUnreadNotesCount } from '../../inbox-panel/utils';
|
2019-03-29 02:45:19 +00:00
|
|
|
|
|
|
|
export function getUnreadNotes( select ) {
|
2020-08-13 13:23:38 +00:00
|
|
|
const { getNotes, getNotesError, isResolving } = select( NOTES_STORE_NAME );
|
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 );
|
2020-08-13 13:23:38 +00:00
|
|
|
const isError = Boolean( getNotesError( 'getNotes', [ notesQuery ] ) );
|
|
|
|
const isRequesting = isResolving( 'getNotes', [ notesQuery ] );
|
2019-04-09 07:50:00 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-25 18:51:15 +00:00
|
|
|
export function getLowStockCount() {
|
|
|
|
return getSetting( 'lowStockCount', 0 );
|
2019-03-29 02:45:19 +00:00
|
|
|
}
|