From 9cc8326de2fae77db946b3868193b7697263053d Mon Sep 17 00:00:00 2001 From: Fernando Marichal Date: Wed, 21 Apr 2021 16:50:33 -0300 Subject: [PATCH] Added email note support This commit adds email note support --- api/admin-notes/add-note.php | 30 +++++++++++ src/admin-notes/add-email-note.js | 82 +++++++++++++++++++++++++++++++ src/admin-notes/admin-notes.js | 18 ++++--- 3 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 src/admin-notes/add-email-note.js diff --git a/api/admin-notes/add-note.php b/api/admin-notes/add-note.php index a5b6ef5dcc4..0ff3ecc52df 100644 --- a/api/admin-notes/add-note.php +++ b/api/admin-notes/add-note.php @@ -6,6 +6,11 @@ register_woocommerce_admin_test_helper_rest_route( 'admin_notes_add_note' ); +register_woocommerce_admin_test_helper_rest_route( + '/admin-notes/add-email-note/v1', + 'admin_notes_add_email_note' +); + function admin_notes_add_note( $request ) { $note = new Note(); @@ -17,3 +22,28 @@ function admin_notes_add_note( $request ) { return true; } + +function admin_notes_add_email_note( $request ) { + $note = new Note(); + + $additional_data = array( + 'role' => 'administrator', + ); + $action_name = sprintf( + 'test-action-%s', + $request->get_param( 'name' ) + ); + + $content = $request->get_param( 'content' ) ?? 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud.'; + + $note->set_name( $request->get_param( 'name' ) ); + $note->set_title( $request->get_param( 'title' ) ); + $note->set_type( 'email' ); + $note->set_content( $content ); + $note->set_content_data( (object) $additional_data ); + $note->add_action( $action_name, 'Test action', wc_admin_url() ); + + $note->save(); + + return true; +} diff --git a/src/admin-notes/add-email-note.js b/src/admin-notes/add-email-note.js new file mode 100644 index 00000000000..89274e938b3 --- /dev/null +++ b/src/admin-notes/add-email-note.js @@ -0,0 +1,82 @@ +/** + * External dependencies. + */ +import { useState } from '@wordpress/element'; +import { Button } from '@wordpress/components'; +import apiFetch from '@wordpress/api-fetch'; + +export const AddEmailNote = () => { + const [isAdding, setIsAdding] = useState(false); + const [hasAdded, setHasAdded] = useState(false); + const [errorMessage, setErrorMessage] = useState(false); + + async function triggerAddEmailNote() { + setIsAdding(true); + setHasAdded(false); + setErrorMessage(false); + + const name = prompt('Enter the note name'); + if (!name) { + setIsAdding(false); + return; + } + + const title = prompt('Enter the note title'); + if (!title) { + setIsAdding(false); + return; + } + + try { + await apiFetch({ + path: '/wc-admin-test-helper/admin-notes/add-email-note/v1', + method: 'POST', + data: { + name, + title, + }, + }); + setHasAdded(true); + } catch (ex) { + setErrorMessage(ex.message); + } + + setIsAdding(false); + } + + return ( + <> +

+ + Add an email note + +

+

+ This will add a new email note. Enable email + insights{' '} + + here + {' '} + and run the cron to send the note by email. +
+ +
+ + {isAdding && 'Adding, please wait'} + {hasAdded && 'Email note added'} + {errorMessage && ( + <> + Error: {errorMessage} + + )} + +

+ + ); +}; diff --git a/src/admin-notes/admin-notes.js b/src/admin-notes/admin-notes.js index 5381596a1bb..0b22680d46f 100644 --- a/src/admin-notes/admin-notes.js +++ b/src/admin-notes/admin-notes.js @@ -3,14 +3,16 @@ */ import { DeleteAllNotes } from './delete-all-notes'; import { AddNote } from './add-note'; +import { AddEmailNote } from './add-email-note'; export const AdminNotes = () => { - return ( - <> -

Admin notes

-

This section contains tools for managing admin notes.

- - - - ); + return ( + <> +

Admin notes

+

This section contains tools for managing admin notes.

+ + + + + ); };