/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { dispatch } from '@wordpress/data';
* Internal dependencies
import { getResourceName } from '../utils';
const updateNote = ( operations ) => ( noteId, noteFields ) => {
const resourceKey = 'note';
operations.update( [ resourceKey ], {
[ resourceKey ]: { noteId, ...noteFields },
} );
};
const removeNote = ( operations ) => async ( noteId ) => {
const { createNotice } = dispatch( 'core/notices' );
const resourceName = getResourceName( 'note', noteId );
const result = await operations.remove( [ resourceKey ], {
[ resourceKey ]: { noteId },
const response = result[ 0 ]
? result[ 0 ][ resourceName ]
: { data: false, error: true };
if ( response && response.data ) {
createNotice(
'success',
__( 'Message dismissed.', 'woocommerce-admin' ),
{
actions: [
label: __( 'Undo', 'woocommerce-admin' ),
onClick: () => {
[ resourceKey ]: { noteId, is_deleted: 0 },
},
],
}
);
if ( response && response.error ) {
'error',
__( 'Message could not be dismissed.', 'woocommerce-admin' )
const removeAllNotes = ( operations ) => async () => {
const result = await operations.removeAll( [ resourceKey ] );
const response = result ? result[ 0 ] : { error: true };
if ( ! response.error ) {
__( 'All messages dismissed.', 'woocommerce-admin' ),
const notesIds = [];
for ( const note in response ) {
notesIds.push( response[ note ].data.id );
operations.undoRemoveAll( [ resourceKey ], {
notesIds,
is_deleted: 0,
} else {
__( 'Messages could not be dismissed.', 'woocommerce-admin' )
const triggerNoteAction = ( operations ) => ( noteId, actionId ) => {
const resourceKey = 'note-action';
[ resourceKey ]: { noteId, actionId },
export default {
updateNote,
removeNote,
removeAllNotes,
triggerNoteAction,