2019-02-19 21:07:19 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
|
|
|
import { IconButton, Button, Dashicon } from '@wordpress/components';
|
|
|
|
import classnames from 'classnames';
|
2019-02-20 11:50:55 +00:00
|
|
|
import interpolateComponents from 'interpolate-components';
|
2019-03-01 17:20:28 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
2019-02-19 21:07:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { Card } from '@woocommerce/components';
|
|
|
|
|
2019-03-01 17:20:28 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import withSelect from 'wc-api/with-select';
|
|
|
|
import { QUERY_DEFAULTS } from 'wc-api/constants';
|
|
|
|
import sanitizeHTML from 'lib/sanitize-html';
|
2019-02-19 21:07:19 +00:00
|
|
|
|
2019-03-01 17:20:28 +00:00
|
|
|
import './style.scss';
|
2019-02-19 21:07:19 +00:00
|
|
|
|
|
|
|
class StoreAlerts extends Component {
|
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
2019-02-20 15:25:56 +00:00
|
|
|
const { alerts } = this.props;
|
|
|
|
|
2019-02-19 21:07:19 +00:00
|
|
|
this.state = {
|
2019-02-20 16:18:10 +00:00
|
|
|
currentIndex: alerts ? 0 : null,
|
2019-02-19 21:07:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.previousAlert = this.previousAlert.bind( this );
|
|
|
|
this.nextAlert = this.nextAlert.bind( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
previousAlert( event ) {
|
|
|
|
event.stopPropagation();
|
2019-02-20 16:18:10 +00:00
|
|
|
const { currentIndex } = this.state;
|
2019-02-19 21:07:19 +00:00
|
|
|
|
2019-02-20 16:18:10 +00:00
|
|
|
if ( currentIndex > 0 ) {
|
2019-02-19 21:07:19 +00:00
|
|
|
this.setState( {
|
2019-02-20 16:18:10 +00:00
|
|
|
currentIndex: currentIndex - 1,
|
2019-02-19 21:07:19 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nextAlert( event ) {
|
|
|
|
event.stopPropagation();
|
2019-02-20 16:18:10 +00:00
|
|
|
const { alerts } = this.props;
|
|
|
|
const { currentIndex } = this.state;
|
2019-02-19 21:07:19 +00:00
|
|
|
|
2019-02-20 16:18:10 +00:00
|
|
|
if ( currentIndex < alerts.length - 1 ) {
|
2019-02-19 21:07:19 +00:00
|
|
|
this.setState( {
|
2019-02-20 16:18:10 +00:00
|
|
|
currentIndex: currentIndex + 1,
|
2019-02-19 21:07:19 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-02-20 15:25:56 +00:00
|
|
|
const { alerts } = this.props;
|
2019-03-04 13:52:20 +00:00
|
|
|
|
|
|
|
if ( ! alerts || alerts.length === 0 ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-02-20 16:18:10 +00:00
|
|
|
const { currentIndex } = this.state;
|
|
|
|
const numberOfAlerts = alerts.length;
|
|
|
|
const alert = alerts[ currentIndex ] ? alerts[ currentIndex ] : null;
|
|
|
|
const type = alert && alert.type ? alert.type : null;
|
2019-02-19 21:07:19 +00:00
|
|
|
const className = classnames( 'woocommerce-store-alerts', {
|
2019-03-01 17:20:28 +00:00
|
|
|
'is-alert-error': 'error' === type,
|
|
|
|
'is-alert-update': 'update' === type,
|
2019-02-19 21:07:19 +00:00
|
|
|
} );
|
2019-03-01 17:20:28 +00:00
|
|
|
const actions =
|
|
|
|
alert &&
|
|
|
|
alert.actions.map( action => (
|
|
|
|
<Button key={ action.name } isDefault href={ action.url }>
|
|
|
|
{ action.label }
|
|
|
|
</Button>
|
|
|
|
) );
|
2019-02-20 19:03:08 +00:00
|
|
|
|
2019-02-19 21:07:19 +00:00
|
|
|
return (
|
2019-03-04 13:52:20 +00:00
|
|
|
<Card
|
|
|
|
title={ [
|
|
|
|
alert.icon && <Dashicon key="icon" icon={ alert.icon } />,
|
|
|
|
<Fragment key="title">{ alert.title }</Fragment>,
|
|
|
|
] }
|
|
|
|
className={ className }
|
|
|
|
action={
|
|
|
|
numberOfAlerts > 1 && (
|
|
|
|
<div className="woocommerce-store-alerts__pagination">
|
|
|
|
<IconButton
|
|
|
|
icon="arrow-left-alt2"
|
|
|
|
onClick={ this.previousAlert }
|
|
|
|
disabled={ 0 === currentIndex }
|
|
|
|
label={ __( 'Previous Alert', 'wc-admin' ) }
|
|
|
|
/>
|
|
|
|
<span
|
|
|
|
className="woocommerce-store-alerts__pagination-label"
|
|
|
|
role="status"
|
|
|
|
aria-live="polite"
|
|
|
|
>
|
|
|
|
{ interpolateComponents( {
|
|
|
|
mixedString: __( '{{current /}} of {{total /}}', 'wc-admin' ),
|
|
|
|
components: {
|
|
|
|
current: <Fragment>{ currentIndex + 1 }</Fragment>,
|
|
|
|
total: <Fragment>{ numberOfAlerts }</Fragment>,
|
|
|
|
},
|
|
|
|
} ) }
|
|
|
|
</span>
|
|
|
|
<IconButton
|
|
|
|
icon="arrow-right-alt2"
|
|
|
|
onClick={ this.nextAlert }
|
|
|
|
disabled={ numberOfAlerts - 1 === currentIndex }
|
|
|
|
label={ __( 'Next Alert', 'wc-admin' ) }
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
className="woocommerce-store-alerts__message"
|
|
|
|
dangerouslySetInnerHTML={ sanitizeHTML( alert.content ) }
|
|
|
|
/>
|
|
|
|
{ actions && <div className="woocommerce-store-alerts__actions">{ actions }</div> }
|
|
|
|
</Card>
|
2019-02-19 21:07:19 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 17:20:28 +00:00
|
|
|
export default compose(
|
|
|
|
withSelect( select => {
|
|
|
|
const { getNotes } = select( 'wc-api' );
|
|
|
|
const alertsQuery = {
|
|
|
|
page: 1,
|
|
|
|
per_page: QUERY_DEFAULTS.pageSize,
|
|
|
|
type: 'error,update',
|
|
|
|
};
|
2019-02-20 15:25:56 +00:00
|
|
|
|
2019-03-01 17:20:28 +00:00
|
|
|
const alerts = getNotes( alertsQuery );
|
2019-02-20 16:37:03 +00:00
|
|
|
|
2019-03-01 17:20:28 +00:00
|
|
|
return { alerts };
|
|
|
|
} )
|
|
|
|
)( StoreAlerts );
|