2018-07-09 15:46:31 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-08-01 12:21:51 +00:00
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2019-03-25 09:08:25 +00:00
|
|
|
import classnames from 'classnames';
|
2018-07-20 18:24:39 +00:00
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
2020-09-03 21:45:40 +00:00
|
|
|
import { withSelect } from '@wordpress/data';
|
2019-04-30 09:40:10 +00:00
|
|
|
import { Button } from '@wordpress/components';
|
2020-11-18 20:52:24 +00:00
|
|
|
import CheckmarkIcon from 'gridicons/dist/checkmark';
|
|
|
|
import TimeIcon from 'gridicons/dist/time';
|
2018-08-01 12:21:51 +00:00
|
|
|
import interpolateComponents from 'interpolate-components';
|
2019-10-10 22:31:28 +00:00
|
|
|
import { get, isNull } from 'lodash';
|
2018-12-12 13:35:56 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-07-09 15:46:31 +00:00
|
|
|
|
2018-09-21 15:19:05 +00:00
|
|
|
import {
|
2018-12-12 13:35:56 +00:00
|
|
|
EmptyContent,
|
2018-09-21 15:19:05 +00:00
|
|
|
Gravatar,
|
|
|
|
Link,
|
|
|
|
ProductImage,
|
|
|
|
ReviewRating,
|
|
|
|
Section,
|
|
|
|
} from '@woocommerce/components';
|
2019-11-22 17:07:26 +00:00
|
|
|
import { getAdminLink } from '@woocommerce/wc-admin-settings';
|
2020-09-03 21:45:40 +00:00
|
|
|
import { REVIEWS_STORE_NAME, QUERY_DEFAULTS } from '@woocommerce/data';
|
2020-08-20 04:59:52 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2018-07-09 15:46:31 +00:00
|
|
|
|
2018-12-12 13:35:56 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { ActivityCard, ActivityCardPlaceholder } from '../activity-card';
|
|
|
|
import ActivityHeader from '../activity-header';
|
2020-08-13 02:05:22 +00:00
|
|
|
import sanitizeHTML from '../../../lib/sanitize-html';
|
2018-08-01 12:21:51 +00:00
|
|
|
|
2018-07-09 15:46:31 +00:00
|
|
|
class ReviewsPanel extends Component {
|
2019-03-21 11:35:46 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.mountTime = new Date().getTime();
|
|
|
|
}
|
|
|
|
|
2020-08-03 16:38:57 +00:00
|
|
|
recordReviewEvent( eventName ) {
|
|
|
|
recordEvent( `activity_panel_reviews_${ eventName }`, {} );
|
|
|
|
}
|
|
|
|
|
2019-03-21 11:35:46 +00:00
|
|
|
renderReview( review, props ) {
|
|
|
|
const { lastRead } = props;
|
2018-12-12 13:35:56 +00:00
|
|
|
const product =
|
2020-02-14 02:23:21 +00:00
|
|
|
( review &&
|
|
|
|
review._embedded &&
|
|
|
|
review._embedded.up &&
|
|
|
|
review._embedded.up[ 0 ] ) ||
|
|
|
|
null;
|
2018-12-12 13:35:56 +00:00
|
|
|
|
|
|
|
if ( isNull( product ) ) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-08-01 12:21:51 +00:00
|
|
|
|
|
|
|
const title = interpolateComponents( {
|
|
|
|
mixedString: sprintf(
|
|
|
|
__(
|
|
|
|
'{{productLink}}%s{{/productLink}} reviewed by {{authorLink}}%s{{/authorLink}}',
|
2019-03-13 17:14:02 +00:00
|
|
|
'woocommerce-admin'
|
2018-08-01 12:21:51 +00:00
|
|
|
),
|
|
|
|
product.name,
|
|
|
|
review.reviewer
|
|
|
|
),
|
|
|
|
components: {
|
2020-02-14 02:23:21 +00:00
|
|
|
productLink: (
|
2020-08-03 16:38:57 +00:00
|
|
|
<Link
|
|
|
|
href={ product.permalink }
|
|
|
|
onClick={ () => this.recordReviewEvent( 'product' ) }
|
|
|
|
type="external"
|
|
|
|
/>
|
2020-02-14 02:23:21 +00:00
|
|
|
),
|
|
|
|
authorLink: (
|
|
|
|
<Link
|
|
|
|
href={ 'mailto:' + review.reviewer_email }
|
2020-08-03 16:38:57 +00:00
|
|
|
onClick={ () => this.recordReviewEvent( 'customer' ) }
|
2020-02-14 02:23:21 +00:00
|
|
|
type="external"
|
|
|
|
/>
|
|
|
|
),
|
2018-08-01 12:21:51 +00:00
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
const subtitle = (
|
2018-07-20 18:24:39 +00:00
|
|
|
<Fragment>
|
2018-08-01 12:21:51 +00:00
|
|
|
<ReviewRating review={ review } />
|
|
|
|
{ review.verified && (
|
|
|
|
<span className="woocommerce-review-activity-card__verified">
|
2020-11-18 20:52:24 +00:00
|
|
|
<CheckmarkIcon size={ 18 } />
|
2019-03-13 17:14:02 +00:00
|
|
|
{ __( 'Verified customer', 'woocommerce-admin' ) }
|
2018-08-01 12:21:51 +00:00
|
|
|
</span>
|
|
|
|
) }
|
|
|
|
</Fragment>
|
|
|
|
);
|
2018-07-25 14:25:08 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const productImage =
|
|
|
|
get( product, [ 'images', 0 ] ) || get( product, [ 'image' ] );
|
2019-03-25 09:08:25 +00:00
|
|
|
const productImageClasses = classnames(
|
|
|
|
'woocommerce-review-activity-card__image-overlay__product',
|
|
|
|
{
|
|
|
|
'is-placeholder': ! productImage || ! productImage.src,
|
|
|
|
}
|
|
|
|
);
|
2018-08-01 12:21:51 +00:00
|
|
|
const icon = (
|
|
|
|
<div className="woocommerce-review-activity-card__image-overlay">
|
|
|
|
<Gravatar user={ review.reviewer_email } size={ 24 } />
|
2019-03-25 09:08:25 +00:00
|
|
|
<div className={ productImageClasses }>
|
|
|
|
<ProductImage product={ product } />
|
|
|
|
</div>
|
2018-08-01 12:21:51 +00:00
|
|
|
</div>
|
|
|
|
);
|
2018-07-25 14:25:08 +00:00
|
|
|
|
2019-10-10 22:31:28 +00:00
|
|
|
const manageReviewEvent = {
|
|
|
|
date: review.date_created_gmt,
|
|
|
|
status: review.status,
|
2018-08-01 12:21:51 +00:00
|
|
|
};
|
2018-07-25 14:25:08 +00:00
|
|
|
|
2019-10-10 22:31:28 +00:00
|
|
|
const cardActions = (
|
|
|
|
<Button
|
2020-06-11 19:22:20 +00:00
|
|
|
isSecondary
|
2020-02-14 02:23:21 +00:00
|
|
|
onClick={ () =>
|
|
|
|
recordEvent( 'review_manage_click', manageReviewEvent )
|
|
|
|
}
|
|
|
|
href={ getAdminLink(
|
|
|
|
'comment.php?action=editcomment&c=' + review.id
|
|
|
|
) }
|
2019-10-10 22:31:28 +00:00
|
|
|
>
|
|
|
|
{ __( 'Manage', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
|
2018-08-01 12:21:51 +00:00
|
|
|
return (
|
|
|
|
<ActivityCard
|
|
|
|
className="woocommerce-review-activity-card"
|
|
|
|
key={ review.id }
|
|
|
|
title={ title }
|
|
|
|
subtitle={ subtitle }
|
2019-04-09 07:50:00 +00:00
|
|
|
date={ review.date_created_gmt }
|
2018-08-01 12:21:51 +00:00
|
|
|
icon={ icon }
|
2019-10-10 22:31:28 +00:00
|
|
|
actions={ cardActions }
|
2019-03-21 11:35:46 +00:00
|
|
|
unread={
|
2019-03-28 22:20:17 +00:00
|
|
|
review.status === 'hold' ||
|
2019-03-21 11:35:46 +00:00
|
|
|
! lastRead ||
|
|
|
|
! review.date_created_gmt ||
|
2020-02-14 02:23:21 +00:00
|
|
|
new Date( review.date_created_gmt + 'Z' ).getTime() >
|
|
|
|
lastRead
|
2019-03-21 11:35:46 +00:00
|
|
|
}
|
2018-08-01 12:21:51 +00:00
|
|
|
>
|
2020-02-14 02:23:21 +00:00
|
|
|
<span
|
|
|
|
dangerouslySetInnerHTML={ sanitizeHTML( review.review ) }
|
|
|
|
/>
|
2018-08-01 12:21:51 +00:00
|
|
|
</ActivityCard>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-30 09:40:10 +00:00
|
|
|
renderEmptyMessage() {
|
|
|
|
const { lastApprovedReviewTime } = this.props;
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const title = __(
|
|
|
|
'You have no reviews to moderate',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
2019-04-30 09:40:10 +00:00
|
|
|
let buttonUrl = '';
|
|
|
|
let buttonTarget = '';
|
|
|
|
let buttonText = '';
|
|
|
|
let content = '';
|
2020-08-03 16:38:57 +00:00
|
|
|
let eventName = 'learn_more';
|
2019-04-30 09:40:10 +00:00
|
|
|
|
|
|
|
if ( lastApprovedReviewTime ) {
|
|
|
|
const now = new Date();
|
|
|
|
const DAY = 24 * 60 * 60 * 1000;
|
|
|
|
if ( ( now.getTime() - lastApprovedReviewTime ) / DAY > 30 ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
buttonUrl =
|
|
|
|
'https://woocommerce.com/posts/reviews-woocommerce-best-practices/';
|
2019-04-30 09:40:10 +00:00
|
|
|
buttonTarget = '_blank';
|
|
|
|
buttonText = __( 'Learn more', 'woocommerce-admin' );
|
|
|
|
content = (
|
|
|
|
<Fragment>
|
|
|
|
<p>
|
|
|
|
{ __(
|
|
|
|
"We noticed that it's been a while since your products had any reviews.",
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
{ __(
|
|
|
|
'Take some time to learn about best practices for collecting and using your reviews.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
</p>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
} else {
|
2020-02-14 02:23:21 +00:00
|
|
|
buttonUrl = getAdminLink(
|
|
|
|
'edit-comments.php?comment_type=review'
|
|
|
|
);
|
2019-04-30 09:40:10 +00:00
|
|
|
buttonText = __( 'View all Reviews', 'woocommerce-admin' );
|
|
|
|
content = (
|
|
|
|
<p>
|
|
|
|
{ __(
|
|
|
|
/* eslint-disable max-len */
|
|
|
|
"Awesome, you've moderated all of your product reviews. How about responding to some of those negative reviews?",
|
|
|
|
'woocommerce-admin'
|
|
|
|
/* eslint-enable */
|
|
|
|
) }
|
|
|
|
</p>
|
|
|
|
);
|
2020-08-03 16:38:57 +00:00
|
|
|
eventName = 'view_reviews';
|
2019-04-30 09:40:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-02-14 02:23:21 +00:00
|
|
|
buttonUrl =
|
|
|
|
'https://woocommerce.com/posts/reviews-woocommerce-best-practices/';
|
2019-04-30 09:40:10 +00:00
|
|
|
buttonTarget = '_blank';
|
|
|
|
buttonText = __( 'Learn more', 'woocommerce-admin' );
|
|
|
|
content = (
|
|
|
|
<Fragment>
|
|
|
|
<p>
|
2020-02-14 02:23:21 +00:00
|
|
|
{ __(
|
|
|
|
"Your customers haven't started reviewing your products.",
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
2019-04-30 09:40:10 +00:00
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
{ __(
|
|
|
|
'Take some time to learn about best practices for collecting and using your reviews.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
</p>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ActivityCard
|
2019-06-12 21:51:08 +00:00
|
|
|
className="woocommerce-empty-activity-card"
|
2019-04-30 09:40:10 +00:00
|
|
|
title={ title }
|
2020-11-18 20:52:24 +00:00
|
|
|
icon={ <TimeIcon size={ 48 } /> }
|
2019-04-30 09:40:10 +00:00
|
|
|
actions={
|
2020-02-14 02:23:21 +00:00
|
|
|
<Button
|
|
|
|
href={ buttonUrl }
|
|
|
|
target={ buttonTarget }
|
2020-06-11 19:22:20 +00:00
|
|
|
isSecondary
|
2020-08-03 16:38:57 +00:00
|
|
|
onClick={ () => this.recordReviewEvent( eventName ) }
|
2020-02-14 02:23:21 +00:00
|
|
|
>
|
2019-04-30 09:40:10 +00:00
|
|
|
{ buttonText }
|
|
|
|
</Button>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{ content }
|
|
|
|
</ActivityCard>
|
|
|
|
);
|
2019-03-21 11:35:46 +00:00
|
|
|
}
|
|
|
|
|
2018-08-01 12:21:51 +00:00
|
|
|
render() {
|
2018-12-12 13:35:56 +00:00
|
|
|
const { isError, isRequesting, reviews } = this.props;
|
|
|
|
|
|
|
|
if ( isError ) {
|
2019-03-13 17:14:02 +00:00
|
|
|
const title = __(
|
|
|
|
'There was an error getting your reviews. Please try again.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
|
|
|
const actionLabel = __( 'Reload', 'woocommerce-admin' );
|
2018-12-12 13:35:56 +00:00
|
|
|
const actionCallback = () => {
|
|
|
|
window.location.reload();
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<EmptyContent
|
|
|
|
title={ title }
|
|
|
|
actionLabel={ actionLabel }
|
|
|
|
actionURL={ null }
|
|
|
|
actionCallback={ actionCallback }
|
|
|
|
/>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-30 09:40:10 +00:00
|
|
|
const title =
|
|
|
|
isRequesting || reviews.length
|
|
|
|
? __( 'Reviews', 'woocommerce-admin' )
|
|
|
|
: __( 'No reviews to moderate', 'woocommerce-admin' );
|
|
|
|
|
2018-08-01 12:21:51 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
2019-04-30 09:40:10 +00:00
|
|
|
<ActivityHeader title={ title } />
|
2018-08-01 12:21:51 +00:00
|
|
|
<Section>
|
2018-12-12 13:35:56 +00:00
|
|
|
{ isRequesting ? (
|
2019-04-30 09:40:10 +00:00
|
|
|
<ActivityCardPlaceholder
|
|
|
|
className="woocommerce-review-activity-card"
|
|
|
|
hasAction
|
|
|
|
hasDate
|
|
|
|
lines={ 2 }
|
|
|
|
/>
|
2018-08-01 12:21:51 +00:00
|
|
|
) : (
|
2019-03-21 11:35:46 +00:00
|
|
|
<Fragment>
|
2019-04-30 09:40:10 +00:00
|
|
|
{ reviews.length
|
2020-02-14 02:23:21 +00:00
|
|
|
? reviews.map( ( review ) =>
|
|
|
|
this.renderReview( review, this.props )
|
|
|
|
)
|
2019-04-30 09:40:10 +00:00
|
|
|
: this.renderEmptyMessage() }
|
2019-03-21 11:35:46 +00:00
|
|
|
</Fragment>
|
2018-08-01 12:21:51 +00:00
|
|
|
) }
|
|
|
|
</Section>
|
2018-07-20 18:24:39 +00:00
|
|
|
</Fragment>
|
|
|
|
);
|
2018-07-09 15:46:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:35:56 +00:00
|
|
|
ReviewsPanel.propTypes = {
|
|
|
|
reviews: PropTypes.array.isRequired,
|
|
|
|
isError: PropTypes.bool,
|
|
|
|
isRequesting: PropTypes.bool,
|
|
|
|
};
|
|
|
|
|
|
|
|
ReviewsPanel.defaultProps = {
|
|
|
|
reviews: [],
|
|
|
|
isError: false,
|
|
|
|
isRequesting: false,
|
|
|
|
};
|
|
|
|
|
2019-04-30 09:40:10 +00:00
|
|
|
export default withSelect( ( select, props ) => {
|
|
|
|
const { hasUnapprovedReviews } = props;
|
2020-08-11 00:16:27 +00:00
|
|
|
const { getReviews, getReviewsError, isResolving } = select(
|
|
|
|
REVIEWS_STORE_NAME
|
2020-02-14 02:23:21 +00:00
|
|
|
);
|
2019-04-30 09:40:10 +00:00
|
|
|
let reviews = [];
|
|
|
|
let isError = false;
|
|
|
|
let isRequesting = false;
|
|
|
|
let lastApprovedReviewTime = null;
|
|
|
|
if ( hasUnapprovedReviews ) {
|
2018-12-12 13:35:56 +00:00
|
|
|
const reviewsQuery = {
|
|
|
|
page: 1,
|
|
|
|
per_page: QUERY_DEFAULTS.pageSize,
|
2019-04-30 09:40:10 +00:00
|
|
|
status: 'hold',
|
2018-12-12 13:35:56 +00:00
|
|
|
_embed: 1,
|
|
|
|
};
|
2019-04-30 09:40:10 +00:00
|
|
|
reviews = getReviews( reviewsQuery );
|
|
|
|
isError = Boolean( getReviewsError( reviewsQuery ) );
|
2020-08-11 00:16:27 +00:00
|
|
|
isRequesting = isResolving( 'getReviews', [ reviewsQuery ] );
|
2019-04-30 09:40:10 +00:00
|
|
|
} else {
|
|
|
|
const approvedReviewsQuery = {
|
|
|
|
page: 1,
|
|
|
|
per_page: 1,
|
|
|
|
status: 'approved',
|
|
|
|
_embed: 1,
|
|
|
|
};
|
|
|
|
const approvedReviews = getReviews( approvedReviewsQuery );
|
|
|
|
if ( approvedReviews.length ) {
|
|
|
|
const lastApprovedReview = approvedReviews[ 0 ];
|
|
|
|
if ( lastApprovedReview.date_created_gmt ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
const creationDate = new Date(
|
|
|
|
lastApprovedReview.date_created_gmt
|
|
|
|
);
|
2019-04-30 09:40:10 +00:00
|
|
|
lastApprovedReviewTime = creationDate.getTime();
|
|
|
|
}
|
|
|
|
}
|
2018-12-12 13:35:56 +00:00
|
|
|
|
2019-04-30 09:40:10 +00:00
|
|
|
isError = Boolean( getReviewsError( approvedReviewsQuery ) );
|
2020-08-11 00:16:27 +00:00
|
|
|
isRequesting = isResolving( 'getReviews', [ approvedReviewsQuery ] );
|
2019-04-30 09:40:10 +00:00
|
|
|
}
|
2019-03-21 11:35:46 +00:00
|
|
|
|
2019-04-30 09:40:10 +00:00
|
|
|
return {
|
|
|
|
reviews,
|
|
|
|
isError,
|
|
|
|
isRequesting,
|
|
|
|
lastApprovedReviewTime,
|
|
|
|
};
|
|
|
|
} )( ReviewsPanel );
|