PluginProbe
Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels / 1.6.4
Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels v1.6.4
2.9.1 2.9.0 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.4.0 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 2.0.0 2.1.0 2.2.0 2.3.2 2.3.3 2.3.5 2.3.6 2.3.8 2.3.9 2.4.3 All 37 releases
automatic-youtube-gallery / block / src / block / server-side-render.js

server-side-render.js in Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels 1.6.4, at block/src/block/server-side-render.js

129 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 // Import block dependencies and components
2 import isEqual from 'lodash/isEqual';
3 import debounce from 'lodash/debounce';
4
5 // Components
6 const {
7 __,
8 sprintf
9 } = wp.i18n;
10
11 const {
12 Placeholder,
13 Spinner
14 } = wp.components;
15
16 const {
17 Component,
18 RawHTML,
19 } = wp.element;
20
21 const { addQueryArgs } = wp.url;
22
23 /**
24 * Create an AYGServerSideRender Component.
25 */
26 export function AYGRendererPath( block, attributes = null, urlQueryArgs = {} ) {
27 return addQueryArgs( `/wp/v2/block-renderer/${ block }`, {
28 context: 'edit',
29 ...( null !== attributes ? { attributes } : {} ),
30 ...urlQueryArgs,
31 } );
32 }
33
34 export class AYGServerSideRender extends Component {
35
36 constructor( props ) {
37 super( props );
38
39 this.state = {
40 response: null,
41 };
42 }
43
44 componentDidMount() {
45 this.isStillMounted = true;
46 this.fetch( this.props );
47 // Only debounce once the initial fetch occurs to ensure that the first
48 // renders show data as soon as possible.
49 this.fetch = debounce( this.fetch, 500 );
50 }
51
52 componentWillUnmount() {
53 this.isStillMounted = false;
54 }
55
56 componentDidUpdate( prevProps, prevState ) {
57 if ( ! isEqual( prevProps, this.props ) ) {
58 this.fetch( this.props );
59 }
60
61 if ( this.state.response !== prevState.response ) {
62 if ( this.props.onChange ) {
63 this.props.onChange();
64 }
65 }
66 }
67
68 fetch( props ) {
69 if ( ! this.isStillMounted ) {
70 return;
71 }
72
73 if ( null !== this.state.response ) {
74 this.setState( { response: null } );
75 }
76
77 const { block, attributes = null, urlQueryArgs = {} } = props;
78
79 const path = AYGRendererPath( block, attributes, urlQueryArgs );
80
81 // Store the latest fetch request so that when we process it, we can
82 // check if it is the current request, to avoid race conditions on slow networks.
83 const fetchRequest = this.currentFetchRequest = wp.apiFetch( { path } )
84 .then( ( response ) => {
85 if ( this.isStillMounted && fetchRequest === this.currentFetchRequest && response && response.rendered ) {
86 this.setState( { response: response.rendered } );
87 }
88 } )
89 .catch( ( error ) => {
90 if ( this.isStillMounted && fetchRequest === this.currentFetchRequest ) {
91 this.setState( { response: {
92 error: true,
93 errorMsg: error.message,
94 } } );
95 }
96 } );
97
98 return fetchRequest;
99 }
100
101 render() {
102 const response = this.state.response;
103
104 if ( ! response ) {
105 return (
106 <Placeholder><Spinner /></Placeholder>
107 );
108 } else if ( response.error ) {
109 // translators: %s: error message describing the problem
110 const errorMessage = sprintf( __( 'Error loading block: %s' ), response.errorMsg );
111
112 return (
113 <Placeholder>{ errorMessage }</Placeholder>
114 );
115 } else if ( ! response.length ) {
116 return (
117 <Placeholder>{ __( 'No results found.' ) }</Placeholder>
118 );
119 }
120
121 return (
122 <RawHTML key="html">{ response }</RawHTML>
123 );
124 }
125
126 }
127
128 export default AYGServerSideRender;
129