Skip to content

Instantly share code, notes, and snippets.

@ravewebdev

ravewebdev/update-block.js

Last active Oct 11, 2020
Embed
What would you like to do?
4.2. Perform Request and Update Block
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',
data: {
...attributes,
},
} )
.then( ( success ) => {
// Update dataAttributes to reflect changes here...
// E.g., dataAttributes.someAttr = someAttr;
return {
type: 'success',
message: success,
};
} )
.catch( ( error ) => {
return {
type: 'error',
message: error.message,
};
} );
setLoading( false );
setNotice( response );
};
return (
<div className={ className }>
{ null !== notice && (
<span
className={ `notice ${notice.type}` }
role={ 'error' === notice.type ? 'alert' : 'status' }
>
{ notice.message }
</span>
) }
<!-- Call components to display our block here... -->
</div>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment