Skip to content

Commit

Permalink
Add callbacks to ServerSideRenderer to handle failures with custom re…
Browse files Browse the repository at this point in the history
…nderers (#16512)

* Add callbacks to ServerSideRenderer to handle failures

* Update variable naming and set default renderers

* LoadingResponsePlaceholder

* swtich case

* fetchComplete

* Remove fetchcomplete method

* Pass render props

* add changelog entry

* add documentation for new props
  • Loading branch information
mikejolley authored and gziolo committed Aug 29, 2019
1 parent 0a5a2b9 commit 7512fc4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 18 deletions.
5 changes: 5 additions & 0 deletions packages/server-side-render/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Master

### Feature
- Add `EmptyResponsePlaceholder`, `ErrorResponsePlaceholder` and `LoadingResponsePlaceholder` render props for parent components to swap out alternate placeholders for the various states (see https://github.com/WordPress/gutenberg/pull/16512).

## 1.0.0 (2019-06-12)

### Initial Release
Expand Down
19 changes: 19 additions & 0 deletions packages/server-side-render/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ E.g: `{ post_id: 12 }`.
- Type: `Object`
- Required: No

### EmptyResponsePlaceholder

This is a [render prop](https://reactjs.org/docs/render-props.html). When the api response is empty, the value of this prop is rendered. The render prop will receive the value of the api response as well as all props passed into `ServerSideRenderer`.
- Type: `WPElement`
- Required: No

### ErrorResponsePlaceholder

This is a [render prop](https://reactjs.org/docs/render-props.html). When the api response is an error, the value of this prop is rendered. The render prop will receive the value of the api response as well as all props passed into `ServerSideRenderer`.
- Type: `WPElement`
- Required: No

### LoadingResponsePlaceholder

This is a [render prop](https://reactjs.org/docs/render-props.html). While the request is being processed (loading state), the value of this prop is rendered. The render prop will receive the value of the api response as well as all props passed into `ServerSideRenderer`.
- Type: `WPElement`
- Required: No


## Usage

Render core/archives preview.
Expand Down
53 changes: 35 additions & 18 deletions packages/server-side-render/src/server-side-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,32 +83,19 @@ export class ServerSideRender extends Component {

render() {
const response = this.state.response;
const { className } = this.props;
const { className, EmptyResponsePlaceholder, ErrorResponsePlaceholder, LoadingResponsePlaceholder } = this.props;

if ( response === '' ) {
return (
<Placeholder
className={ className }
>
{ __( 'Block rendered as empty.' ) }
</Placeholder>
<EmptyResponsePlaceholder response={ response } { ...this.props } />
);
} else if ( ! response ) {
return (
<Placeholder
className={ className }
>
<Spinner />
</Placeholder>
<LoadingResponsePlaceholder response={ response } { ...this.props } />
);
} else if ( response.error ) {
// translators: %s: error message describing the problem
const errorMessage = sprintf( __( 'Error loading block: %s' ), response.errorMsg );
return (
<Placeholder
className={ className }
>
{ errorMessage }
</Placeholder>
<ErrorResponsePlaceholder response={ response } { ...this.props } />
);
}

Expand All @@ -123,4 +110,34 @@ export class ServerSideRender extends Component {
}
}

ServerSideRender.defaultProps = {
EmptyResponsePlaceholder: ( { className } ) => (
<Placeholder
className={ className }
>
{ __( 'Block rendered as empty.' ) + className }
</Placeholder>
),
ErrorResponsePlaceholder: ( { response, className } ) => {
// translators: %s: error message describing the problem
const errorMessage = sprintf( __( 'Error loading block: %s' ), response.errorMsg );
return (
<Placeholder
className={ className }
>
{ errorMessage }
</Placeholder>
);
},
LoadingResponsePlaceholder: ( { className } ) => {
return (
<Placeholder
className={ className }
>
<Spinner />
</Placeholder>
);
},
};

export default ServerSideRender;

0 comments on commit 7512fc4

Please sign in to comment.