|
| 1 | +import React, { PropTypes } from 'react' |
| 2 | +import ReactDOM from 'react-dom' |
| 3 | +import api from 'boost/api' |
| 4 | +import clientKey from 'boost/clientKey' |
| 5 | +import activityRecord from 'boost/activityRecord' |
| 6 | +const clipboard = require('electron').clipboard |
| 7 | + |
| 8 | +function getDefault () { |
| 9 | + return { |
| 10 | + openDropdown: false, |
| 11 | + isSharing: false, |
| 12 | + // Fetched url |
| 13 | + url: null, |
| 14 | + // for tooltip Copy -> Copied! |
| 15 | + copied: false, |
| 16 | + failed: false |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +export default class ShareButton extends React.Component { |
| 21 | + constructor (props) { |
| 22 | + super(props) |
| 23 | + this.state = getDefault() |
| 24 | + } |
| 25 | + |
| 26 | + componentWillReceiveProps (nextProps) { |
| 27 | + this.setState(getDefault()) |
| 28 | + } |
| 29 | + |
| 30 | + componentDidMount () { |
| 31 | + this.dropdownInterceptor = e => { |
| 32 | + this.dropdownClicked = true |
| 33 | + } |
| 34 | + ReactDOM.findDOMNode(this.refs.dropdown).addEventListener('click', this.dropdownInterceptor) |
| 35 | + this.shareViaPublicURLHandler = e => { |
| 36 | + this.handleShareViaPublicURLClick(e) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + componentWillUnmount () { |
| 41 | + document.removeEventListener('click', this.dropdownHandler) |
| 42 | + ReactDOM.findDOMNode(this.refs.dropdown).removeEventListener('click', this.dropdownInterceptor) |
| 43 | + } |
| 44 | + |
| 45 | + handleOpenButtonClick (e) { |
| 46 | + this.openDropdown() |
| 47 | + if (this.dropdownHandler == null) { |
| 48 | + this.dropdownHandler = e => { |
| 49 | + if (!this.dropdownClicked) { |
| 50 | + this.closeDropdown() |
| 51 | + } else { |
| 52 | + this.dropdownClicked = false |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + document.removeEventListener('click', this.dropdownHandler) |
| 57 | + document.addEventListener('click', this.dropdownHandler) |
| 58 | + } |
| 59 | + |
| 60 | + openDropdown () { |
| 61 | + this.setState({openDropdown: true}) |
| 62 | + } |
| 63 | + |
| 64 | + closeDropdown () { |
| 65 | + document.removeEventListener('click', this.dropdownHandler) |
| 66 | + this.setState({openDropdown: false}) |
| 67 | + } |
| 68 | + |
| 69 | + handleShareViaPublicURLClick (e) { |
| 70 | + let { user } = this.props |
| 71 | + let input = Object.assign({}, this.props.article, { |
| 72 | + clientKey: clientKey.get(), |
| 73 | + writerName: user.name |
| 74 | + }) |
| 75 | + this.setState({ |
| 76 | + isSharing: true, |
| 77 | + failed: false |
| 78 | + }, () => { |
| 79 | + api.shareViaPublicURL(input) |
| 80 | + .then(res => { |
| 81 | + let url = res.body.url |
| 82 | + this.setState({url: url, isSharing: false}) |
| 83 | + activityRecord.emit('ARTICLE_SHARE') |
| 84 | + }) |
| 85 | + .catch(err => { |
| 86 | + console.log(err) |
| 87 | + this.setState({isSharing: false, failed: true}) |
| 88 | + }) |
| 89 | + }) |
| 90 | + } |
| 91 | + |
| 92 | + handleCopyURLClick () { |
| 93 | + clipboard.writeText(this.state.url) |
| 94 | + this.setState({copied: true}) |
| 95 | + } |
| 96 | + |
| 97 | + // Restore copy url tooltip |
| 98 | + handleCopyURLMouseLeave () { |
| 99 | + this.setState({copied: false}) |
| 100 | + } |
| 101 | + |
| 102 | + render () { |
| 103 | + let hasPublicURL = this.state.url != null |
| 104 | + return ( |
| 105 | + <div className='ShareButton'> |
| 106 | + <button ref='openButton' onClick={e => this.handleOpenButtonClick(e)} className='ShareButton-open-button'> |
| 107 | + <i className='fa fa-fw fa-share-alt'/> |
| 108 | + { |
| 109 | + this.state.openDropdown ? null : ( |
| 110 | + <span className='tooltip'>Share</span> |
| 111 | + ) |
| 112 | + } |
| 113 | + </button> |
| 114 | + <div ref='dropdown' className={'share-dropdown' + (this.state.openDropdown ? '' : ' hide')}> |
| 115 | + { |
| 116 | + !hasPublicURL ? ( |
| 117 | + <button |
| 118 | + onClick={e => this.shareViaPublicURLHandler(e)} |
| 119 | + ref='sharePublicURL' |
| 120 | + disabled={this.state.isSharing}> |
| 121 | + <i className='fa fa-fw fa-external-link'/> {this.state.failed ? 'Failed : Click to Try again' : !this.state.isSharing ? 'Share via public URL' : 'Sharing...'} |
| 122 | + </button> |
| 123 | + ) : ( |
| 124 | + <div className='ShareButton-url'> |
| 125 | + <input className='ShareButton-url-input' value={this.state.url} readOnly/> |
| 126 | + <button |
| 127 | + onClick={e => this.handleCopyURLClick(e)} |
| 128 | + className='ShareButton-url-button' |
| 129 | + onMouseLeave={e => this.handleCopyURLMouseLeave(e)} |
| 130 | + > |
| 131 | + <i className='fa fa-fw fa-clipboard'/> |
| 132 | + <div className='ShareButton-url-button-tooltip'>{this.state.copied ? 'Copied!' : 'Copy URL'}</div> |
| 133 | + </button> |
| 134 | + <div className='ShareButton-url-alert'>This url is valid for 7 days.</div> |
| 135 | + </div> |
| 136 | + ) |
| 137 | + } |
| 138 | + </div> |
| 139 | + </div> |
| 140 | + ) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +ShareButton.propTypes = { |
| 145 | + article: PropTypes.shape({ |
| 146 | + publicURL: PropTypes.string |
| 147 | + }), |
| 148 | + user: PropTypes.shape({ |
| 149 | + name: PropTypes.string |
| 150 | + }) |
| 151 | +} |
0 commit comments