Metabox.js
1.69 KB
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
import React, { Component } from "react";
import { LinkSuggestions as LinkSuggestionsElement } from "yoast-premium-components";
import Loader from "yoast-components/composites/basic/Loader";
/**
* Link suggestions metabox component.
*/
export default class Metabox extends Component {
/**
* Constructs a metabox component for the link suggestions.
*
* @param {Object} props The properties for this components.
* @param {boolean} props.isLoading Whether this component should start of showing a loader.
* @param {Array} props.suggestionsn The suggestions to render initially.
* @returns {void}
*/
constructor( props ) {
super( props );
this.state = {
loading: this.props.isLoading,
suggestions: this.props.suggestions,
};
this.retrievedLinkSuggestions = this.retrievedLinkSuggestions.bind( this );
this.props.linkSuggestions.on( "retrievedLinkSuggestions", this.retrievedLinkSuggestions );
}
/**
* Updates the link suggestions in the state.
*
* @param {Array} suggestions The link suggestions to set in the state.
* @returns {void}
*/
retrievedLinkSuggestions( suggestions ) {
this.setState( {
suggestions,
loading: false,
} );
}
/**
* Renders this component.
*
* @returns {React.Element} The rendered element.
*/
render() {
if ( this.state.loading ) {
return <div className="yoast-link-suggestions yoast-link-suggestions--loading"><Loader /></div>;
}
return <div className="yoast-link-suggestions"><LinkSuggestionsElement suggestions={this.state.suggestions} /></div>;
}
}
Metabox.propTypes = {
linkSuggestions: React.PropTypes.object.isRequired,
suggestions: React.PropTypes.array.isRequired,
isLoading: React.PropTypes.bool.isRequired,
};