Last active
October 11, 2020 07:30
-
-
Save ravewebdev/9b4b0c8abfc5ef1d74a011244c43b15c to your computer and use it in GitHub Desktop.
4.2. Perform Request and Update Block
This file contains hidden or 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', | |
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