Added note types

This commit is contained in:
Fernando Marichal 2021-04-23 16:37:34 -03:00
parent 9cc8326de2
commit 14e14d3719
4 changed files with 119 additions and 34 deletions

View File

@ -6,44 +6,61 @@ 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();
$default_note_data = get_default_note_data();
$type = $request->get_param( 'type' );
$layout = $request->get_param( 'layout' );
$note->set_name( $request->get_param( 'name' ) );
$note->set_title( $request->get_param( 'title' ) );
$note->set_content( $default_note_data[ 'content' ] );
$note->set_image( $default_note_data[ $type ][ $layout ] );
$note->set_layout( $layout );
$note->set_type( $type );
possibly_add_action( $note );
if ( 'email' === $type ) {
add_email_note_params( $note );
}
$note->save();
return true;
}
function admin_notes_add_email_note( $request ) {
$note = new Note();
function add_email_note_params( $note ) {
$additional_data = array(
'role' => 'administrator',
);
$note->set_content_data( (object) $additional_data );
}
function possibly_add_action( $note ) {
if ( $note->get_type() === 'info' ) {
return;
}
$action_name = sprintf(
'test-action-%s',
$request->get_param( 'name' )
$note->get_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;
}
function get_default_note_data() {
return array(
'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.',
'info' => array(
'banner' => 'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSAK9Xg4L6FGmDAW5UVtVEv1IXKtGV3-rxYLfAzOBF-fMUdmyWz&usqp=CAU',
'thumbnail' => 'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTlm2mX1h6GfMnT9sDD9GsJB7of3MZw3UDzCAQM9EBepiJzyxXH&usqp=CAU',
'plain' => ''
),
'email' => array(
'plain' => 'https://www.logosvgpng.com/wp-content/uploads/2018/10/woocommerce-logo-vector.png'
),
'update' => array(
'plain' => ''
)
);
}

View File

@ -4,12 +4,15 @@
import { useState } from '@wordpress/element';
import { Button } from '@wordpress/components';
import apiFetch from '@wordpress/api-fetch';
import { SelectControl } from '@wordpress/components';
export const AddNote = () => {
const [ isAdding, setIsAdding ] = useState( false );
const [ hasAdded, setHasAdded ] = useState( false );
const [ errorMessage, setErrorMessage ] = useState( false );
const [ noteType, setNoteType ] = useState( 'info' );
const [ noteLayout, setNoteLayout ] = useState( 'plain' );
async function triggerAddNote() {
setIsAdding( true );
@ -34,6 +37,8 @@ export const AddNote = () => {
method: 'POST',
data: {
name,
type: noteType,
layout: noteLayout,
title,
},
} );
@ -46,20 +51,76 @@ export const AddNote = () => {
setIsAdding( false );
}
function onTypeChange( val ) {
setNoteType( val );
if ( val !== 'info' ) {
setNoteLayout( 'plain' );
}
}
function onLayoutChange( val ) {
setNoteLayout( val );
}
function getAddNoteDescription() {
switch ( noteType ){
case 'email':
return (
<>
This will add a new <strong>email</strong> note. Enable email insights{' '}
<a href="/wp-admin/admin.php?page=wc-settings&tab=email">
here
</a>{' '}
and run the cron to send the note by email.
</>);
default:
return (
<>
This will add a new note. Currently only the note name
and title will be used to create the note.
</>
);
}
}
return (
<>
<p><strong>Add a note</strong></p>
<p>
This will add a new note. Currently only the note name
and title will be used to create the note.
<div>
{ getAddNoteDescription() }
<br/>
<Button
onClick={ triggerAddNote }
disabled={ isAdding }
isPrimary
>
Add admin note
</Button>
<div className="woocommerce-admin-test-helper__add-notes">
<Button
onClick={ triggerAddNote }
disabled={ isAdding }
isPrimary
>
Add admin note
</Button>
<SelectControl
label="Type"
onChange={ onTypeChange }
labelPosition="side"
options={ [
{ label: 'Info', value: 'info' },
{ label: 'Update', value: 'update' },
{ label: 'Email', value: 'email' },
] }
value={ noteType }
/>
<SelectControl
label="Layout"
onChange={ onLayoutChange }
labelPosition="side"
options={ [
{ label: 'Plain', value: 'plain' },
{ label: 'Banner', value: 'banner' },
{ label: 'Thumbnail', value: 'thumbnail' },
] }
disabled={ noteType !== 'info' }
value={ noteLayout }
/>
</div>
<br/>
<span className="woocommerce-admin-test-helper__action-status">
{ isAdding && 'Adding, please wait' }
@ -70,7 +131,7 @@ export const AddNote = () => {
</>
) }
</span>
</p>
</div>
</>
);
};

View File

@ -3,7 +3,6 @@
*/
import { DeleteAllNotes } from './delete-all-notes';
import { AddNote } from './add-note';
import { AddEmailNote } from './add-email-note';
export const AdminNotes = () => {
return (
@ -11,7 +10,6 @@ export const AdminNotes = () => {
<h2>Admin notes</h2>
<p>This section contains tools for managing admin notes.</p>
<AddNote />
<AddEmailNote />
<DeleteAllNotes />
</>
);

View File

@ -11,6 +11,15 @@
font-family: monospace;
}
.woocommerce-admin-test-helper__add-notes {
width: 410px;
display: flex;
justify-content: space-between;
.components-base-control__field {
margin-bottom: 0;
padding-top: 3px;
}
}
#wc-admin-test-helper-options {
div.search-box {