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