2017-08-10 01:56:27 +00:00
|
|
|
import React, {PureComponent} from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-03-08 00:45:18 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
2017-06-01 02:04:27 +00:00
|
|
|
import {Dropdown, DropdownButton, DropdownDivider, DropdownItem} from '../base/dropdown';
|
2016-11-27 21:42:38 +00:00
|
|
|
import * as constants from '../../../common/constants';
|
2017-06-01 02:04:27 +00:00
|
|
|
import {showPrompt} from '../modals/index';
|
2017-04-20 17:18:00 +00:00
|
|
|
import {trackEvent} from '../../../analytics/index';
|
2016-11-27 21:42:38 +00:00
|
|
|
|
2017-03-08 05:52:17 +00:00
|
|
|
const LOCALSTORAGE_KEY = 'insomnia.httpMethods';
|
2017-03-08 00:45:18 +00:00
|
|
|
|
|
|
|
@autobind
|
2017-02-28 21:32:23 +00:00
|
|
|
class MethodDropdown extends PureComponent {
|
2017-06-01 02:04:27 +00:00
|
|
|
_handleSetCustomMethod () {
|
2017-03-08 00:45:18 +00:00
|
|
|
let recentMethods;
|
|
|
|
try {
|
|
|
|
const v = window.localStorage.getItem(LOCALSTORAGE_KEY);
|
2017-03-08 05:52:17 +00:00
|
|
|
recentMethods = JSON.parse(v) || [];
|
2017-03-08 00:45:18 +00:00
|
|
|
} catch (err) {
|
|
|
|
recentMethods = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prompt user for the method
|
2017-06-01 02:04:27 +00:00
|
|
|
showPrompt({
|
2017-03-08 00:45:18 +00:00
|
|
|
defaultValue: this.props.method,
|
2017-07-20 01:55:40 +00:00
|
|
|
title: 'HTTP Method',
|
2017-03-08 00:45:18 +00:00
|
|
|
submitName: 'Done',
|
|
|
|
upperCase: true,
|
|
|
|
selectText: true,
|
|
|
|
hint: 'Common examples are LINK, UNLINK, FIND, PURGE',
|
2017-03-08 05:52:17 +00:00
|
|
|
label: 'Name',
|
2017-03-08 00:45:18 +00:00
|
|
|
placeholder: 'CUSTOM',
|
2017-06-01 02:04:27 +00:00
|
|
|
hints: recentMethods,
|
2017-06-09 22:00:36 +00:00
|
|
|
onDeleteHint: method => {
|
|
|
|
recentMethods = recentMethods.filter(m => m !== method);
|
|
|
|
window.localStorage.setItem(LOCALSTORAGE_KEY, JSON.stringify(recentMethods));
|
|
|
|
},
|
2017-06-01 02:04:27 +00:00
|
|
|
onComplete: method => {
|
2017-06-09 22:00:36 +00:00
|
|
|
// Don't add empty methods
|
2017-06-01 02:04:27 +00:00
|
|
|
if (!method) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-09 22:00:36 +00:00
|
|
|
// Don't add base methods
|
|
|
|
if (constants.HTTP_METHODS.includes(method)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-01 02:04:27 +00:00
|
|
|
// Save method as recent
|
|
|
|
recentMethods = recentMethods.filter(m => m !== method);
|
|
|
|
recentMethods.unshift(method);
|
|
|
|
window.localStorage.setItem(LOCALSTORAGE_KEY, JSON.stringify(recentMethods));
|
|
|
|
|
|
|
|
// Invoke callback
|
|
|
|
this.props.onChange(method);
|
|
|
|
trackEvent('Request', 'Set Method', 'Custom');
|
|
|
|
}
|
2017-03-08 00:45:18 +00:00
|
|
|
});
|
2017-04-20 17:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_handleChange (method) {
|
|
|
|
this.props.onChange(method);
|
|
|
|
trackEvent('Request', 'Set Method', method);
|
2017-03-08 00:45:18 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 21:42:38 +00:00
|
|
|
render () {
|
2017-04-20 17:18:00 +00:00
|
|
|
const {
|
|
|
|
method,
|
|
|
|
right,
|
|
|
|
onChange, // eslint-disable-line no-unused-vars
|
|
|
|
...extraProps
|
|
|
|
} = this.props;
|
2016-11-27 21:42:38 +00:00
|
|
|
return (
|
|
|
|
<Dropdown className="method-dropdown" right={right}>
|
|
|
|
<DropdownButton type="button" {...extraProps}>
|
|
|
|
{method} <i className="fa fa-caret-down"/>
|
|
|
|
</DropdownButton>
|
|
|
|
{constants.HTTP_METHODS.map(method => (
|
|
|
|
<DropdownItem key={method}
|
|
|
|
className={`http-method-${method}`}
|
2017-04-20 17:18:00 +00:00
|
|
|
onClick={this._handleChange}
|
2016-11-27 21:42:38 +00:00
|
|
|
value={method}>
|
|
|
|
{method}
|
|
|
|
</DropdownItem>
|
|
|
|
))}
|
2017-03-08 05:52:17 +00:00
|
|
|
<DropdownDivider/>
|
|
|
|
<DropdownItem className="http-method-custom"
|
2017-03-08 00:45:18 +00:00
|
|
|
onClick={this._handleSetCustomMethod}
|
|
|
|
value={method}>
|
2017-04-21 04:30:52 +00:00
|
|
|
Custom Method
|
2017-03-08 00:45:18 +00:00
|
|
|
</DropdownItem>
|
2016-11-27 21:42:38 +00:00
|
|
|
</Dropdown>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-11-27 21:42:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MethodDropdown.propTypes = {
|
2017-03-08 05:52:17 +00:00
|
|
|
// Required
|
2016-11-27 21:42:38 +00:00
|
|
|
onChange: PropTypes.func.isRequired,
|
2017-03-08 05:52:17 +00:00
|
|
|
method: PropTypes.string.isRequired,
|
|
|
|
|
|
|
|
// Optional
|
|
|
|
right: PropTypes.bool
|
2016-11-27 21:42:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default MethodDropdown;
|