This repository has been archived by the owner on Aug 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
frontend-container-block.js
109 lines (94 loc) · 2.36 KB
/
frontend-container-block.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* External dependencies
*/
import { __, _n, sprintf } from '@wordpress/i18n';
import { speak } from '@wordpress/a11y';
import { Component } from 'react';
import PropTypes from 'prop-types';
/**
* Internal dependencies
*/
import { getSortArgs } from './utils';
import FrontendBlock from './frontend-block';
/**
* Container of the block rendered in the frontend.
*/
class FrontendContainerBlock extends Component {
constructor() {
super( ...arguments );
const { attributes } = this.props;
this.state = {
orderby: attributes.orderby,
reviewsToDisplay: parseInt( attributes.reviewsOnPageLoad, 10 ),
};
this.onAppendReviews = this.onAppendReviews.bind( this );
this.onChangeOrderby = this.onChangeOrderby.bind( this );
}
onAppendReviews() {
const { attributes } = this.props;
const { reviewsToDisplay } = this.state;
this.setState( {
reviewsToDisplay:
reviewsToDisplay + parseInt( attributes.reviewsOnLoadMore, 10 ),
} );
}
onChangeOrderby( event ) {
const { attributes } = this.props;
this.setState( {
orderby: event.target.value,
reviewsToDisplay: parseInt( attributes.reviewsOnPageLoad, 10 ),
} );
}
onReviewsAppended( { newReviews } ) {
speak(
sprintf(
_n(
'%d review loaded.',
'%d reviews loaded.',
newReviews.length,
'woocommerce'
),
newReviews.length
)
);
}
onReviewsReplaced() {
speak( __( 'Reviews list updated.', 'woocommerce' ) );
}
onReviewsLoadError() {
speak(
__(
'There was an error loading the reviews.',
'woocommerce'
)
);
}
render() {
const { attributes } = this.props;
const { categoryIds, productId } = attributes;
const { reviewsToDisplay } = this.state;
const { order, orderby } = getSortArgs( this.state.orderby );
return (
<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 }
/>
);
}
}
FrontendContainerBlock.propTypes = {
/**
* The attributes for this block.
*/
attributes: PropTypes.object.isRequired,
};
export default FrontendContainerBlock;