Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Last active July 1, 2019 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorbenic/c0211d5382d1b62b97972713920b90e9 to your computer and use it in GitHub Desktop.
Save igorbenic/c0211d5382d1b62b97972713920b90e9 to your computer and use it in GitHub Desktop.
Headless WordPress: Notices System | https://ibenic.com/headless-wordpress-notices-system
// ...
import Dashboard from './components/Dashboard';
import AppNotices from './components/AppNotices';
import '@wordpress/notices';
function App() {
// ...
return (
<div className="App container">
// ...
<AppNotices />
// ...
</div>
);
}
export default App;
@import '~bootstrap/scss/bootstrap.scss';
@import '~@wordpress/components/build-style/style.css';
.components-editor-notices__snackbar {
position: fixed;
right: 0;
bottom: 20px;
padding-left: 16px;
padding-right: 16px;
}
import React from 'react';
/**
* External dependencies
*/
import { filter } from 'lodash';
/**
* WordPress dependencies
*/
import { SnackbarList, NoticeList } from '@wordpress/components';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
// ...
export function AppNotices( { notices, onRemove } ) {
const defaultNotices = filter( notices, {
type: 'default',
} );
const snackbarNotices = filter( notices, {
type: 'snackbar',
} );
return (
<>
<NoticeList
notices={ defaultNotices }
className="components-editor-notices__pinned"
/>
<SnackbarList
notices={ snackbarNotices }
className="components-editor-notices__snackbar"
onRemove={ onRemove }
/>
</>
);
}
export default compose( [
withSelect( ( select ) => ( {
notices: select( 'core/notices' ).getNotices(),
} ) ),
withDispatch( ( dispatch ) => ( {
onRemove: dispatch( 'core/notices' ).removeNotice,
} ) ),
] )( AppNotices );
import { dispatch } from '@wordpress/data';
export function addSnackbar( message, id ) {
addNotice( message, id, 'success', 'snackbar');
}
export function addNotice( message, id, type = 'success', noticeType = 'default' ) {
switch( type ) {
case 'success':
dispatch( 'core/notices' ).createSuccessNotice( message, {
id: id,
type: noticeType,
} );
break;
case 'error':
dispatch( 'core/notices' ).createErrorNotice( message, {
id: id,
type: noticeType,
} );
break;
case 'warning':
dispatch( 'core/notices' ).createWarningNotice( message, {
id: id,
type: noticeType,
} );
break;
case 'info':
dispatch( 'core/notices' ).createInfoNotice( message, {
id: id,
type: noticeType,
} );
break;
default:
dispatch( 'core/notices' ).createNotice( message, {
id: id,
type: noticeType,
} );
}
}
export function removeNotice( id ) {
dispatch( 'core/notices' ).removeNotice( id );
}
import React from 'react';
import { TextControl, Button } from '@wordpress/components';
import { addSnackbar } from '../../functions/notices';
const axios = require('axios');
class Settings extends React.Component {
// ...
updateUser() {
// ...
axios({
method: 'POST',
url: userURI,
headers: { 'Authorization': 'Bearer ' + token },
data: this.state.update
}).then(function (response) {
if ( response.status === 200 ) {
const data = response.data;
_this.props.setCurrentUser( data );
// Adding snackbar.
addSnackbar( 'Settings Updated', 'updated_user' );
}
})
.catch(function (error) {
console.error( error );
});
}
// ...
}
export default Settings;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment