insomnia/app/components/RenderedQueryString.js

55 lines
1.2 KiB
JavaScript
Raw Normal View History

2016-07-20 23:16:28 +00:00
import React, {PropTypes, Component} from 'react';
import {getRenderedRequest} from '../lib/render';
import * as querystring from '../lib/querystring';
2016-07-20 23:09:05 +00:00
2016-07-20 23:16:28 +00:00
class RenderedQueryString extends Component {
constructor (props) {
super(props);
this.state = {
string: ''
2016-07-20 23:09:05 +00:00
}
}
2016-07-21 19:15:35 +00:00
_update (props, delay = false) {
2016-07-20 23:16:28 +00:00
clearTimeout(this._timeout);
this._timeout = setTimeout(() => {
2016-07-21 19:15:35 +00:00
getRenderedRequest(props.request).then(({url, parameters}) => {
2016-07-20 23:16:28 +00:00
const qs = querystring.buildFromParams(parameters);
2016-07-21 19:15:35 +00:00
const fullUrl = querystring.joinURL(url, qs);
this.setState({string: fullUrl});
2016-07-20 23:16:28 +00:00
});
2016-07-21 19:15:35 +00:00
}, delay ? 300 : 0);
2016-07-20 23:16:28 +00:00
}
componentDidMount () {
this._update(this.props);
}
componentWillReceiveProps (nextProps) {
2016-07-21 19:15:35 +00:00
let delay = true;
// Update right away if we're switching requests
if (nextProps.request._id !== this.props.request._id) {
delay = false;
}
this._update(nextProps, delay);
2016-07-20 23:16:28 +00:00
}
render () {
if (this.state.string) {
return <span>{this.state.string}</span>
} else {
return <span className="super-faint">add some parameters below</span>
}
}
}
2016-07-20 23:09:05 +00:00
2016-07-20 23:16:28 +00:00
RenderedQueryString.propTypes = {
request: PropTypes.object.isRequired
2016-07-20 23:09:05 +00:00
};
2016-07-20 23:16:28 +00:00
export default RenderedQueryString;