2021-12-04 12:30:03 +01:00
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
import find from 'lodash/find';
|
|
|
|
import QueryUrl from '../QueryUrl';
|
2022-01-20 17:04:44 +01:00
|
|
|
import GraphQLRequestPane from '../GraphQLRequestPane';
|
|
|
|
import HttpRequestPane from '../HttpRequestPane';
|
2021-12-04 12:30:03 +01:00
|
|
|
import ResponsePane from '../ResponsePane';
|
2022-01-04 18:00:15 +01:00
|
|
|
import Welcome from '../Welcome';
|
2021-12-04 12:30:03 +01:00
|
|
|
import {
|
|
|
|
flattenItems,
|
|
|
|
findItem
|
|
|
|
} from '../../utils';
|
2021-12-06 14:29:49 +01:00
|
|
|
import useGraphqlSchema from '../../hooks/useGraphqlSchema';
|
2021-12-04 12:30:03 +01:00
|
|
|
|
|
|
|
import StyledWrapper from './StyledWrapper';
|
|
|
|
|
2021-12-09 17:44:49 +01:00
|
|
|
const RequestTabPanel = ({dispatch, actions, collections, activeRequestTabId, requestTabs}) => {
|
2021-12-04 12:30:03 +01:00
|
|
|
if(typeof window == 'undefined') {
|
|
|
|
return <div></div>;
|
|
|
|
}
|
|
|
|
|
|
|
|
let asideWidth = 200;
|
2021-12-06 14:29:49 +01:00
|
|
|
let {
|
|
|
|
schema
|
|
|
|
} = useGraphqlSchema('https://api.spacex.land/graphql');
|
2021-12-04 12:30:03 +01:00
|
|
|
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);
|
|
|
|
};
|
2022-01-18 17:11:27 +01:00
|
|
|
// useEffect(() => {
|
|
|
|
// document.addEventListener('mouseup', handleMouseUp);
|
|
|
|
// document.addEventListener('mousemove', handleMouseMove);
|
2021-12-04 12:30:03 +01:00
|
|
|
|
2022-01-18 17:11:27 +01:00
|
|
|
// return () => {
|
|
|
|
// document.removeEventListener('mouseup', handleMouseUp);
|
|
|
|
// document.removeEventListener('mousemove', handleMouseMove);
|
|
|
|
// };
|
|
|
|
// }, [dragging, leftPaneWidth]);
|
|
|
|
|
|
|
|
const onUrlChange = (value) => {
|
|
|
|
dispatch({
|
|
|
|
type: actions.REQUEST_URL_CHANGED,
|
|
|
|
url: value,
|
|
|
|
requestTab: focusedTab,
|
|
|
|
collectionId: collection ? collection.id : null
|
|
|
|
});
|
|
|
|
};
|
2021-12-04 12:30:03 +01:00
|
|
|
|
2022-01-18 17:25:22 +01:00
|
|
|
const onGraphqlQueryChange = (value) => {
|
|
|
|
console.log(value);
|
|
|
|
dispatch({
|
|
|
|
type: actions.REQUEST_GQL_QUERY_CHANGED,
|
|
|
|
query: value,
|
|
|
|
requestTab: focusedTab,
|
|
|
|
collectionId: collection ? collection.id : null
|
|
|
|
});
|
|
|
|
};
|
2021-12-04 12:30:03 +01:00
|
|
|
|
|
|
|
if(!activeRequestTabId) {
|
|
|
|
return (
|
2022-01-04 18:00:15 +01:00
|
|
|
<Welcome dispatch={dispatch} actions={actions}/>
|
2021-12-04 12:30:03 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2021-12-04 12:30:03 +01:00
|
|
|
|
|
|
|
const runQuery = async () => {
|
2022-01-18 15:58:18 +01:00
|
|
|
dispatch({
|
|
|
|
type: actions.SEND_REQUEST,
|
|
|
|
requestTab: focusedTab,
|
|
|
|
collectionId: collection ? collection.id : null
|
|
|
|
});
|
2021-12-04 12:30:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-01-07 20:26:10 +01:00
|
|
|
<StyledWrapper className="flex flex-col flex-grow">
|
2021-12-04 12:30:03 +01:00
|
|
|
<div
|
|
|
|
className="pb-4 px-4"
|
|
|
|
style={{
|
2022-01-24 23:11:35 +01:00
|
|
|
borderBottom: 'solid 1px var(--color-layout-border)'
|
2021-12-04 12:30:03 +01:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div className="pt-2 text-gray-600">{item.name}</div>
|
|
|
|
<QueryUrl
|
2022-01-18 17:11:27 +01:00
|
|
|
value = {item.request.url}
|
2021-12-04 12:30:03 +01:00
|
|
|
onChange={onUrlChange}
|
|
|
|
handleRun={runQuery}
|
2022-01-07 17:50:24 +01:00
|
|
|
collections={collections}
|
2021-12-04 12:30:03 +01:00
|
|
|
/>
|
|
|
|
</div>
|
2022-01-07 20:26:10 +01:00
|
|
|
<section className="main flex flex-grow">
|
2021-12-04 12:30:03 +01:00
|
|
|
<section className="request-pane px-4">
|
2022-01-07 20:26:10 +01:00
|
|
|
<div style={{width: `${leftPaneWidth}px`, height: 'calc(100% - 5px)'}}>
|
2022-01-20 17:04:44 +01:00
|
|
|
{item.request.type === 'graphql' ? (
|
|
|
|
<GraphQLRequestPane
|
|
|
|
onRunQuery={runQuery}
|
|
|
|
schema={schema}
|
|
|
|
leftPaneWidth={leftPaneWidth}
|
|
|
|
value={item.request.body.graphql.query}
|
|
|
|
onQueryChange={onGraphqlQueryChange}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
|
|
|
|
{item.request.type === 'http' ? (
|
|
|
|
<HttpRequestPane
|
|
|
|
leftPaneWidth={leftPaneWidth}
|
|
|
|
/>
|
|
|
|
) : null}
|
2021-12-04 12:30:03 +01:00
|
|
|
</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}
|
2022-01-18 15:58:18 +01:00
|
|
|
isLoading={item.response && item.response.state === 'sending' ? true : false}
|
2021-12-04 12:30:03 +01:00
|
|
|
/>
|
|
|
|
</section>
|
|
|
|
</section>
|
|
|
|
</StyledWrapper>
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
export default RequestTabPanel;
|