Skip to content

Instantly share code, notes, and snippets.

View ravewebdev's full-sized avatar

Rae Van Epps ravewebdev

View GitHub Profile
@ravewebdev
ravewebdev / unique-block-id.js
Last active July 15, 2020 20:48
1.1. Unique Block Identifiers
registerBlockType( 'rave/initiative-tracker', {
title: __( 'Initiative Tracker', 'initiative-tracker' ),
attributes: {
id: {
type: 'string',
default: '',
},
},
edit: ( props ) => {
const {
@ravewebdev
ravewebdev / display-notice.js
Created July 14, 2020 22:52
4.2.5. Display update notice
{ null !== notice && (
<span
className={ `notice ${notice.type}` }
role={ 'error' === notice.type ? 'alert' : 'status' }
>
{ notice.message }
</span>
) }
@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,
@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 / 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 / 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 / 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 / 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 / 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 / create-nonce.php
Created July 13, 2020 22:22
3.2.1. Create nonce for use with REST API
<?php
function localize_route() {
$nonce = wp_create_nonce( 'wp_rest' );
}
add_action( 'wp_enqueue_scripts', 'localize_route' );