import { JSONTree, Label, Button, Tabs } from 'UI'
import cn from 'classnames';
import copy from 'copy-to-clipboard';
import stl from './fetchDetails.css';
import ResultTimings from '../../shared/ResultTimings/ResultTimings';
const REQUEST = 'REQUEST';
const RESPONSE = 'RESPONSE';
const TIMINGS = 'TIMINGS';
const TABS = [ REQUEST, RESPONSE, TIMINGS ].map(tab => ({ text: tab, key: tab }));
export default class FetchDetails extends React.PureComponent {
state = { activeTab: REQUEST, tabs: [] };
onTabClick = activeTab => this.setState({ activeTab })
componentDidMount() {
this.checkTabs();
}
renderActiveTab = tab => {
const { resource: { duration, timings }, isResult } = this.props;
switch(tab) {
case REQUEST:
const { resource: { payload } } = this.props;
let jsonPayload = undefined;
try {
jsonPayload = typeof payload === 'string' ? JSON.parse(payload) : payload
} catch (e) {}
return !!payload ? (
{/*
{ 'Payload '}
*/}
{ jsonPayload === undefined
?
{ payload }
:
}
) : ''
break;
case RESPONSE:
const { resource: { response = this.props.resource.body } } = this.props; // for IOS compat.
let jsonResponse = undefined;
try {
jsonResponse = JSON.parse(response);
} catch (e) {}
return !!response ? (
{/*
{ 'Response '}
*/}
{ jsonResponse === undefined
?
{ response }
:
}
// jsonResponse === undefined
// ? { response }
// :
) : ''
break;
case TIMINGS:
return
}
}
componentDidUpdate(prevProps) {
if (prevProps.resource.index === this.props.resource.index) return;
this.checkTabs();
}
checkTabs() {
const { resource: { payload, response, body }, isResult } = this.props;
const _tabs = TABS.filter(t => {
if (t.key == REQUEST && !!payload) {
return true
}
if (t.key == RESPONSE && !!response) {
return true;
}
if (t.key == TIMINGS && isResult) {
return true;
}
return false;
})
this.setState({ tabs: _tabs, activeTab: _tabs.length > 0 ? _tabs[0].key : null })
}
render() {
const {
resource: {
method,
url,
duration
},
nextClick,
prevClick,
first = false,
last = false,
} = this.props;
const { activeTab, tabs } = this.state;
return (
{ 'URL'}
{ url }
Duration
{parseInt(duration)} ms
{ this.renderActiveTab(activeTab) }
);
}
}