2019-08-22 11:36:20 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __, _n, sprintf } from '@wordpress/i18n';
|
|
|
|
import { speak } from '@wordpress/a11y';
|
2023-03-02 14:26:00 +00:00
|
|
|
import { Component } from '@wordpress/element';
|
2023-09-20 04:21:26 +00:00
|
|
|
import { Review } from '@woocommerce/base-components/reviews/types';
|
2019-08-22 11:36:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-10-28 13:53:09 +00:00
|
|
|
import { getSortArgs } from './utils';
|
2019-08-22 11:36:20 +00:00
|
|
|
import FrontendBlock from './frontend-block';
|
2023-09-20 04:21:26 +00:00
|
|
|
import { ReviewBlockAttributes } from './attributes';
|
|
|
|
|
|
|
|
type FrontendContainerBlockProps = {
|
|
|
|
attributes: ReviewBlockAttributes;
|
|
|
|
};
|
2019-08-22 11:36:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Container of the block rendered in the frontend.
|
|
|
|
*/
|
2023-09-20 04:21:26 +00:00
|
|
|
class FrontendContainerBlock extends Component<
|
|
|
|
FrontendContainerBlockProps,
|
|
|
|
{ orderby: string; reviewsToDisplay: number }
|
|
|
|
> {
|
|
|
|
constructor( props: FrontendContainerBlockProps ) {
|
|
|
|
super( props );
|
2019-08-22 11:36:20 +00:00
|
|
|
const { attributes } = this.props;
|
|
|
|
|
|
|
|
this.state = {
|
2023-09-20 04:21:26 +00:00
|
|
|
orderby: attributes?.orderby,
|
|
|
|
reviewsToDisplay: this.getReviewsOnPageLoad(),
|
2019-08-22 11:36:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.onAppendReviews = this.onAppendReviews.bind( this );
|
|
|
|
this.onChangeOrderby = this.onChangeOrderby.bind( this );
|
|
|
|
}
|
|
|
|
|
2023-09-20 04:21:26 +00:00
|
|
|
getReviewsOnPageLoad() {
|
|
|
|
const { attributes } = this.props;
|
|
|
|
|
|
|
|
return typeof attributes.reviewsOnPageLoad === 'number'
|
|
|
|
? attributes.reviewsOnPageLoad
|
|
|
|
: parseInt( attributes.reviewsOnPageLoad, 10 );
|
|
|
|
}
|
|
|
|
|
|
|
|
getReviewsOnLoadMore() {
|
2019-08-22 11:36:20 +00:00
|
|
|
const { attributes } = this.props;
|
2023-09-20 04:21:26 +00:00
|
|
|
|
|
|
|
return typeof attributes.reviewsOnLoadMore === 'number'
|
|
|
|
? attributes.reviewsOnLoadMore
|
|
|
|
: parseInt( attributes.reviewsOnLoadMore, 10 );
|
|
|
|
}
|
|
|
|
|
|
|
|
onAppendReviews() {
|
2019-08-22 11:36:20 +00:00
|
|
|
const { reviewsToDisplay } = this.state;
|
|
|
|
|
|
|
|
this.setState( {
|
2023-09-20 04:21:26 +00:00
|
|
|
reviewsToDisplay: reviewsToDisplay + this.getReviewsOnLoadMore(),
|
2019-08-22 11:36:20 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2023-09-20 04:21:26 +00:00
|
|
|
onChangeOrderby( event: React.ChangeEvent< HTMLSelectElement > ) {
|
2019-08-22 11:36:20 +00:00
|
|
|
this.setState( {
|
|
|
|
orderby: event.target.value,
|
2023-09-20 04:21:26 +00:00
|
|
|
reviewsToDisplay: this.getReviewsOnPageLoad(),
|
2019-08-22 11:36:20 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2023-09-20 04:21:26 +00:00
|
|
|
onReviewsAppended( { newReviews }: { newReviews: Review[] } ) {
|
2019-08-22 11:36:20 +00:00
|
|
|
speak(
|
|
|
|
sprintf(
|
2021-02-19 11:58:44 +00:00
|
|
|
/* translators: %d is the count of reviews loaded. */
|
2019-08-22 11:36:20 +00:00
|
|
|
_n(
|
|
|
|
'%d review loaded.',
|
|
|
|
'%d reviews loaded.',
|
|
|
|
newReviews.length,
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
2019-09-05 15:09:31 +00:00
|
|
|
newReviews.length
|
2019-08-22 11:36:20 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
onReviewsReplaced() {
|
|
|
|
speak( __( 'Reviews list updated.', 'woo-gutenberg-products-block' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
onReviewsLoadError() {
|
2019-09-05 15:09:31 +00:00
|
|
|
speak(
|
|
|
|
__(
|
|
|
|
'There was an error loading the reviews.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
)
|
|
|
|
);
|
2019-08-22 11:36:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { attributes } = this.props;
|
|
|
|
const { categoryIds, productId } = attributes;
|
|
|
|
const { reviewsToDisplay } = this.state;
|
2019-10-28 13:53:09 +00:00
|
|
|
const { order, orderby } = getSortArgs( this.state.orderby );
|
2019-08-22 11:36:20 +00:00
|
|
|
|
|
|
|
return (
|
2023-09-20 04:21:26 +00:00
|
|
|
// @ts-expect-error - TODO: Refactor WrappedComponent
|
2019-08-22 11:36:20 +00:00
|
|
|
<FrontendBlock
|
|
|
|
attributes={ attributes }
|
|
|
|
categoryIds={ categoryIds }
|
|
|
|
onAppendReviews={ this.onAppendReviews }
|
|
|
|
onChangeOrderby={ this.onChangeOrderby }
|
|
|
|
onReviewsAppended={ this.onReviewsAppended }
|
|
|
|
onReviewsLoadError={ this.onReviewsLoadError }
|
|
|
|
onReviewsReplaced={ this.onReviewsReplaced }
|
|
|
|
order={ order }
|
|
|
|
orderby={ orderby }
|
|
|
|
productId={ productId }
|
|
|
|
reviewsToDisplay={ reviewsToDisplay }
|
2021-08-18 07:33:13 +00:00
|
|
|
sortSelectValue={ this.state.orderby }
|
2019-08-22 11:36:20 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FrontendContainerBlock;
|