-
Notifications
You must be signed in to change notification settings - Fork 5
/
server-side-render-x.js
172 lines (157 loc) · 4.26 KB
/
server-side-render-x.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* External dependencies
*/
import { isEqual, debounce } from 'lodash';
/**
* WordPress dependencies
*/
const { Component, RawHTML, Fragment } = wp.element;
const { __, sprintf } = wp.i18n;
const apiFetch = wp.apiFetch;
const { addQueryArgs } = wp.url;
const { Placeholder, Spinner } = wp.components;
export function rendererPath(block, attributes = null, urlQueryArgs = {}) {
return addQueryArgs(`/wp/v2/block-renderer/${block}`, {
context: 'edit',
...(null !== attributes ? { attributes } : {}),
...urlQueryArgs,
});
}
export class ServerSideRenderX extends Component {
constructor(props) {
super(props);
this.state = {
response: null,
prevResponse: null,
};
}
componentDidMount() {
this.isStillMounted = true;
this.fetch(this.props);
// Only debounce once the initial fetch occurs to ensure that the first
// renders show data as soon as possible.
this.fetch = debounce(this.fetch, 500);
}
componentWillUnmount() {
this.isStillMounted = false;
}
componentDidUpdate(prevProps) {
if (!isEqual(prevProps, this.props)) {
this.fetch(this.props);
}
}
fetch(props) {
if (!this.isStillMounted) {
return;
}
if (null !== this.state.response) {
//this.setState({ response: null, prevResponse: this.state.response });
this.setState(state => (
{
response: null,
prevResponse: state.response
}
));
}
const { block, attributes = null, urlQueryArgs = {} } = props;
const path = rendererPath(block, attributes, urlQueryArgs);
// Store the latest fetch request so that when we process it, we can
// check if it is the current request, to avoid race conditions on slow networks.
const fetchRequest = (this.currentFetchRequest = apiFetch({ path })
.then((response) => {
if (
this.isStillMounted &&
fetchRequest === this.currentFetchRequest &&
response
) {
this.setState({ response: response.rendered });
}
})
.catch((error) => {
if (
this.isStillMounted &&
fetchRequest === this.currentFetchRequest
) {
this.setState({
response: {
error: true,
errorMsg: error.message,
},
});
}
}));
return fetchRequest;
}
render() {
const { right, top, unit } = this.props.spinnerLocation;
const response = this.state.response;
//let response = this.state.response;
//response = `<div style="position:relative;"><div style="position:absolute;right:0;top:10px"><span class="components-spinner"></span></div>${response}</div>`;
const prevResponse = this.state.prevResponse;
let prevResponseHTML = "";
if (prevResponse !== null) {
prevResponseHTML = `<div style="position:relative;"><div style="position:absolute;right:${right}${unit};top:${top}${unit}"><span class="components-spinner"></span></div>${prevResponse}</div>`;
//response = `<div style="position:relative;"><div style="position:absolute;right:0;top:10px"><span class="components-spinner"></span></div>${response}</div>`;
}
const {
className,
EmptyResponsePlaceholder,
ErrorResponsePlaceholder,
LoadingResponsePlaceholder,
} = this.props;
if (response === '') {
return (
<EmptyResponsePlaceholder
response={response}
{...this.props}
/>
);
} else if (!response) {
return (
<Fragment>
<RawHTML key="html" className={className}>
{prevResponseHTML}
</RawHTML>
</Fragment>
);
} else if (response.error) {
return (
<ErrorResponsePlaceholder
response={response}
{...this.props}
/>
);
}
return (
<RawHTML key="html" className={className}>
{response}
</RawHTML>
);
}
}
ServerSideRenderX.defaultProps = {
spinnerLocation: {right: 0, top: 10, unit: 'px'},
EmptyResponsePlaceholder: ({ className }) => (
<Placeholder className={className}>
{__('Block rendered as empty.')}
</Placeholder>
),
ErrorResponsePlaceholder: ({ response, className }) => {
const errorMessage = sprintf(
// translators: %s: error message describing the problem
__('Error loading block: %s'),
response.errorMsg
);
return (
<Placeholder className={className}>{errorMessage}</Placeholder>
);
},
LoadingResponsePlaceholder: ({ className }) => {
return (
<Placeholder className={className}>
<Spinner />
</Placeholder>
);
},
};
export default ServerSideRenderX;