import React, { Component, PropTypes } from 'react'; import { connect } from 'react-redux'; import { FormattedMessage as Message } from 'react-intl'; import classNames from 'classnames'; import buttons from 'components/ui/buttons.scss'; import messages from './Finish.messages'; import styles from './finish.scss'; class Finish extends Component { static displayName = 'Finish'; static propTypes = { appName: PropTypes.string.isRequired, code: PropTypes.string.isRequired, displayCode: PropTypes.bool, success: PropTypes.bool }; state = { isCopySupported: document.queryCommandSupported && document.queryCommandSupported('copy') }; handleCopyClick = (event) => { event.preventDefault(); // http://stackoverflow.com/a/987376/5184751 try { const selection = window.getSelection(); const range = document.createRange(); range.selectNodeContents(this.code); selection.removeAllRanges(); selection.addRange(range); const successful = document.execCommand('copy'); selection.removeAllRanges(); // TODO: было бы ещё неплохо сделать какую-то анимацию, вроде "Скопировано", // ибо сейчас после клика как-то неубедительно, скопировалось оно или нет console.log('Copying text command was ' + (successful ? 'successful' : 'unsuccessful')); } catch (err) {} }; setCode = (el) => { this.code = el; }; render() { const {appName, code, displayCode, success} = this.props; const {isCopySupported} = this.state; return (
{success ? (
{appName}) }} />
{displayCode ? (
{code}
{isCopySupported ? ( ) : ( '' )}
) : (
)}
) : (
{appName}) }} />
)}
); } } export default connect((state) => ({ appName: state.auth.client ? state.auth.client.name : 'Undefined', code: 'HW9vkZA6Y4vRN3ciSm1IIDk98PHLkPPlv3jvo1MX', displayCode: true, success: true }))(Finish);