2019-02-19 21:07:19 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
2019-03-18 19:05:44 +00:00
|
|
|
import { IconButton, Button, Dashicon, Dropdown, NavigableMenu } from '@wordpress/components';
|
2019-02-19 21:07:19 +00:00
|
|
|
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-03-12 13:13:20 +00:00
|
|
|
import { withDispatch } from '@wordpress/data';
|
2019-03-18 20:41:35 +00:00
|
|
|
import moment from 'moment';
|
2019-02-19 21:07:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
2019-03-18 19:05:44 +00:00
|
|
|
import { Card, DropdownButton } from '@woocommerce/components';
|
2019-02-19 21:07:19 +00:00
|
|
|
|
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-03-06 22:50:47 +00:00
|
|
|
import StoreAlertsPlaceholder from './placeholder';
|
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
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 19:05:44 +00:00
|
|
|
renderActions( alert ) {
|
2019-05-24 20:22:46 +00:00
|
|
|
const { triggerNoteAction, updateNote } = this.props;
|
2019-03-18 19:05:44 +00:00
|
|
|
const actions = alert.actions.map( action => {
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
key={ action.name }
|
|
|
|
isDefault
|
2019-05-21 19:01:55 +00:00
|
|
|
isPrimary={ action.primary }
|
2019-05-24 17:05:12 +00:00
|
|
|
href={ action.url || undefined }
|
2019-05-24 20:22:46 +00:00
|
|
|
onClick={ () => triggerNoteAction( alert.id, action.id ) }
|
2019-03-18 19:05:44 +00:00
|
|
|
>
|
|
|
|
{ action.label }
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
2019-03-18 20:41:35 +00:00
|
|
|
// TODO: should "next X" be the start, or exactly 1X from the current date?
|
2019-03-18 19:05:44 +00:00
|
|
|
const snoozeOptions = [
|
|
|
|
{
|
2019-03-18 20:41:35 +00:00
|
|
|
newDate: moment()
|
|
|
|
.add( 4, 'hours' )
|
|
|
|
.unix(),
|
2019-03-18 19:05:44 +00:00
|
|
|
label: __( 'Later Today', 'woocommerce-admin' ),
|
|
|
|
},
|
|
|
|
{
|
2019-03-18 20:41:35 +00:00
|
|
|
newDate: moment()
|
|
|
|
.add( 1, 'day' )
|
|
|
|
.hour( 9 )
|
|
|
|
.minute( 0 )
|
|
|
|
.second( 0 )
|
|
|
|
.millisecond( 0 )
|
|
|
|
.unix(),
|
2019-03-18 19:05:44 +00:00
|
|
|
label: __( 'Tomorrow', 'woocommerce-admin' ),
|
|
|
|
},
|
|
|
|
{
|
2019-03-18 20:41:35 +00:00
|
|
|
newDate: moment()
|
|
|
|
.add( 1, 'week' )
|
|
|
|
.hour( 9 )
|
|
|
|
.minute( 0 )
|
|
|
|
.second( 0 )
|
|
|
|
.millisecond( 0 )
|
|
|
|
.unix(),
|
2019-03-18 19:05:44 +00:00
|
|
|
label: __( 'Next Week', 'woocommerce-admin' ),
|
|
|
|
},
|
|
|
|
{
|
2019-03-18 20:41:35 +00:00
|
|
|
newDate: moment()
|
|
|
|
.add( 1, 'month' )
|
|
|
|
.hour( 9 )
|
|
|
|
.minute( 0 )
|
|
|
|
.second( 0 )
|
|
|
|
.millisecond( 0 )
|
|
|
|
.unix(),
|
2019-03-18 19:05:44 +00:00
|
|
|
label: __( 'Next Month', 'woocommerce-admin' ),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2019-03-18 20:41:35 +00:00
|
|
|
const setReminderDate = ( newDate, onClose ) => {
|
2019-03-18 19:05:44 +00:00
|
|
|
return () => {
|
|
|
|
onClose();
|
2019-03-18 20:41:35 +00:00
|
|
|
updateNote( alert.id, { status: 'snoozed', date_reminder: newDate } );
|
2019-03-18 19:05:44 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const snooze = alert.is_snoozable && (
|
|
|
|
<Dropdown
|
2019-03-19 15:50:37 +00:00
|
|
|
className="woocommerce-store-alerts__snooze"
|
2019-03-18 19:05:44 +00:00
|
|
|
position="bottom"
|
|
|
|
expandOnMobile
|
|
|
|
renderToggle={ ( { isOpen, onToggle } ) => (
|
|
|
|
<DropdownButton
|
|
|
|
onClick={ onToggle }
|
|
|
|
isOpen={ isOpen }
|
|
|
|
labels={ [ __( 'Remind Me Later', 'woocommerce-admin' ) ] }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
renderContent={ ( { onClose } ) => (
|
|
|
|
<NavigableMenu className="components-dropdown-menu__menu">
|
|
|
|
{ snoozeOptions.map( ( option, idx ) => (
|
|
|
|
<Button
|
|
|
|
className="components-dropdown-menu__menu-item"
|
|
|
|
key={ idx }
|
2019-03-18 20:41:35 +00:00
|
|
|
onClick={ setReminderDate( option.newDate, onClose ) }
|
2019-03-18 19:05:44 +00:00
|
|
|
>
|
|
|
|
{ option.label }
|
|
|
|
</Button>
|
|
|
|
) ) }
|
|
|
|
</NavigableMenu>
|
|
|
|
) }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( actions || snooze ) {
|
|
|
|
return (
|
|
|
|
<div className="woocommerce-store-alerts__actions">
|
|
|
|
{ actions }
|
|
|
|
{ snooze }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:07:19 +00:00
|
|
|
render() {
|
2019-03-08 16:17:48 +00:00
|
|
|
const alerts = this.props.alerts || [];
|
|
|
|
const preloadAlertCount = wcSettings.alertCount && parseInt( wcSettings.alertCount );
|
2019-03-04 13:52:20 +00:00
|
|
|
|
2019-03-12 13:13:20 +00:00
|
|
|
if ( preloadAlertCount > 0 && this.props.isLoading ) {
|
2019-03-08 16:17:48 +00:00
|
|
|
return <StoreAlertsPlaceholder hasMultipleAlerts={ preloadAlertCount > 1 } />;
|
|
|
|
} else if ( 0 === alerts.length ) {
|
2019-03-04 13:52:20 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-02-20 16:18:10 +00:00
|
|
|
const { currentIndex } = this.state;
|
|
|
|
const numberOfAlerts = alerts.length;
|
2019-03-08 16:17:48 +00:00
|
|
|
const alert = alerts[ currentIndex ];
|
|
|
|
const type = alert.type;
|
2019-05-22 08:38:25 +00:00
|
|
|
const className = classnames( 'woocommerce-store-alerts', 'woocommerce-analytics__card', {
|
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-12 13:13:20 +00:00
|
|
|
|
2019-03-08 16:17:48 +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 }
|
2019-03-13 17:14:02 +00:00
|
|
|
label={ __( 'Previous Alert', 'woocommerce-admin' ) }
|
2019-03-04 13:52:20 +00:00
|
|
|
/>
|
|
|
|
<span
|
|
|
|
className="woocommerce-store-alerts__pagination-label"
|
|
|
|
role="status"
|
|
|
|
aria-live="polite"
|
|
|
|
>
|
|
|
|
{ interpolateComponents( {
|
2019-03-13 17:14:02 +00:00
|
|
|
mixedString: __( '{{current /}} of {{total /}}', 'woocommerce-admin' ),
|
2019-03-04 13:52:20 +00:00
|
|
|
components: {
|
|
|
|
current: <Fragment>{ currentIndex + 1 }</Fragment>,
|
|
|
|
total: <Fragment>{ numberOfAlerts }</Fragment>,
|
|
|
|
},
|
|
|
|
} ) }
|
|
|
|
</span>
|
|
|
|
<IconButton
|
|
|
|
icon="arrow-right-alt2"
|
|
|
|
onClick={ this.nextAlert }
|
|
|
|
disabled={ numberOfAlerts - 1 === currentIndex }
|
2019-03-13 17:14:02 +00:00
|
|
|
label={ __( 'Next Alert', 'woocommerce-admin' ) }
|
2019-03-04 13:52:20 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
className="woocommerce-store-alerts__message"
|
|
|
|
dangerouslySetInnerHTML={ sanitizeHTML( alert.content ) }
|
|
|
|
/>
|
2019-03-18 19:05:44 +00:00
|
|
|
{ this.renderActions( alert ) }
|
2019-03-04 13:52:20 +00:00
|
|
|
</Card>
|
2019-02-19 21:07:19 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 17:20:28 +00:00
|
|
|
export default compose(
|
|
|
|
withSelect( select => {
|
2019-03-12 13:13:20 +00:00
|
|
|
const { getNotes, isGetNotesRequesting } = select( 'wc-api' );
|
2019-03-01 17:20:28 +00:00
|
|
|
const alertsQuery = {
|
|
|
|
page: 1,
|
|
|
|
per_page: QUERY_DEFAULTS.pageSize,
|
|
|
|
type: 'error,update',
|
2019-03-12 13:13:20 +00:00
|
|
|
status: 'unactioned',
|
2019-03-01 17:20:28 +00:00
|
|
|
};
|
2019-02-20 15:25:56 +00:00
|
|
|
|
2019-03-12 13:13:20 +00:00
|
|
|
// Filter out notes that may have been marked actioned or not delayed after the initial request
|
|
|
|
const filterNotes = note => 'unactioned' === note.status;
|
|
|
|
const alerts = getNotes( alertsQuery ).filter( filterNotes );
|
|
|
|
|
|
|
|
const isLoading = isGetNotesRequesting( alertsQuery );
|
2019-02-20 16:37:03 +00:00
|
|
|
|
2019-03-12 13:13:20 +00:00
|
|
|
return {
|
|
|
|
alerts,
|
|
|
|
isLoading,
|
|
|
|
};
|
|
|
|
} ),
|
|
|
|
withDispatch( dispatch => {
|
2019-05-24 20:22:46 +00:00
|
|
|
const { triggerNoteAction, updateNote } = dispatch( 'wc-api' );
|
|
|
|
|
2019-03-12 13:13:20 +00:00
|
|
|
return {
|
2019-05-24 20:22:46 +00:00
|
|
|
triggerNoteAction,
|
2019-03-12 13:13:20 +00:00
|
|
|
updateNote,
|
|
|
|
};
|
2019-03-01 17:20:28 +00:00
|
|
|
} )
|
|
|
|
)( StoreAlerts );
|