This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ null !== notice && ( | |
<span | |
className={ `notice ${notice.type}` } | |
role={ 'error' === notice.type ? 'alert' : 'status' } | |
> | |
{ notice.message } | |
</span> | |
) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
registerBlockType( 'rave/initiative-tracker', { | |
title: __( 'Initiative Tracker', 'initiative-tracker' ), | |
attributes: { | |
id: { | |
type: 'string', | |
default: '', | |
}, | |
}, | |
edit: ( props ) => { | |
const { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
register_block_type( 'rave/initiative-tracker', array( | |
'editor_script' => 'initiative-tracker-editor-script', | |
'editor_style' => 'initiative-tracker-editor-style', | |
'style' => 'initiative-tracker-style', | |
'render_callback' => __NAMESPACE__ . '\render_block', | |
) ); | |
function render_block( array $attributes ) : string { | |
$id = $attributes['id']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const FrontendTracker = ( props ) => { | |
const { | |
dataAttributes, | |
} = props; | |
const [ attributes, setAttributes ] = useState( { | |
block_id: 0, | |
} ); | |
useEffect( () => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const trackerClass = '.wp-block-rave-initiative-tracker', | |
trackers = document.querySelectorAll( trackerClass ); | |
trackers.forEach( ( tracker ) => { | |
const attributes = { | |
block_id: tracker.dataset.id, | |
post_id: parseInt( tracker.dataset.post_id, 10 ), | |
}; | |
render( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
register_rest_route( 'rave-initiative/v1', '/initiative/(?P<id>[\d]+)', [ | |
'methods' => WP_REST_SERVER::EDITABLE, | |
'callback' => 'update_initiative', | |
'permission_callback' => 'check_initiative_permissions', | |
] ); | |
function check_initiative_permissions( WP_REST_Request $request ) : bool { | |
$post_id = $request->get_param( 'id' ) ?? 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function localize_route() { | |
wp_localize_script( 'initiative-tracker-frontend-script', 'initTracker', [ | |
'route' => 'rave-initiative/v1/initiative', | |
] ); | |
} | |
add_action( 'wp_enqueue_scripts', 'localize_route' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const FrontendTracker = ( props ) => { | |
// Add the following before the `return()` block from Part 1, step 2.1. | |
const [ isLoading, setLoading ] = useState( false ); | |
const [ notice, setNotice ] = useState( null ); | |
useEffect( () => { | |
if ( null === notice ) { | |
return; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const FrontendTracker = ( props ) => { | |
// Add the following in place of the `return` statement from Part 1, step 2.1. | |
const saveUpdates = async () => { | |
setLoading( true ); | |
setNotice( null ); | |
const response = await apiFetch( { | |
path: `${initTracker.route}/${ dataAttributes.post_id }`, | |
method: 'POST', |