2017-02-02 16:18:33 +01:00
|
|
|
import { Components, registerComponent } from 'meteor/nova:core';
|
2016-12-06 18:09:26 +01:00
|
|
|
import { withMutation } from 'meteor/nova:core';
|
2016-04-07 12:43:41 +09:00
|
|
|
import React, { PropTypes, Component } from 'react';
|
|
|
|
import FRC from 'formsy-react-components';
|
2016-12-06 15:51:59 +09:00
|
|
|
|
2016-04-07 12:43:41 +09:00
|
|
|
const Input = FRC.Input;
|
|
|
|
|
|
|
|
class EmbedlyURL extends Component {
|
|
|
|
|
2017-01-23 15:50:55 +01:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-04-07 12:43:41 +09:00
|
|
|
this.handleBlur = this.handleBlur.bind(this);
|
2017-01-23 15:50:55 +01:00
|
|
|
|
2016-04-07 12:43:41 +09:00
|
|
|
this.state = {
|
2017-01-23 15:50:55 +01:00
|
|
|
loading: false,
|
|
|
|
value: props.value || '',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean the media property of the document if it exists: this field is handled server-side in an async callback
|
|
|
|
async componentDidMount() {
|
|
|
|
try {
|
|
|
|
if (this.props.document && !_.isEmpty(this.props.document.media)) {
|
|
|
|
await this.context.updateCurrentValues({media: {}});
|
|
|
|
}
|
|
|
|
} catch(error) {
|
2017-02-02 15:15:51 +01:00
|
|
|
console.error('Error cleaning "media" property', error); // eslint-disable-line
|
2016-04-07 12:43:41 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// called whenever the URL input field loses focus
|
2017-01-23 15:50:55 +01:00
|
|
|
async handleBlur() {
|
|
|
|
try {
|
|
|
|
// value from formsy input ref
|
|
|
|
const url = this.input.getValue();
|
2017-02-02 15:15:51 +01:00
|
|
|
|
|
|
|
// start the mutation only if the input has a value
|
2017-01-23 15:50:55 +01:00
|
|
|
if (url.length) {
|
2017-02-02 15:15:51 +01:00
|
|
|
|
|
|
|
// notify the user that something happens
|
|
|
|
await this.setState({loading: true});
|
|
|
|
|
2017-01-23 15:50:55 +01:00
|
|
|
// the URL has changed, get new title, body, thumbnail & media for this url
|
|
|
|
const result = await this.props.getEmbedlyData({url});
|
|
|
|
|
|
|
|
// uncomment for debug
|
|
|
|
// console.log('Embedly Data', result);
|
|
|
|
|
2017-02-02 15:15:51 +01:00
|
|
|
// extract the relevant data, for easier consumption
|
|
|
|
const { data: { getEmbedlyData: { title, description, thumbnailUrl } } } = result;
|
|
|
|
|
|
|
|
// update the form
|
2017-01-23 15:50:55 +01:00
|
|
|
await this.context.updateCurrentValues({
|
2017-02-02 15:15:51 +01:00
|
|
|
title: title || "",
|
|
|
|
body: description || "",
|
|
|
|
thumbnailUrl: thumbnailUrl || "",
|
2016-12-06 15:51:59 +09:00
|
|
|
});
|
2017-01-23 15:50:55 +01:00
|
|
|
|
2017-02-02 15:15:51 +01:00
|
|
|
// embedly component is done
|
2017-01-23 15:50:55 +01:00
|
|
|
await this.setState({loading: false});
|
2017-02-02 15:15:51 +01:00
|
|
|
|
|
|
|
// remove errors & keep the current values
|
|
|
|
await this.context.clearForm({clearErrors: true});
|
2017-01-23 15:50:55 +01:00
|
|
|
}
|
|
|
|
} catch(error) {
|
2017-02-02 15:15:51 +01:00
|
|
|
|
|
|
|
console.error(error); // eslint-disable-line
|
|
|
|
|
|
|
|
// embedly component is done
|
2017-01-23 15:50:55 +01:00
|
|
|
await this.setState({loading: false});
|
2017-02-02 15:15:51 +01:00
|
|
|
|
|
|
|
// something bad happened
|
|
|
|
await this.context.throwError(error.message);
|
2016-07-04 12:57:41 +09:00
|
|
|
}
|
2016-04-07 12:43:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
|
|
|
const wrapperStyle = {
|
|
|
|
position: "relative"
|
|
|
|
};
|
|
|
|
|
|
|
|
const loadingStyle = {
|
|
|
|
position: "absolute",
|
|
|
|
pointerEvents: "none",
|
|
|
|
top: "15px",
|
|
|
|
right: "15px"
|
|
|
|
};
|
|
|
|
|
|
|
|
loadingStyle.display = this.state.loading ? "block" : "none";
|
2016-11-26 02:46:55 +08:00
|
|
|
|
2016-09-07 14:35:49 +02:00
|
|
|
// see https://facebook.github.io/react/warnings/unknown-prop.html
|
2017-02-02 15:15:51 +01:00
|
|
|
const {document, control, getEmbedlyData, ...rest} = this.props; // eslint-disable-line
|
2016-04-07 12:43:41 +09:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="embedly-url-field" style={wrapperStyle}>
|
2016-11-26 02:46:55 +08:00
|
|
|
<Input
|
2016-09-07 14:35:49 +02:00
|
|
|
{...rest}
|
2016-11-26 02:46:55 +08:00
|
|
|
onBlur={this.handleBlur}
|
|
|
|
type="text"
|
2016-04-09 10:22:44 +09:00
|
|
|
ref={ref => this.input = ref}
|
|
|
|
/>
|
|
|
|
<div className="embedly-url-field-loading" style={loadingStyle}>
|
2016-12-06 18:06:29 +01:00
|
|
|
<Components.Loading />
|
2016-04-09 10:22:44 +09:00
|
|
|
</div>
|
2016-04-07 12:43:41 +09:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EmbedlyURL.propTypes = {
|
2017-02-02 15:15:51 +01:00
|
|
|
name: PropTypes.string,
|
|
|
|
value: PropTypes.any,
|
|
|
|
label: PropTypes.string
|
2016-04-07 12:43:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
EmbedlyURL.contextTypes = {
|
2017-02-02 15:15:51 +01:00
|
|
|
updateCurrentValues: PropTypes.func,
|
|
|
|
throwError: PropTypes.func,
|
|
|
|
clearForm: PropTypes.func,
|
2016-04-07 12:43:41 +09:00
|
|
|
}
|
|
|
|
|
2016-12-06 18:09:26 +01:00
|
|
|
export default withMutation({
|
|
|
|
name: 'getEmbedlyData',
|
|
|
|
args: {url: 'String'},
|
|
|
|
})(EmbedlyURL);
|