2018-06-25 17:42:50 +00:00
|
|
|
import React, { PureComponent } from 'react';
|
2017-08-10 01:56:27 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2017-03-03 01:44:07 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
2018-10-17 16:42:33 +00:00
|
|
|
import { buildQueryStringFromParams, joinUrlAndQueryString, smartEncodeUrl } from 'insomnia-url';
|
2017-06-01 23:53:10 +00:00
|
|
|
import CopyButton from './base/copy-button';
|
2016-07-20 23:09:05 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
@autobind
|
2016-11-26 08:29:16 +00:00
|
|
|
class RenderedQueryString extends PureComponent {
|
2018-06-25 17:42:50 +00:00
|
|
|
constructor(props) {
|
2017-03-03 01:44:07 +00:00
|
|
|
super(props);
|
2017-03-06 17:51:58 +00:00
|
|
|
this._interval = null;
|
2017-03-03 01:44:07 +00:00
|
|
|
this.state = {
|
2018-12-12 17:36:11 +00:00
|
|
|
string: '',
|
2017-03-03 01:44:07 +00:00
|
|
|
};
|
|
|
|
}
|
2016-07-20 23:09:05 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
async _debouncedUpdate(props) {
|
2017-03-06 17:51:58 +00:00
|
|
|
clearTimeout(this._interval);
|
|
|
|
this._interval = setTimeout(() => {
|
|
|
|
this._update(props);
|
|
|
|
}, 300);
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
async _update(props) {
|
|
|
|
const { request } = props;
|
2017-03-06 17:51:58 +00:00
|
|
|
const enabledParameters = request.parameters.filter(p => !p.disabled);
|
2017-03-09 06:23:23 +00:00
|
|
|
|
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = await props.handleRender({
|
|
|
|
url: request.url,
|
2018-12-12 17:36:11 +00:00
|
|
|
parameters: enabledParameters,
|
2017-03-09 06:23:23 +00:00
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
// Just ignore failures
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result) {
|
2018-06-25 17:42:50 +00:00
|
|
|
const { url, parameters } = result;
|
2017-11-26 20:45:40 +00:00
|
|
|
const qs = buildQueryStringFromParams(parameters);
|
|
|
|
const fullUrl = joinUrlAndQueryString(url, qs);
|
2018-06-25 17:42:50 +00:00
|
|
|
this.setState({
|
2018-12-12 17:36:11 +00:00
|
|
|
string: smartEncodeUrl(fullUrl, request.settingEncodeUrl),
|
2018-06-25 17:42:50 +00:00
|
|
|
});
|
2017-03-09 06:23:23 +00:00
|
|
|
}
|
2016-07-20 23:16:28 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
componentDidMount() {
|
2016-07-20 23:16:28 +00:00
|
|
|
this._update(this.props);
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
componentWillUnmount() {
|
2017-03-06 17:51:58 +00:00
|
|
|
clearTimeout(this._interval);
|
2016-08-17 18:05:52 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
componentWillReceiveProps(nextProps) {
|
2016-07-21 19:15:35 +00:00
|
|
|
if (nextProps.request._id !== this.props.request._id) {
|
2017-03-06 17:51:58 +00:00
|
|
|
this._update(nextProps);
|
|
|
|
} else {
|
|
|
|
this._debouncedUpdate(nextProps);
|
2016-07-21 19:15:35 +00:00
|
|
|
}
|
2016-07-20 23:16:28 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
render() {
|
2017-06-01 23:53:10 +00:00
|
|
|
let inner = null;
|
2016-07-20 23:16:28 +00:00
|
|
|
if (this.state.string) {
|
2018-10-17 16:42:33 +00:00
|
|
|
inner = <span className="selectable force-wrap">{this.state.string}</span>;
|
2016-07-20 23:16:28 +00:00
|
|
|
} else {
|
2017-06-01 23:53:10 +00:00
|
|
|
inner = <span className="super-duper-faint italic">...</span>;
|
2016-07-20 23:16:28 +00:00
|
|
|
}
|
2017-06-01 23:53:10 +00:00
|
|
|
|
|
|
|
return (
|
2017-06-12 21:55:04 +00:00
|
|
|
<div className="wide scrollable">
|
2018-06-25 17:42:50 +00:00
|
|
|
<CopyButton
|
|
|
|
content={this.state.string}
|
|
|
|
className="pull-right text-right icon"
|
|
|
|
title="Copy URL"
|
|
|
|
confirmMessage="">
|
|
|
|
<i className="fa fa-copy" />
|
2017-06-01 23:53:10 +00:00
|
|
|
</CopyButton>
|
|
|
|
{inner}
|
|
|
|
</div>
|
|
|
|
);
|
2016-07-20 23:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-20 23:09:05 +00:00
|
|
|
|
2016-07-20 23:16:28 +00:00
|
|
|
RenderedQueryString.propTypes = {
|
2016-07-22 20:11:20 +00:00
|
|
|
request: PropTypes.object.isRequired,
|
2018-12-12 17:36:11 +00:00
|
|
|
handleRender: PropTypes.func.isRequired,
|
2016-07-20 23:09:05 +00:00
|
|
|
};
|
|
|
|
|
2016-07-20 23:16:28 +00:00
|
|
|
export default RenderedQueryString;
|