2018-07-09 15:46:31 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* 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';
|
2019-04-30 09:40:10 +00:00
|
|
|
import { Button } from '@wordpress/components';
|
2018-07-25 14:25:08 +00:00
|
|
|
import Gridicon from 'gridicons';
|
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-12-12 13:35:56 +00:00
|
|
|
* WooCommerce dependencies
|
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';
|
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';
|
2019-04-30 09:40:10 +00:00
|
|
|
import { QUERY_DEFAULTS } from 'wc-api/constants';
|
2018-12-12 13:35:56 +00:00
|
|
|
import sanitizeHTML from 'lib/sanitize-html';
|
|
|
|
import withSelect from 'wc-api/with-select';
|
2019-10-10 22:31:28 +00:00
|
|
|
import { recordEvent } from 'lib/tracks';
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
renderReview( review, props ) {
|
|
|
|
const { lastRead } = props;
|
2018-12-12 13:35:56 +00:00
|
|
|
const product =
|
|
|
|
( review && review._embedded && review._embedded.up && review._embedded.up[ 0 ] ) || null;
|
|
|
|
|
|
|
|
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: {
|
|
|
|
productLink: <Link href={ product.permalink } type="external" />,
|
|
|
|
authorLink: <Link href={ 'mailto:' + review.reviewer_email } type="external" />,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
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">
|
|
|
|
<Gridicon icon="checkmark" 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
|
|
|
|
2019-03-25 09:08:25 +00:00
|
|
|
const productImage = get( product, [ 'images', 0 ] ) || get( product, [ 'image' ] );
|
|
|
|
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
|
|
|
|
isDefault
|
|
|
|
onClick={ () => recordEvent( 'review_manage_click', manageReviewEvent ) }
|
|
|
|
href={ getAdminLink( 'comment.php?action=editcomment&c=' + review.id ) }
|
|
|
|
>
|
|
|
|
{ __( '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 ||
|
|
|
|
new Date( review.date_created_gmt + 'Z' ).getTime() > lastRead
|
|
|
|
}
|
2018-08-01 12:21:51 +00:00
|
|
|
>
|
2018-12-12 13:35:56 +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;
|
|
|
|
|
|
|
|
const title = __( 'You have no reviews to moderate', 'woocommerce-admin' );
|
|
|
|
let buttonUrl = '';
|
|
|
|
let buttonTarget = '';
|
|
|
|
let buttonText = '';
|
|
|
|
let content = '';
|
|
|
|
|
|
|
|
if ( lastApprovedReviewTime ) {
|
|
|
|
const now = new Date();
|
|
|
|
const DAY = 24 * 60 * 60 * 1000;
|
|
|
|
if ( ( now.getTime() - lastApprovedReviewTime ) / DAY > 30 ) {
|
|
|
|
buttonUrl = 'https://woocommerce.com/posts/reviews-woocommerce-best-practices/';
|
|
|
|
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 {
|
|
|
|
buttonUrl = getAdminLink( 'edit-comments.php' );
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buttonUrl = 'https://woocommerce.com/posts/reviews-woocommerce-best-practices/';
|
|
|
|
buttonTarget = '_blank';
|
|
|
|
buttonText = __( 'Learn more', 'woocommerce-admin' );
|
|
|
|
content = (
|
|
|
|
<Fragment>
|
|
|
|
<p>
|
|
|
|
{ __( "Your customers haven't started reviewing your products.", 'woocommerce-admin' ) }
|
|
|
|
</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 }
|
|
|
|
icon={ <Gridicon icon="time" size={ 48 } /> }
|
|
|
|
actions={
|
|
|
|
<Button href={ buttonUrl } target={ buttonTarget } isDefault>
|
|
|
|
{ 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
|
|
|
|
? reviews.map( review => this.renderReview( review, this.props ) )
|
|
|
|
: 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;
|
|
|
|
const { getReviews, getReviewsError, isGetReviewsRequesting } = select( 'wc-api' );
|
|
|
|
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 ) );
|
|
|
|
isRequesting = isGetReviewsRequesting( reviewsQuery );
|
|
|
|
} 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 ) {
|
|
|
|
const creationDate = new Date( lastApprovedReview.date_created_gmt );
|
|
|
|
lastApprovedReviewTime = creationDate.getTime();
|
|
|
|
}
|
|
|
|
}
|
2018-12-12 13:35:56 +00:00
|
|
|
|
2019-04-30 09:40:10 +00:00
|
|
|
isError = Boolean( getReviewsError( approvedReviewsQuery ) );
|
|
|
|
isRequesting = isGetReviewsRequesting( approvedReviewsQuery );
|
|
|
|
}
|
2019-03-21 11:35:46 +00:00
|
|
|
|
2019-04-30 09:40:10 +00:00
|
|
|
return {
|
|
|
|
reviews,
|
|
|
|
isError,
|
|
|
|
isRequesting,
|
|
|
|
lastApprovedReviewTime,
|
|
|
|
};
|
|
|
|
} )( ReviewsPanel );
|