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';
|
2018-10-12 19:20:48 +00:00
|
|
|
import { withSelect } 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';
|
2018-07-16 18:37:25 +00:00
|
|
|
|
|
|
|
class InboxPanel extends Component {
|
|
|
|
render() {
|
2018-10-16 22:34:36 +00:00
|
|
|
const { isError, isRequesting, notes } = this.props;
|
|
|
|
|
|
|
|
if ( isError ) {
|
|
|
|
const title = __( 'There was an error getting your inbox. Please try again.', 'wc-admin' );
|
|
|
|
const actionLabel = __( 'Reload', 'wc-admin' );
|
|
|
|
const actionCallback = () => {
|
|
|
|
// TODO Add tracking for how often an error is displayed, and the reload action is clicked.
|
|
|
|
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-20 18:41:39 +00:00
|
|
|
const getButtonsFromActions = actions => {
|
|
|
|
if ( ! actions ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return actions.map( action => (
|
2018-10-12 20:57:49 +00:00
|
|
|
<Button disabled isDefault href={ action.url }>
|
2018-07-20 18:41:39 +00:00
|
|
|
{ action.label }
|
|
|
|
</Button>
|
|
|
|
) );
|
|
|
|
};
|
|
|
|
|
2018-10-12 20:56:45 +00:00
|
|
|
const notesArray = Object.keys( notes ).map( key => notes[ key ] );
|
|
|
|
|
2018-07-16 18:37:25 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<ActivityHeader title={ __( 'Inbox', 'wc-admin' ) } />
|
|
|
|
<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 }
|
|
|
|
/>
|
|
|
|
) : (
|
2018-10-16 20:11:23 +00:00
|
|
|
notesArray.map( note => (
|
2018-07-20 18:41:39 +00:00
|
|
|
<ActivityCard
|
2018-10-12 20:56:45 +00:00
|
|
|
key={ note.id }
|
2018-07-20 18:41:39 +00:00
|
|
|
className="woocommerce-inbox-activity-card"
|
2018-10-12 20:56:45 +00:00
|
|
|
title={ note.title }
|
|
|
|
date={ note.date_created }
|
|
|
|
icon={ <Gridicon icon={ note.icon } size={ 48 } /> }
|
|
|
|
unread={ 'unread' === note.status }
|
|
|
|
actions={ getButtonsFromActions( note.actions ) }
|
2018-07-20 18:41:39 +00:00
|
|
|
>
|
2018-10-12 20:56:45 +00:00
|
|
|
<span dangerouslySetInnerHTML={ sanitizeHTML( note.content ) } />
|
2018-07-20 18:41:39 +00:00
|
|
|
</ActivityCard>
|
|
|
|
) )
|
|
|
|
) }
|
2018-07-16 18:37:25 +00:00
|
|
|
</Section>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 19:20:48 +00:00
|
|
|
export default compose(
|
|
|
|
withSelect( select => {
|
2018-10-16 19:46:00 +00:00
|
|
|
const { getNotes, isGetNotesError, isGetNotesRequesting } = select( 'wc-admin' );
|
|
|
|
const inboxQuery = {
|
|
|
|
page: 1,
|
|
|
|
per_page: 25,
|
|
|
|
};
|
|
|
|
|
|
|
|
const notes = getNotes( inboxQuery );
|
|
|
|
const isError = isGetNotesError( inboxQuery );
|
|
|
|
const isRequesting = isGetNotesRequesting( inboxQuery );
|
|
|
|
|
|
|
|
return { notes, isError, isRequesting };
|
2018-10-12 19:20:48 +00:00
|
|
|
} )
|
|
|
|
)( InboxPanel );
|