|
| 1 | +import PropTypes from 'prop-types' |
| 2 | +import React from 'react' |
| 3 | +import CSSModules from 'browser/lib/CSSModules' |
| 4 | +import styles from './CreateMarkdownFromURLModal.styl' |
| 5 | +import dataApi from 'browser/main/lib/dataApi' |
| 6 | +import ModalEscButton from 'browser/components/ModalEscButton' |
| 7 | +import i18n from 'browser/lib/i18n' |
| 8 | + |
| 9 | +class CreateMarkdownFromURLModal extends React.Component { |
| 10 | + constructor (props) { |
| 11 | + super(props) |
| 12 | + |
| 13 | + this.state = { |
| 14 | + name: '', |
| 15 | + showerror: false, |
| 16 | + errormessage: '' |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + componentDidMount () { |
| 21 | + this.refs.name.focus() |
| 22 | + this.refs.name.select() |
| 23 | + } |
| 24 | + |
| 25 | + handleCloseButtonClick (e) { |
| 26 | + this.props.close() |
| 27 | + } |
| 28 | + |
| 29 | + handleChange (e) { |
| 30 | + this.setState({ |
| 31 | + name: this.refs.name.value |
| 32 | + }) |
| 33 | + } |
| 34 | + |
| 35 | + handleKeyDown (e) { |
| 36 | + if (e.keyCode === 27) { |
| 37 | + this.props.close() |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + handleInputKeyDown (e) { |
| 42 | + switch (e.keyCode) { |
| 43 | + case 13: |
| 44 | + this.confirm() |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + handleConfirmButtonClick (e) { |
| 49 | + this.confirm() |
| 50 | + } |
| 51 | + |
| 52 | + showError (message) { |
| 53 | + this.setState({ |
| 54 | + showerror: true, |
| 55 | + errormessage: message |
| 56 | + }) |
| 57 | + } |
| 58 | + |
| 59 | + hideError () { |
| 60 | + this.setState({ |
| 61 | + showerror: false, |
| 62 | + errormessage: '' |
| 63 | + }) |
| 64 | + } |
| 65 | + |
| 66 | + confirm () { |
| 67 | + this.hideError() |
| 68 | + const { storage, folder, dispatch, location } = this.props |
| 69 | + |
| 70 | + dataApi.createNoteFromUrl(this.state.name, storage, folder, dispatch, location).then((result) => { |
| 71 | + this.props.close() |
| 72 | + }).catch((result) => { |
| 73 | + this.showError(result.error) |
| 74 | + }) |
| 75 | + } |
| 76 | + |
| 77 | + render () { |
| 78 | + return ( |
| 79 | + <div styleName='root' |
| 80 | + tabIndex='-1' |
| 81 | + onKeyDown={(e) => this.handleKeyDown(e)} |
| 82 | + > |
| 83 | + <div styleName='header'> |
| 84 | + <div styleName='title'>{i18n.__('Import Markdown From URL')}</div> |
| 85 | + </div> |
| 86 | + <ModalEscButton handleEscButtonClick={(e) => this.handleCloseButtonClick(e)} /> |
| 87 | + <div styleName='control'> |
| 88 | + <div styleName='control-folder'> |
| 89 | + <div styleName='control-folder-label'>{i18n.__('Insert URL Here')}</div> |
| 90 | + <input styleName='control-folder-input' |
| 91 | + ref='name' |
| 92 | + value={this.state.name} |
| 93 | + onChange={(e) => this.handleChange(e)} |
| 94 | + onKeyDown={(e) => this.handleInputKeyDown(e)} |
| 95 | + /> |
| 96 | + </div> |
| 97 | + <button styleName='control-confirmButton' |
| 98 | + onClick={(e) => this.handleConfirmButtonClick(e)} |
| 99 | + > |
| 100 | + {i18n.__('Import')} |
| 101 | + </button> |
| 102 | + <div className='error' styleName='error'>{this.state.errormessage}</div> |
| 103 | + </div> |
| 104 | + </div> |
| 105 | + ) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +CreateMarkdownFromURLModal.propTypes = { |
| 110 | + storage: PropTypes.string, |
| 111 | + folder: PropTypes.string, |
| 112 | + dispatch: PropTypes.func, |
| 113 | + location: PropTypes.shape({ |
| 114 | + pathname: PropTypes.string |
| 115 | + }) |
| 116 | +} |
| 117 | + |
| 118 | +export default CSSModules(CreateMarkdownFromURLModal, styles) |
0 commit comments