2016-08-08 11:18:21 +09:00
|
|
|
import Telescope from 'meteor/nova:lib';
|
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
|
|
|
import { graphql } from 'react-apollo';
|
|
|
|
import gql from 'graphql-tag';
|
|
|
|
|
2016-04-07 12:43:41 +09:00
|
|
|
const Input = FRC.Input;
|
|
|
|
|
|
|
|
class EmbedlyURL extends Component {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.handleBlur = this.handleBlur.bind(this);
|
|
|
|
this.state = {
|
|
|
|
loading: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// called whenever the URL input field loses focus
|
|
|
|
handleBlur() {
|
|
|
|
|
|
|
|
this.setState({loading: true});
|
|
|
|
|
2016-07-04 12:57:41 +09:00
|
|
|
const url = this.input.getValue();
|
|
|
|
|
|
|
|
if (url.length) {
|
|
|
|
// the URL has changed, get a new thumbnail
|
2016-12-06 15:51:59 +09:00
|
|
|
this.props.getEmbedlyData({variables: {url}}).then(result => {
|
2016-07-04 12:57:41 +09:00
|
|
|
this.setState({loading: false});
|
2016-12-06 15:51:59 +09:00
|
|
|
console.log(result)
|
|
|
|
this.context.addToAutofilledValues({
|
|
|
|
title: result.data.getEmbedlyData.title,
|
|
|
|
body: result.data.getEmbedlyData.description,
|
|
|
|
thumbnailUrl: result.data.getEmbedlyData.thumbnailUrl
|
|
|
|
});
|
|
|
|
}).catch(error => {
|
|
|
|
this.setState({loading: false});
|
|
|
|
console.log(error)
|
|
|
|
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 Loading = Telescope.components.Loading;
|
|
|
|
|
|
|
|
const wrapperStyle = {
|
|
|
|
position: "relative"
|
|
|
|
};
|
|
|
|
|
|
|
|
const loadingStyle = {
|
|
|
|
position: "absolute",
|
|
|
|
pointerEvents: "none",
|
|
|
|
top: "15px",
|
|
|
|
right: "15px"
|
|
|
|
};
|
|
|
|
|
|
|
|
loadingStyle.display = this.state.loading ? "block" : "none";
|
2016-09-07 14:35:49 +02:00
|
|
|
|
|
|
|
// see https://facebook.github.io/react/warnings/unknown-prop.html
|
2016-12-06 15:51:59 +09:00
|
|
|
const {document, updateCurrentValue, control, getEmbedlyData, ...rest} = this.props;
|
2016-04-07 12:43:41 +09:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="embedly-url-field" style={wrapperStyle}>
|
2016-04-09 10:22:44 +09:00
|
|
|
<Input
|
2016-09-07 14:35:49 +02:00
|
|
|
{...rest}
|
2016-04-09 10:22:44 +09:00
|
|
|
onBlur={this.handleBlur}
|
|
|
|
type="text"
|
|
|
|
ref={ref => this.input = ref}
|
|
|
|
/>
|
|
|
|
<div className="embedly-url-field-loading" style={loadingStyle}>
|
|
|
|
<Loading />
|
|
|
|
</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,
|
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 15:51:59 +09:00
|
|
|
function withGetEmbedlyData() {
|
|
|
|
return graphql(gql`
|
|
|
|
mutation getEmbedlyData($url: String) {
|
|
|
|
getEmbedlyData(url: $url) {
|
|
|
|
title
|
2016-12-06 16:25:50 +09:00
|
|
|
media
|
2016-12-06 15:51:59 +09:00
|
|
|
description
|
|
|
|
thumbnailUrl
|
|
|
|
sourceName
|
|
|
|
sourceUrl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, {
|
|
|
|
name: 'getEmbedlyData'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withGetEmbedlyData()(EmbedlyURL);
|