import React, {Component, PropTypes} from 'react';
import classnames from 'classnames';
import {DEBOUNCE_MILLIS, isMac} from '../../common/constants';
import {Dropdown, DropdownButton, DropdownItem, DropdownDivider, DropdownHint} from './base/dropdown';
import {trackEvent} from '../../analytics';
import MethodDropdown from './dropdowns/MethodDropdown';
import PromptModal from './modals/PromptModal';
import {showModal} from './modals/index';
class RequestUrlBar extends Component {
state = {
currentInterval: null,
currentTimeout: null,
};
_handleFormSubmit = e => {
e.preventDefault();
e.stopPropagation();
this.props.handleSend();
};
_handleMethodChange = method => {
this.props.onMethodChange(method);
trackEvent('Request', 'Method Change', method);
};
_handleUrlChange = e => {
const url = e.target.value;
clearTimeout(this._timeout);
this._timeout = setTimeout(() => {
this.props.onUrlChange(url);
}, DEBOUNCE_MILLIS);
};
_handleUrlPaste = e => {
e.preventDefault();
const text = e.clipboardData.getData('text/plain');
this.props.onUrlPaste(text);
// Set the input anyway in case nothing is able to import
e.target.value = text;
};
_handleGenerateCode = () => {
this.props.handleGenerateCode();
trackEvent('Request', 'Generate Code', 'Send Action');
};
_handleKeyDown = e => {
if (!this._input) {
return;
}
// meta+l
const metaPressed = isMac() ? e.metaKey : e.ctrlKey;
if (metaPressed && e.keyCode === 76) {
e.preventDefault();
this._input.focus();
this._input.select();
}
};
_handleSend = () => {
// Don't stop interval because duh, it needs to keep going!
// XXX this._handleStopInterval(); XXX
this._handleStopTimeout();
this.props.handleSend();
};
_handleSendAfterDelay = async () => {
const seconds = await showModal(PromptModal, {
inputType: 'decimal',
headerName: 'Send After Delay',
label: 'Delay in seconds',
defaultValue: 3,
submitName: 'Start',
});
this._handleStopTimeout();
this._sendTimeout = setTimeout(this._handleSend, seconds * 1000);
this.setState({currentTimeout: seconds});
trackEvent('Request', 'Send on Delay', 'Send Action', seconds);
};
_handleSendOnInterval = async () => {
const seconds = await showModal(PromptModal, {
inputType: 'decimal',
headerName: 'Send on Interval',
label: 'Interval in seconds',
defaultValue: 3,
submitName: 'Start',
});
this._handleStopInterval();
this._sendInterval = setInterval(this._handleSend, seconds * 1000);
this.setState({currentInterval: seconds});
trackEvent('Request', 'Send on Interval', 'Send Action', seconds);
};
_handleStopInterval = () => {
clearTimeout(this._sendInterval);
if (this.state.currentInterval) {
this.setState({currentInterval: null});
trackEvent('Request', 'Stop Send Interval');
}
};
_handleStopTimeout = () => {
clearTimeout(this._sendTimeout);
if (this.state.currentTimeout) {
this.setState({currentTimeout: null});
}
trackEvent('Request', 'Stop Send Timeout');
};
_handleClickSend = e => {
const metaPressed = isMac() ? e.metaKey : e.ctrlKey;
// If we're pressing a meta key, let the dropdown open
if (metaPressed) {
e.preventDefault(); // Don't submit the form
return;
}
// If we're not pressing a meta key, cancel dropdown and send the request
e.stopPropagation(); // Don't trigger the dropdown
this._handleFormSubmit(e);
};
componentDidMount () {
document.body.addEventListener('keydown', this._handleKeyDown);
document.body.addEventListener('keyup', this._handleKeyUp);
}
componentWillUnmount () {
document.body.removeEventListener('keydown', this._handleKeyDown);
document.body.removeEventListener('keyup', this._handleKeyUp);
}
renderSendButton () {
const {currentInterval, currentTimeout} = this.state;
let cancelButton = null;
if (currentInterval) {
cancelButton = (
)
} else if (currentTimeout) {
cancelButton = (
)
}
let sendButton;
if (!cancelButton) {
sendButton = (