/**
* External dependencies
*/
import { __, _n, sprintf } from '@wordpress/i18n';
import {
BlockControls,
InspectorControls,
} from '@wordpress/editor';
import {
Button,
Disabled,
Notice,
PanelBody,
Placeholder,
RangeControl,
SelectControl,
Spinner,
ToggleControl,
Toolbar,
withSpokenMessages,
} from '@wordpress/components';
import classNames from 'classnames';
import { SearchListItem } from '@woocommerce/components';
import { Fragment, RawHTML } from '@wordpress/element';
import { compose } from '@wordpress/compose';
import { escapeHTML } from '@wordpress/escape-html';
import PropTypes from 'prop-types';
import { getAdminLink } from '@woocommerce/navigation';
/**
* Internal dependencies
*/
import ApiErrorPlaceholder from '../../components/api-error-placeholder';
import EditorBlock from './editor-block.js';
import ProductControl from '../../components/product-control';
import ToggleButtonControl from '../../components/toggle-button-control';
import { IconReviewsByProduct } from '../../components/icons';
import { withProduct } from '../../hocs';
import { ENABLE_REVIEW_RATING, SHOW_AVATARS } from '../../constants';
/**
* Component to handle edit mode of "Reviews by Product".
*/
const ReviewsByProductEditor = ( { attributes, debouncedSpeak, error, getProduct, isLoading, product, setAttributes } ) => {
const { className, editMode, productId, showReviewDate, showReviewerName } = attributes;
const getBlockControls = () => (
setAttributes( { editMode: ! editMode } ),
isActive: editMode,
},
] }
/>
);
const renderProductControlItem = ( args ) => {
const { item = 0 } = args;
return (
);
};
const getInspectorControls = () => {
const minPerPage = 1;
const maxPerPage = 20;
return (
{
const id = value[ 0 ] ? value[ 0 ].id : 0;
setAttributes( { productId: id } );
} }
renderItem={ renderProductControlItem }
/>
setAttributes( { showReviewRating: ! attributes.showReviewRating } ) }
/>
{ ( attributes.showReviewRating && ! ENABLE_REVIEW_RATING ) && (
{ sprintf( __( 'Product rating is disabled in your %sstore settings%s.', 'woo-gutenberg-products-block' ), ``, '' ) }
) }
setAttributes( { showReviewerName: ! attributes.showReviewerName } ) }
/>
setAttributes( { showReviewImage: ! attributes.showReviewImage } ) }
/>
setAttributes( { showReviewDate: ! attributes.showReviewDate } ) }
/>
{ attributes.showReviewImage && (
setAttributes( { imageType: value } ) }
/>
{ ( attributes.imageType === 'reviewer' && ! SHOW_AVATARS ) && (
{ sprintf( __( 'Reviewer photo is disabled in your %ssite settings%s.', 'woo-gutenberg-products-block' ), ``, '' ) }
) }
) }
setAttributes( { showOrderby: ! attributes.showOrderby } ) }
/>
setAttributes( { orderby } ) }
/>
setAttributes( { reviewsOnPageLoad } ) }
max={ maxPerPage }
min={ minPerPage }
/>
setAttributes( { showLoadMore: ! attributes.showLoadMore } ) }
/>
{ attributes.showLoadMore && (
setAttributes( { reviewsOnLoadMore } ) }
max={ maxPerPage }
min={ minPerPage }
/>
) }
);
};
const renderApiError = () => (
);
const renderLoadingScreen = () => {
return (
}
label={ __( 'Reviews by Product', 'woo-gutenberg-products-block' ) }
className="wc-block-reviews-by-product"
>
);
};
const renderEditMode = () => {
const onDone = () => {
setAttributes( { editMode: false } );
debouncedSpeak(
__(
'Showing Reviews by Product block preview.',
'woo-gutenberg-products-block'
)
);
};
return (
}
label={ __( 'Reviews by Product', 'woo-gutenberg-products-block' ) }
className="wc-block-reviews-by-product"
>
{ __(
'Show reviews of your product to build trust',
'woo-gutenberg-products-block'
) }
{
const id = value[ 0 ] ? value[ 0 ].id : 0;
setAttributes( { productId: id } );
} }
queryArgs={ {
orderby: 'comment_count',
order: 'desc',
} }
renderItem={ renderProductControlItem }
/>
);
};
const renderViewMode = () => {
const showReviewImage = ( SHOW_AVATARS || attributes.imageType === 'product' ) && attributes.showReviewImage;
const showReviewRating = ENABLE_REVIEW_RATING && attributes.showReviewRating;
const classes = classNames( 'wc-block-reviews-by-product', className, {
'has-image': showReviewImage,
'has-name': showReviewerName,
'has-date': showReviewDate,
'has-rating': showReviewRating,
} );
return (
{ product.review_count === 0 ? (
}
label={ __( 'Reviews by Product', 'woo-gutenberg-products-block' ) }
>
' + escapeHTML( product.name ) + ''
),
} } />
) : (
) }
);
};
if ( error ) {
return renderApiError();
}
if ( ! productId || editMode ) {
return renderEditMode();
}
if ( ! product || isLoading ) {
return renderLoadingScreen();
}
return (
{ getBlockControls() }
{ getInspectorControls() }
{ renderViewMode() }
);
};
ReviewsByProductEditor.propTypes = {
/**
* The attributes for this block.
*/
attributes: PropTypes.object.isRequired,
/**
* The register block name.
*/
name: PropTypes.string.isRequired,
/**
* A callback to update attributes.
*/
setAttributes: PropTypes.func.isRequired,
// from withProduct
error: PropTypes.object,
getProduct: PropTypes.func,
isLoading: PropTypes.bool,
product: PropTypes.shape( {
name: PropTypes.node,
review_count: PropTypes.number,
} ),
// from withSpokenMessages
debouncedSpeak: PropTypes.func.isRequired,
};
export default compose( [
withProduct,
withSpokenMessages,
] )( ReviewsByProductEditor );