From 5a49991cb1904bbd015e120d459281bae33c4822 Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Fri, 24 May 2019 16:22:46 -0400 Subject: [PATCH] Trigger a note action on button click. --- .../header/activity-panel/panels/inbox.js | 20 ++++++++++------ .../client/layout/store-alerts/index.js | 12 ++++------ .../client/wc-api/notes/mutations.js | 6 +++++ .../client/wc-api/notes/operations.js | 24 ++++++++++++++++++- 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/plugins/woocommerce-admin/client/header/activity-panel/panels/inbox.js b/plugins/woocommerce-admin/client/header/activity-panel/panels/inbox.js index 5949850e8a8..311946bda9e 100644 --- a/plugins/woocommerce-admin/client/header/activity-panel/panels/inbox.js +++ b/plugins/woocommerce-admin/client/header/activity-panel/panels/inbox.js @@ -49,18 +49,23 @@ class InboxPanel extends Component { } renderNotes() { - const { lastRead, notes } = this.props; + const { lastRead, notes, triggerNoteAction } = this.props; if ( 0 === Object.keys( notes ).length ) { return this.renderEmptyCard(); } - const getButtonsFromActions = actions => { - if ( ! actions ) { + const getButtonsFromActions = note => { + if ( ! note.actions ) { return []; } - return actions.map( action => ( - ) ); @@ -80,7 +85,7 @@ class InboxPanel extends Component { ! note.date_created_gmt || new Date( note.date_created_gmt + 'Z' ).getTime() > lastRead } - actions={ getButtonsFromActions( note.actions ) } + actions={ getButtonsFromActions( note ) } > @@ -154,10 +159,11 @@ export default compose( return { notes, isError, isRequesting, lastRead: userData.activity_panel_inbox_last_read }; } ), withDispatch( dispatch => { - const { updateCurrentUserData } = dispatch( 'wc-api' ); + const { updateCurrentUserData, triggerNoteAction } = dispatch( 'wc-api' ); return { updateCurrentUserData, + triggerNoteAction, }; } ) )( InboxPanel ); diff --git a/plugins/woocommerce-admin/client/layout/store-alerts/index.js b/plugins/woocommerce-admin/client/layout/store-alerts/index.js index 714041bfb5b..cfa73582332 100644 --- a/plugins/woocommerce-admin/client/layout/store-alerts/index.js +++ b/plugins/woocommerce-admin/client/layout/store-alerts/index.js @@ -8,7 +8,6 @@ import { IconButton, Button, Dashicon, Dropdown, NavigableMenu } from '@wordpres import classnames from 'classnames'; import interpolateComponents from 'interpolate-components'; import { compose } from '@wordpress/compose'; -import { noop } from 'lodash'; import { withDispatch } from '@wordpress/data'; import moment from 'moment'; @@ -64,18 +63,15 @@ class StoreAlerts extends Component { } renderActions( alert ) { - const { updateNote } = this.props; + const { triggerNoteAction, updateNote } = this.props; const actions = alert.actions.map( action => { - const markStatus = () => { - updateNote( alert.id, { status: action.status } ); - }; return ( @@ -257,8 +253,10 @@ export default compose( }; } ), withDispatch( dispatch => { - const { updateNote } = dispatch( 'wc-api' ); + const { triggerNoteAction, updateNote } = dispatch( 'wc-api' ); + return { + triggerNoteAction, updateNote, }; } ) diff --git a/plugins/woocommerce-admin/client/wc-api/notes/mutations.js b/plugins/woocommerce-admin/client/wc-api/notes/mutations.js index 15f98427aae..13d1057c2dc 100644 --- a/plugins/woocommerce-admin/client/wc-api/notes/mutations.js +++ b/plugins/woocommerce-admin/client/wc-api/notes/mutations.js @@ -5,6 +5,12 @@ const updateNote = operations => ( noteId, noteFields ) => { operations.update( [ resourceKey ], { [ resourceKey ]: { noteId, ...noteFields } } ); }; +const triggerNoteAction = operations => ( noteId, actionId ) => { + const resourceKey = 'note-action'; + operations.update( [ resourceKey ], { [ resourceKey ]: { noteId, actionId } } ); +}; + export default { updateNote, + triggerNoteAction, }; diff --git a/plugins/woocommerce-admin/client/wc-api/notes/operations.js b/plugins/woocommerce-admin/client/wc-api/notes/operations.js index 06ab2f596de..bbd5aaa1bcd 100644 --- a/plugins/woocommerce-admin/client/wc-api/notes/operations.js +++ b/plugins/woocommerce-admin/client/wc-api/notes/operations.js @@ -20,7 +20,10 @@ function read( resourceNames, fetch = apiFetch ) { } function update( resourceNames, data, fetch = apiFetch ) { - return [ ...updateNote( resourceNames, data, fetch ) ]; + return [ + ...updateNote( resourceNames, data, fetch ), + ...triggerAction( resourceNames, data, fetch ), + ]; } function readNoteQueries( resourceNames, fetch ) { @@ -93,7 +96,26 @@ function updateNote( resourceNames, data, fetch ) { return []; } +function triggerAction( resourceNames, data, fetch ) { + const resourceName = 'note-action'; + if ( resourceNames.includes( resourceName ) ) { + const { noteId, actionId } = data[ resourceName ]; + const url = `${ NAMESPACE }/admin/notes/${ noteId }/action/${ actionId }`; + return [ + fetch( { path: url, method: 'POST' } ) + .then( note => { + return { [ 'note:' + noteId ]: { data: note } }; + } ) + .catch( error => { + return { [ 'note:' + noteId ]: { error } }; + } ), + ]; + } + return []; +} + export default { read, update, + triggerAction, };