2019-08-22 11:36:20 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-03-02 14:26:00 +00:00
|
|
|
import { Component } from '@wordpress/element';
|
2019-08-22 11:36:20 +00:00
|
|
|
import isShallowEqual from '@wordpress/is-shallow-equal';
|
2024-08-27 19:29:44 +00:00
|
|
|
import type { Review } from '@woocommerce/base-components/reviews/types';
|
|
|
|
import { ErrorObject } from '@woocommerce/editor-components/error-placeholder';
|
2019-08-22 11:36:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { getReviews } from '../../blocks/reviews/utils';
|
2023-08-30 06:56:28 +00:00
|
|
|
import { formatError } from '../utils/errors';
|
2019-08-22 11:36:20 +00:00
|
|
|
|
2024-08-27 19:29:44 +00:00
|
|
|
interface WithReviewsProps {
|
|
|
|
order: 'asc' | 'desc';
|
|
|
|
orderby: string;
|
|
|
|
reviewsToDisplay: number;
|
|
|
|
categoryIds?: string | string[];
|
|
|
|
delayFunction?: ( f: () => void ) => DelayedFunction;
|
|
|
|
onReviewsAppended?: () => void;
|
|
|
|
onReviewsLoadError?: ( error: ErrorObject ) => void;
|
|
|
|
onReviewsReplaced?: () => void;
|
|
|
|
productId?: string | number;
|
|
|
|
attributes: {
|
|
|
|
previewReviews?: Review[];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
interface WithReviewsState {
|
|
|
|
error: string | ErrorObject | null;
|
|
|
|
loading: boolean;
|
|
|
|
reviews: Review[];
|
|
|
|
totalReviews: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
type DelayedFunction = ( () => void ) & { cancel?: () => void };
|
|
|
|
|
2019-12-10 17:17:46 +00:00
|
|
|
/**
|
|
|
|
* HOC that queries reviews for a component.
|
|
|
|
*/
|
2024-08-27 19:29:44 +00:00
|
|
|
const withReviews = (
|
|
|
|
OriginalComponent: React.FunctionComponent< Record< string, unknown > >
|
|
|
|
) => {
|
|
|
|
class WrappedComponent extends Component<
|
|
|
|
WithReviewsProps,
|
|
|
|
WithReviewsState
|
|
|
|
> {
|
2019-10-22 14:13:14 +00:00
|
|
|
isPreview = !! this.props.attributes.previewReviews;
|
|
|
|
|
2024-08-27 19:29:44 +00:00
|
|
|
delayedAppendReviews = (
|
|
|
|
this.props.delayFunction ?? ( ( f: () => void ) => f )
|
|
|
|
)( this.appendReviews );
|
2019-10-22 14:13:14 +00:00
|
|
|
|
2021-07-12 11:58:10 +00:00
|
|
|
isMounted = false;
|
|
|
|
|
2024-08-27 19:29:44 +00:00
|
|
|
state: WithReviewsState = {
|
2019-10-22 14:13:14 +00:00
|
|
|
error: null,
|
|
|
|
loading: true,
|
2024-08-27 19:29:44 +00:00
|
|
|
reviews:
|
|
|
|
this.isPreview && this.props.attributes?.previewReviews
|
|
|
|
? this.props.attributes.previewReviews
|
|
|
|
: [],
|
|
|
|
totalReviews:
|
|
|
|
this.isPreview && this.props.attributes?.previewReviews
|
|
|
|
? this.props.attributes.previewReviews.length
|
|
|
|
: 0,
|
2019-10-22 14:13:14 +00:00
|
|
|
};
|
2019-08-22 11:36:20 +00:00
|
|
|
|
|
|
|
componentDidMount() {
|
2021-07-12 11:58:10 +00:00
|
|
|
this.isMounted = true;
|
2019-08-22 11:36:20 +00:00
|
|
|
this.replaceReviews();
|
|
|
|
}
|
|
|
|
|
2024-08-27 19:29:44 +00:00
|
|
|
componentDidUpdate( prevProps: WithReviewsProps ) {
|
2019-08-22 11:36:20 +00:00
|
|
|
if ( prevProps.reviewsToDisplay < this.props.reviewsToDisplay ) {
|
|
|
|
// Since this attribute might be controlled via something with
|
|
|
|
// short intervals between value changes, this allows for optionally
|
|
|
|
// delaying review fetches via the provided delay function.
|
|
|
|
this.delayedAppendReviews();
|
2019-09-05 15:09:31 +00:00
|
|
|
} else if ( this.shouldReplaceReviews( prevProps, this.props ) ) {
|
2019-08-22 11:36:20 +00:00
|
|
|
this.replaceReviews();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-27 19:29:44 +00:00
|
|
|
shouldReplaceReviews(
|
|
|
|
prevProps: WithReviewsProps,
|
|
|
|
nextProps: WithReviewsProps
|
|
|
|
) {
|
2019-08-22 11:36:20 +00:00
|
|
|
return (
|
|
|
|
prevProps.orderby !== nextProps.orderby ||
|
|
|
|
prevProps.order !== nextProps.order ||
|
|
|
|
prevProps.productId !== nextProps.productId ||
|
2024-08-27 19:29:44 +00:00
|
|
|
! isShallowEqual(
|
|
|
|
prevProps.categoryIds as string[],
|
|
|
|
nextProps.categoryIds as string[]
|
|
|
|
)
|
2019-08-22 11:36:20 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-12 11:58:10 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.isMounted = false;
|
2024-08-27 19:29:44 +00:00
|
|
|
if (
|
|
|
|
'cancel' in this.delayedAppendReviews &&
|
|
|
|
typeof this.delayedAppendReviews.cancel === 'function'
|
|
|
|
) {
|
2019-08-27 09:38:28 +00:00
|
|
|
this.delayedAppendReviews.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-27 19:29:44 +00:00
|
|
|
getArgs( reviewsToSkip: number ) {
|
2022-06-15 09:56:52 +00:00
|
|
|
const { categoryIds, order, orderby, productId, reviewsToDisplay } =
|
|
|
|
this.props;
|
2024-08-27 19:29:44 +00:00
|
|
|
const args: Record< string, string | number > = {
|
2019-08-22 11:36:20 +00:00
|
|
|
order,
|
|
|
|
orderby,
|
|
|
|
per_page: reviewsToDisplay - reviewsToSkip,
|
|
|
|
offset: reviewsToSkip,
|
|
|
|
};
|
|
|
|
|
2022-05-03 14:09:20 +00:00
|
|
|
if ( categoryIds ) {
|
|
|
|
const categories = Array.isArray( categoryIds )
|
|
|
|
? categoryIds
|
|
|
|
: JSON.parse( categoryIds );
|
|
|
|
|
|
|
|
args.category_id = Array.isArray( categories )
|
|
|
|
? categories.join( ',' )
|
|
|
|
: categories;
|
2019-08-22 11:36:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( productId ) {
|
|
|
|
args.product_id = productId;
|
|
|
|
}
|
|
|
|
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
|
|
|
replaceReviews() {
|
2019-10-22 14:13:14 +00:00
|
|
|
if ( this.isPreview ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-27 19:29:44 +00:00
|
|
|
const onReviewsReplaced =
|
|
|
|
this.props.onReviewsReplaced ?? ( () => undefined );
|
2019-08-22 11:36:20 +00:00
|
|
|
this.updateListOfReviews().then( onReviewsReplaced );
|
|
|
|
}
|
|
|
|
|
|
|
|
appendReviews() {
|
2019-10-22 14:13:14 +00:00
|
|
|
if ( this.isPreview ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-27 19:29:44 +00:00
|
|
|
const onReviewsAppended =
|
|
|
|
this.props.onReviewsAppended ?? ( () => undefined );
|
|
|
|
const { reviewsToDisplay } = this.props;
|
2019-08-22 11:36:20 +00:00
|
|
|
const { reviews } = this.state;
|
|
|
|
|
|
|
|
// Given that this function is delayed, props might have been updated since
|
|
|
|
// it was called so we need to check again if fetching new reviews is necessary.
|
|
|
|
if ( reviewsToDisplay <= reviews.length ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateListOfReviews( reviews ).then( onReviewsAppended );
|
|
|
|
}
|
|
|
|
|
2024-08-27 19:29:44 +00:00
|
|
|
updateListOfReviews( oldReviews: Review[] = [] ) {
|
2019-08-22 11:36:20 +00:00
|
|
|
const { reviewsToDisplay } = this.props;
|
|
|
|
const { totalReviews } = this.state;
|
2019-09-05 15:09:31 +00:00
|
|
|
const reviewsToLoad =
|
|
|
|
Math.min( totalReviews, reviewsToDisplay ) - oldReviews.length;
|
2019-08-22 11:36:20 +00:00
|
|
|
|
|
|
|
this.setState( {
|
|
|
|
loading: true,
|
|
|
|
reviews: oldReviews.concat( Array( reviewsToLoad ).fill( {} ) ),
|
|
|
|
} );
|
|
|
|
|
|
|
|
return getReviews( this.getArgs( oldReviews.length ) )
|
2019-09-09 10:52:48 +00:00
|
|
|
.then(
|
|
|
|
( {
|
|
|
|
reviews: newReviews,
|
2019-08-22 11:36:20 +00:00
|
|
|
totalReviews: newTotalReviews,
|
2019-09-09 10:52:48 +00:00
|
|
|
} ) => {
|
2021-07-12 11:58:10 +00:00
|
|
|
if ( this.isMounted ) {
|
|
|
|
this.setState( {
|
|
|
|
reviews: oldReviews
|
|
|
|
.filter(
|
|
|
|
( review ) =>
|
|
|
|
Object.keys( review ).length
|
|
|
|
)
|
|
|
|
.concat( newReviews ),
|
|
|
|
totalReviews: newTotalReviews,
|
|
|
|
loading: false,
|
|
|
|
error: null,
|
|
|
|
} );
|
|
|
|
}
|
2019-09-09 10:52:48 +00:00
|
|
|
return { newReviews };
|
|
|
|
}
|
|
|
|
)
|
2019-08-22 11:36:20 +00:00
|
|
|
.catch( this.setError );
|
|
|
|
}
|
|
|
|
|
2024-08-27 19:29:44 +00:00
|
|
|
setError = async ( e: Error ) => {
|
2021-07-12 11:58:10 +00:00
|
|
|
if ( ! this.isMounted ) {
|
|
|
|
return;
|
|
|
|
}
|
2024-08-27 19:29:44 +00:00
|
|
|
|
|
|
|
const onReviewsLoadError =
|
|
|
|
this.props.onReviewsLoadError ?? ( () => undefined );
|
2019-09-04 16:07:00 +00:00
|
|
|
const error = await formatError( e );
|
2019-08-27 09:38:41 +00:00
|
|
|
|
2019-09-04 16:07:00 +00:00
|
|
|
this.setState( { reviews: [], loading: false, error } );
|
2019-08-27 09:38:41 +00:00
|
|
|
|
2019-09-04 16:07:00 +00:00
|
|
|
onReviewsLoadError( error );
|
2019-10-22 14:13:14 +00:00
|
|
|
};
|
2019-08-22 11:36:20 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { reviewsToDisplay } = this.props;
|
|
|
|
const { error, loading, reviews, totalReviews } = this.state;
|
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
return (
|
|
|
|
<OriginalComponent
|
|
|
|
{ ...this.props }
|
|
|
|
error={ error }
|
|
|
|
isLoading={ loading }
|
|
|
|
reviews={ reviews.slice( 0, reviewsToDisplay ) }
|
|
|
|
totalReviews={ totalReviews }
|
|
|
|
/>
|
|
|
|
);
|
2019-08-22 11:36:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
const { displayName = OriginalComponent.name || 'Component' } =
|
|
|
|
OriginalComponent;
|
2024-08-27 19:29:44 +00:00
|
|
|
(
|
|
|
|
WrappedComponent as React.ComponentType< WithReviewsProps >
|
|
|
|
).displayName = `WithReviews(${ displayName })`;
|
2019-08-22 11:36:20 +00:00
|
|
|
return WrappedComponent;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default withReviews;
|