2018-07-16 18:37:25 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2018-07-20 18:41:39 +00:00
|
|
|
import { Button } from '@wordpress/components';
|
2018-07-16 18:37:25 +00:00
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
2018-10-12 19:20:48 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
2018-07-16 18:37:25 +00:00
|
|
|
import Gridicon from 'gridicons';
|
2019-03-21 02:54:36 +00:00
|
|
|
import { withDispatch } from '@wordpress/data';
|
2018-07-16 18:37:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2018-07-20 18:41:39 +00:00
|
|
|
import { ActivityCard, ActivityCardPlaceholder } from '../activity-card';
|
2018-07-16 18:37:25 +00:00
|
|
|
import ActivityHeader from '../activity-header';
|
2018-10-16 22:34:36 +00:00
|
|
|
import { EmptyContent, Section } from '@woocommerce/components';
|
2018-10-12 20:40:16 +00:00
|
|
|
import sanitizeHTML from 'lib/sanitize-html';
|
2019-01-30 07:22:10 +00:00
|
|
|
import { QUERY_DEFAULTS } from 'wc-api/constants';
|
2019-03-21 02:54:36 +00:00
|
|
|
import withSelect from 'wc-api/with-select';
|
2019-05-28 19:56:50 +00:00
|
|
|
import classnames from 'classnames';
|
2018-07-16 18:37:25 +00:00
|
|
|
|
|
|
|
class InboxPanel extends Component {
|
2019-03-21 02:54:36 +00:00
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
|
|
|
this.mountTime = Date.now();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
const userDataFields = {
|
|
|
|
[ 'activity_panel_inbox_last_read' ]: this.mountTime,
|
|
|
|
};
|
|
|
|
this.props.updateCurrentUserData( userDataFields );
|
|
|
|
}
|
|
|
|
|
2019-04-15 07:06:34 +00:00
|
|
|
renderEmptyCard() {
|
|
|
|
return (
|
|
|
|
<ActivityCard
|
|
|
|
className="woocommerce-empty-review-activity-card"
|
|
|
|
title={ __( 'Your inbox is empty', 'woocommerce-admin' ) }
|
|
|
|
icon={ <Gridicon icon="checkmark" size={ 48 } /> }
|
|
|
|
>
|
|
|
|
{ __(
|
|
|
|
'As things begin to happen in your store your inbox will start to fill up. ' +
|
|
|
|
"You'll see things like achievements, new feature announcements, extension recommendations and more!",
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
</ActivityCard>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderNotes() {
|
2019-05-24 20:22:46 +00:00
|
|
|
const { lastRead, notes, triggerNoteAction } = this.props;
|
2019-04-15 07:06:34 +00:00
|
|
|
|
|
|
|
if ( 0 === Object.keys( notes ).length ) {
|
|
|
|
return this.renderEmptyCard();
|
|
|
|
}
|
|
|
|
|
2019-05-24 20:22:46 +00:00
|
|
|
const getButtonsFromActions = note => {
|
|
|
|
if ( ! note.actions ) {
|
2019-04-15 07:06:34 +00:00
|
|
|
return [];
|
|
|
|
}
|
2019-05-24 20:22:46 +00:00
|
|
|
return note.actions.map( action => (
|
|
|
|
<Button
|
|
|
|
isDefault
|
|
|
|
isPrimary={ action.primary }
|
|
|
|
href={ action.url || undefined }
|
|
|
|
onClick={ () => triggerNoteAction( note.id, action.id ) }
|
|
|
|
>
|
2019-04-15 07:06:34 +00:00
|
|
|
{ action.label }
|
|
|
|
</Button>
|
|
|
|
) );
|
|
|
|
};
|
|
|
|
|
|
|
|
const notesArray = Object.keys( notes ).map( key => notes[ key ] );
|
|
|
|
|
|
|
|
return notesArray.map( note => (
|
|
|
|
<ActivityCard
|
|
|
|
key={ note.id }
|
2019-05-28 19:56:50 +00:00
|
|
|
className={ classnames( 'woocommerce-inbox-activity-card', {
|
|
|
|
actioned: 'unactioned' !== note.status,
|
|
|
|
} ) }
|
2019-04-15 07:06:34 +00:00
|
|
|
title={ note.title }
|
|
|
|
date={ note.date_created_gmt }
|
|
|
|
icon={ <Gridicon icon={ note.icon } size={ 48 } /> }
|
|
|
|
unread={
|
|
|
|
! lastRead ||
|
|
|
|
! note.date_created_gmt ||
|
|
|
|
new Date( note.date_created_gmt + 'Z' ).getTime() > lastRead
|
|
|
|
}
|
2019-05-24 20:22:46 +00:00
|
|
|
actions={ getButtonsFromActions( note ) }
|
2019-04-15 07:06:34 +00:00
|
|
|
>
|
|
|
|
<span dangerouslySetInnerHTML={ sanitizeHTML( note.content ) } />
|
|
|
|
</ActivityCard>
|
|
|
|
) );
|
|
|
|
}
|
|
|
|
|
2018-07-16 18:37:25 +00:00
|
|
|
render() {
|
2019-04-15 07:06:34 +00:00
|
|
|
const { isError, isRequesting } = this.props;
|
2018-10-16 22:34:36 +00:00
|
|
|
|
|
|
|
if ( isError ) {
|
2019-03-13 17:14:02 +00:00
|
|
|
const title = __(
|
|
|
|
'There was an error getting your inbox. Please try again.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
|
|
|
const actionLabel = __( 'Reload', 'woocommerce-admin' );
|
2018-10-16 22:34:36 +00:00
|
|
|
const actionCallback = () => {
|
2019-02-06 06:41:53 +00:00
|
|
|
// @todo Add tracking for how often an error is displayed, and the reload action is clicked.
|
2018-10-16 22:34:36 +00:00
|
|
|
window.location.reload();
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<EmptyContent
|
|
|
|
title={ title }
|
|
|
|
actionLabel={ actionLabel }
|
|
|
|
actionURL={ null }
|
|
|
|
actionCallback={ actionCallback }
|
|
|
|
/>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
2018-10-12 19:20:48 +00:00
|
|
|
|
2018-07-16 18:37:25 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
2019-03-13 17:14:02 +00:00
|
|
|
<ActivityHeader title={ __( 'Inbox', 'woocommerce-admin' ) } />
|
2018-07-16 18:37:25 +00:00
|
|
|
<Section>
|
2018-10-16 19:46:00 +00:00
|
|
|
{ isRequesting ? (
|
2018-07-20 18:41:39 +00:00
|
|
|
<ActivityCardPlaceholder
|
|
|
|
className="woocommerce-inbox-activity-card"
|
|
|
|
hasAction
|
|
|
|
hasDate
|
|
|
|
lines={ 2 }
|
|
|
|
/>
|
|
|
|
) : (
|
2019-04-15 07:06:34 +00:00
|
|
|
this.renderNotes()
|
2018-07-20 18:41:39 +00:00
|
|
|
) }
|
2018-07-16 18:37:25 +00:00
|
|
|
</Section>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 19:20:48 +00:00
|
|
|
export default compose(
|
|
|
|
withSelect( select => {
|
2019-03-21 02:54:36 +00:00
|
|
|
const { getCurrentUserData, getNotes, getNotesError, isGetNotesRequesting } = select(
|
|
|
|
'wc-api'
|
|
|
|
);
|
|
|
|
const userData = getCurrentUserData();
|
2018-10-16 19:46:00 +00:00
|
|
|
const inboxQuery = {
|
|
|
|
page: 1,
|
2018-10-26 08:19:39 +00:00
|
|
|
per_page: QUERY_DEFAULTS.pageSize,
|
2018-12-11 01:00:57 +00:00
|
|
|
type: 'info,warning',
|
2019-04-01 02:53:34 +00:00
|
|
|
orderby: 'date',
|
|
|
|
order: 'desc',
|
2019-05-28 19:56:50 +00:00
|
|
|
status: 'unactioned',
|
2018-10-16 19:46:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const notes = getNotes( inboxQuery );
|
2018-12-15 12:38:54 +00:00
|
|
|
const isError = Boolean( getNotesError( inboxQuery ) );
|
2018-10-16 19:46:00 +00:00
|
|
|
const isRequesting = isGetNotesRequesting( inboxQuery );
|
|
|
|
|
2019-03-21 02:54:36 +00:00
|
|
|
return { notes, isError, isRequesting, lastRead: userData.activity_panel_inbox_last_read };
|
|
|
|
} ),
|
|
|
|
withDispatch( dispatch => {
|
2019-05-24 20:22:46 +00:00
|
|
|
const { updateCurrentUserData, triggerNoteAction } = dispatch( 'wc-api' );
|
2019-03-21 02:54:36 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
updateCurrentUserData,
|
2019-05-24 20:22:46 +00:00
|
|
|
triggerNoteAction,
|
2019-03-21 02:54:36 +00:00
|
|
|
};
|
2018-10-12 19:20:48 +00:00
|
|
|
} )
|
|
|
|
)( InboxPanel );
|