/* global $, embedly */ import React, { PropTypes, Component } from 'react' class Attachments extends Component { constructor(props) { super(props) this.state = { linkEdit: '', embedlyLinkError: false, embedlyLinkStarted: false, embedlyLinkLoaded: false } } componentDidMount = () => { embedly('on', 'card.rendered', this.embedlyCardRendered) if (this.props.link) this.loadLink() } componentWillUnmount = () => { embedly('off') } componentDidUpdate = () => { const { embedlyLinkStarted } = this.state !embedlyLinkStarted && this.props.link && this.loadLink() } embedlyCardRendered = (iframe, test) => { this.setState({embedlyLinkLoaded: true, embedlyLinkError: false}) } resetLinkInput = () => { this.setState({ linkEdit: '' }) this.linkInput.focus() } onLinkChangeHandler = e => { this.setState({ linkEdit: e.target.value }) } onLinkKeyUpHandler = e => { const { linkEdit } = this.state let finalLink const ENTER_KEY = 13 if (e.which === ENTER_KEY) { // TODO evaluate converting this to '//' no matter what (infer protocol) if (linkEdit.slice(0, 7) !== 'http://' && linkEdit.slice(0, 8) !== 'https://' && linkEdit.slice(0, 2) !== '//') { finalLink = '//' + linkEdit } this.setState({ linkEdit: '' }) this.props.updateTopic({ link: finalLink }) } } loadLink = () => { debugger this.setState({ embedlyLinkStarted: true }) var e = embedly('card', document.getElementById('embedlyLink')) if (e && e.type === 'error') this.setState({embedlyLinkError: true}) } removeLink = () => { this.setState({ embedlyLinkStarted: false, embedlyLinkLoaded: false, embedlyLinkError: false }) this.props.updateTopic({ link: null }) $('div.embedly-card').after(``) $('.embedly-card').remove() } render = () => { const { link, authorizedToEdit } = this.props const { linkEdit, embedlyLinkLoaded, embedlyLinkStarted, embedlyLinkError } = this.state const hasAttachment = !!link if (!hasAttachment && !authorizedToEdit) return null const className = hasAttachment ? `embeds ${embedlyLinkLoaded ? '' : 'nonEmbedlyLink'}` : 'attachments' return (
(this.linkInput = input)} placeholder="Enter or paste a link" value={linkEdit} onChange={this.onLinkChangeHandler} onKeyUp={this.onLinkKeyUpHandler}> {linkEdit &&
}
{link} {embedlyLinkStarted && !embedlyLinkLoaded && !embedlyLinkError &&
loading...
} {authorizedToEdit && (
)}
) } } Attachments.propTypes = { link: PropTypes.string, authorizedToEdit: PropTypes.bool, updateTopic: PropTypes.func } export default Attachments