2020-06-12 06:38:33 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { dispatch } from '@wordpress/data';
|
|
|
|
|
|
|
|
export function createNoticesFromResponse( response ) {
|
|
|
|
const { createNotice } = dispatch( 'core/notices' );
|
|
|
|
|
2020-07-28 02:32:58 +00:00
|
|
|
if (
|
|
|
|
response.error_data &&
|
|
|
|
response.errors &&
|
|
|
|
Object.keys( response.errors ).length
|
|
|
|
) {
|
2020-06-12 06:38:33 +00:00
|
|
|
// Loop over multi-error responses.
|
2020-07-28 02:32:58 +00:00
|
|
|
Object.keys( response.errors ).forEach( ( errorKey ) => {
|
2020-06-12 06:38:33 +00:00
|
|
|
createNotice( 'error', response.errors[ errorKey ].join( ' ' ) );
|
|
|
|
} );
|
|
|
|
} else if ( response.message ) {
|
|
|
|
// Handle generic messages.
|
|
|
|
createNotice( response.code ? 'error' : 'success', response.message );
|
|
|
|
}
|
2020-07-28 02:32:58 +00:00
|
|
|
}
|