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'; 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
; } 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 ( ); } const focusedTab = find(requestTabs, (rt) => rt.id === activeRequestTabId); if(!focusedTab || !focusedTab.id) { return (
An error occured!
); } 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); const timeStart = Date.now(); const { data, errors, extensions, headers, status } = await rawRequest(item.request.url, query); const timeEnd = Date.now(); setData(data); setIsLoading(false); console.log(headers); if(data && !errors) { // todo: alternate way to calculate length when content length is not present const size = headers.map["content-length"]; dispatch({ type: actions.RESPONSE_RECEIVED, response: { data: data, headers: Object.entries(headers.map), size: size, status: status, duration: timeEnd - timeStart }, requestTab: focusedTab, collectionId: collection.id }); } }; return (
{item.name}
) }; export default RequestTabPanel;