bruno/packages/grafnode-components/src/components/RequestTabPanel/index.js

159 lines
4.4 KiB
JavaScript
Raw Normal View History

import React, { useState, useEffect } from 'react';
import find from 'lodash/find';
import { rawRequest, gql } from 'graphql-request';
import QueryUrl from '../QueryUrl';
import RequestPane from '../RequestPane';
import ResponsePane from '../ResponsePane';
2022-01-04 18:00:15 +01:00
import Welcome from '../Welcome';
import {
flattenItems,
findItem
} from '../../utils';
import useGraphqlSchema from '../../hooks/useGraphqlSchema';
import StyledWrapper from './StyledWrapper';
const RequestTabPanel = ({dispatch, actions, collections, activeRequestTabId, requestTabs}) => {
if(typeof window == 'undefined') {
return <div></div>;
}
let asideWidth = 200;
let [data, setData] = useState({});
let [url, setUrl] = useState('https://api.spacex.land/graphql');
let {
schema
} = useGraphqlSchema('https://api.spacex.land/graphql');
let [query, setQuery] = useState('');
let [isLoading, setIsLoading] = useState(false);
const [leftPaneWidth, setLeftPaneWidth] = useState(500);
const [rightPaneWidth, setRightPaneWidth] = useState(window.innerWidth - 700 - asideWidth);
const [dragging, setDragging] = useState(false);
const handleMouseMove = (e) => {
e.preventDefault();
if(dragging) {
setLeftPaneWidth(e.clientX - asideWidth );
setRightPaneWidth(window.innerWidth - (e.clientX));
}
};
const handleMouseUp = (e) => {
e.preventDefault();
setDragging(false);
};
const handleMouseDown = (e) => {
e.preventDefault();
setDragging(true);
};
useEffect(() => {
document.addEventListener('mouseup', handleMouseUp);
document.addEventListener('mousemove', handleMouseMove);
return () => {
document.removeEventListener('mouseup', handleMouseUp);
document.removeEventListener('mousemove', handleMouseMove);
};
}, [dragging, leftPaneWidth]);
const onUrlChange = (value) => setUrl(value);
const onQueryChange = (value) => setQuery(value);
if(!activeRequestTabId) {
return (
2022-01-04 18:00:15 +01:00
<Welcome dispatch={dispatch} actions={actions}/>
);
}
const focusedTab = find(requestTabs, (rt) => rt.id === activeRequestTabId);
if(!focusedTab || !focusedTab.id) {
return (
<div className="pb-4 px-4">An error occured!</div>
);
}
2022-01-04 18:00:15 +01:00
let collection;
let item;
if(focusedTab.collectionId) {
collection = find(collections, (c) => c.id === focusedTab.collectionId);
let flattenedItems = flattenItems(collection.items);
item = findItem(flattenedItems, activeRequestTabId);
} else {
item = focusedTab;
}
const runQuery = async () => {
const query = gql`${item.request.body.graphql.query}`;
setIsLoading(true);
2022-01-01 20:18:12 +01:00
const timeStart = Date.now();
const { data, errors, extensions, headers, status } = await rawRequest(item.request.url, query);
2022-01-01 20:18:12 +01:00
const timeEnd = Date.now();
setData(data);
setIsLoading(false);
console.log(headers);
if(data && !errors) {
2022-01-01 20:39:54 +01:00
// todo: alternate way to calculate length when content length is not present
const size = headers.map["content-length"];
dispatch({
type: actions.RESPONSE_RECEIVED,
2022-01-01 19:42:38 +01:00
response: {
data: data,
headers: Object.entries(headers.map),
2022-01-01 20:39:54 +01:00
size: size,
2022-01-01 20:18:12 +01:00
status: status,
duration: timeEnd - timeStart
2022-01-01 19:42:38 +01:00
},
requestTab: focusedTab,
collectionId: collection.id
});
}
};
return (
<StyledWrapper>
<div
className="pb-4 px-4"
style={{
borderBottom: 'solid 1px #e1e1e1'
}}
>
<div className="pt-2 text-gray-600">{item.name}</div>
<QueryUrl
value = {url}
onChange={onUrlChange}
handleRun={runQuery}
/>
</div>
<section className="main">
<section className="request-pane px-4">
<div style={{width: `${leftPaneWidth}px`}}>
<RequestPane
onRunQuery={runQuery}
schema={schema}
leftPaneWidth={leftPaneWidth}
value={item.request.body.graphql.query}
onQueryChange={onQueryChange}
/>
</div>
</section>
<div className="drag-request" onMouseDown={handleMouseDown}>
</div>
<section className="response-pane px-4 flex-grow">
<ResponsePane
rightPaneWidth={rightPaneWidth}
2022-01-01 19:42:38 +01:00
response={item.response}
isLoading={isLoading}
/>
</section>
</section>
</StyledWrapper>
)
};
export default RequestTabPanel;