2017-01-23 15:50:55 +01:00
|
|
|
import { Components, registerComponent } from 'meteor/nova:lib';
|
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) {
|
|
|
|
console.error('Error cleaning "media" property', error)
|
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 {
|
|
|
|
await this.setState({loading: true});
|
|
|
|
|
|
|
|
// value from formsy input ref
|
|
|
|
const url = this.input.getValue();
|
|
|
|
|
|
|
|
if (url.length) {
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
await this.context.updateCurrentValues({
|
|
|
|
title: result.data.getEmbedlyData.title || "",
|
|
|
|
body: result.data.getEmbedlyData.description || "",
|
|
|
|
thumbnailUrl: result.data.getEmbedlyData.thumbnailUrl || "",
|
2016-12-06 15:51:59 +09:00
|
|
|
});
|
2017-01-23 15:50:55 +01:00
|
|
|
|
|
|
|
await this.setState({loading: false});
|
|
|
|
}
|
|
|
|
} catch(error) {
|
|
|
|
await this.setState({loading: false});
|
|
|
|
console.log(error); // eslint-disable-line no-console
|
|
|
|
this.context.throwError({content: error.message, type: "error"});
|
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-01-23 15:50:55 +01:00
|
|
|
const {document, control, getEmbedlyData, ...rest} = this.props; // eslint-disable-line no-unused-vars
|
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 = {
|
|
|
|
name: React.PropTypes.string,
|
|
|
|
value: React.PropTypes.any,
|
|
|
|
label: React.PropTypes.string
|
|
|
|
}
|
|
|
|
|
|
|
|
EmbedlyURL.contextTypes = {
|
2016-05-12 11:47:49 +09:00
|
|
|
addToAutofilledValues: React.PropTypes.func,
|
2017-01-23 15:50:55 +01:00
|
|
|
updateCurrentValues: React.PropTypes.func,
|
2016-06-24 09:44:32 +02:00
|
|
|
throwError: React.PropTypes.func,
|
|
|
|
actions: React.PropTypes.object,
|
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);
|