| 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 |
|