Skip to content

Instantly share code, notes, and snippets.

@ravewebdev
Last active October 11, 2020 07:30
Show Gist options
  • Save ravewebdev/9b4b0c8abfc5ef1d74a011244c43b15c to your computer and use it in GitHub Desktop.
Save ravewebdev/9b4b0c8abfc5ef1d74a011244c43b15c to your computer and use it in GitHub Desktop.
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