From ffde5ed5d592dc53fd6d72ac714763f39926caa3 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Thu, 23 Feb 2017 21:38:44 -0800 Subject: [PATCH] factor out embedly card into its own component; it seems to help --- .../src/components/TopicCard/Attachments.js | 74 +++---------------- .../src/components/TopicCard/EmbedlyCard.js | 65 ++++++++++++++++ 2 files changed, 77 insertions(+), 62 deletions(-) create mode 100644 frontend/src/components/TopicCard/EmbedlyCard.js diff --git a/frontend/src/components/TopicCard/Attachments.js b/frontend/src/components/TopicCard/Attachments.js index ee23915b..0ba7234f 100644 --- a/frontend/src/components/TopicCard/Attachments.js +++ b/frontend/src/components/TopicCard/Attachments.js @@ -1,39 +1,20 @@ /* global $, embedly */ import React, { PropTypes, Component } from 'react' +import EmbedlyCard from './EmbedlyCard' + class Attachments extends Component { constructor(props) { super(props) this.state = { - linkEdit: '', - embedlyLinkError: false, - embedlyLinkStarted: false, - embedlyLinkLoaded: false + linkEdit: '' } } - 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() + removeLink = () => { + this.props.updateTopic({ link: null }) + $('.embedly-card').remove() } onLinkChangeHandler = e => { @@ -41,46 +22,23 @@ class Attachments extends Component { } 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 - } + const { linkEdit } = this.state this.setState({ linkEdit: '' }) - this.props.updateTopic({ link: finalLink }) + this.props.updateTopic({ link: linkEdit }) } } - 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 { linkEdit } = this.state const hasAttachment = !!link if (!hasAttachment && !authorizedToEdit) return null + const embedlyLinkLoaded = 'TODO how to get this value' + const className = hasAttachment ? `embeds ${embedlyLinkLoaded ? '' : 'nonEmbedlyLink'}` : 'attachments' @@ -100,15 +58,7 @@ class Attachments extends Component { {linkEdit &&
} - - {link} - - {embedlyLinkStarted && !embedlyLinkLoaded && !embedlyLinkError &&
loading...
} + {link && } {authorizedToEdit && (
{ + 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}) + } + + loadLink = () => { + this.setState({ embedlyLinkStarted: true }) + var e = embedly('card', document.getElementById('embedlyLink')) + if (e && e.type === 'error') this.setState({embedlyLinkError: true}) + } + + render = () => { + const { link } = this.props + const { embedlyLinkLoaded, embedlyLinkStarted, embedlyLinkError } = this.state + + const notReady = embedlyLinkStarted && !embedlyLinkLoaded && !embedlyLinkError + + return ( +
+ + {link} + + {notReady &&
loading...
} +
+ ) + } +} + +EmbedlyCard.propTypes = { + link: PropTypes.string +} + +export default EmbedlyCard