Skip to content

Instantly share code, notes, and snippets.

View ravewebdev's full-sized avatar

Rae Van Epps ravewebdev

View GitHub Profile
@ravewebdev
ravewebdev / render-component.js
Last active July 13, 2020 22:28
2.1.3. Render component
const FrontendTracker = ( props ) => {
// Code from 2.1.1.-2.1.2. here...
return (
<div className={ className }>
<!-- Call components to display your block here... -->
</div>
);
};
@ravewebdev
ravewebdev / callback.php
Last active July 13, 2020 22:28
3.1.3. Handle endpoint callback
<?php
function update_initiative( WP_REST_Request $request ) {
$post_id = $request->get_param( 'id' );
$block_id = $request->get_param( 'block_id' );
$post_content = get_post_field( 'post_content', $post_id );
$post_blocks = parse_blocks( $post_content );
// Update usage count for target block.
$post_blocks = array_map( function( $block ) use ( $block_id ) {
if ( 'rave/initiative-tracker' !== ( $block['blockName'] ?? '' ) || ( $block['attrs']['id'] ?? 0 ) !== $block_id ) {
@ravewebdev
ravewebdev / init-loading-state.js
Created July 14, 2020 17:13
4.1.1. Set up loading state
const FrontendTracker = ( props ) => {
// Code from 2.1. here...
const [ isLoading, setLoading ] = useState( false );
};
@ravewebdev
ravewebdev / track-attr-changes.js
Last active July 14, 2020 19:27
2.1.2. Use State and Effect hooks to track attribute changes
const FrontendTracker = ( props ) => {
// Code from 2.1.1. here...
const [ attributes, setAttributes ] = useState( {
block_id: 0,
} );
useEffect( () => {
setAttributes( {
...dataAttributes,
} );
@ravewebdev
ravewebdev / init-notice-state.js
Last active July 14, 2020 22:13
4.1.2. Set up and reset notice state
const FrontendTracker = ( props ) => {
// Code from 4.1.1. here...
const [ notice, setNotice ] = useState( null );
useEffect( () => {
if ( null === notice ) {
return;
}
const timer = setTimeout( () => {
@ravewebdev
ravewebdev / retrieve-localized-data.js
Created July 14, 2020 22:24
4.2.1. Retrieve localized data
const saveCharacterUpdates = async () => {
const path = initTracker.initiative,
nonce = initTracker.nonce;
if ( null === path || null === nonce ) {
return;
}
};
@ravewebdev
ravewebdev / localize-route.php
Last active July 14, 2020 22:34
3.2.2. Localize frontend script data
<?php
function localize_route() {
// Code from 3.2.1. here...
wp_localize_script( 'initiative-tracker-frontend-script', 'initTracker', [
'nonce' => $nonce,
'initiative' => 'rave-initiative/v1/initiative',
] );
}
@ravewebdev
ravewebdev / fetch-request.js
Last active July 14, 2020 22:40
4.2.2. Perform fetch request
const saveCharacterUpdates = async () => {
// Code from 4.2.1. here...
setLoading( true );
setNotice( null );
const response = await apiFetch( {
path: `${path}/${ dataAttributes.post_id }`,
method: 'POST',
data: {
...attributes,
@ravewebdev
ravewebdev / nonce-handling.php
Created July 14, 2020 22:42
4.2.3. Add nonce handling
<?php
public function update_initiative( WP_REST_Request $request ) {
$nonce = $request->get_header( 'x_wp_nonce' );
if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
return new WP_Error( 'invalid-nonce', __( 'Invalid nonce.', 'initiative-tracker' ) );
}
// Code from 3.1.3. here...
}
@ravewebdev
ravewebdev / request-response.js
Created July 14, 2020 22:49
4.2.4. Handle request response
const saveCharacterUpdates = async () => {
// Code from 4.2.2. here...
const response = await apiFetch( // Args defined in 4.2.2. )
.then( ( success ) => {
// Update dataAttributes to reflect changes here...
// E.g., dataAttributes.someAttr = someAttr;
return {
type: 'success',
message: success,