2021-12-04 12:30:03 +01:00
import React , { useState , useEffect } from 'react' ;
import find from 'lodash/find' ;
2022-03-18 10:15:27 +01:00
import { useSelector , useDispatch } from 'react-redux' ;
2022-03-19 13:05:51 +01:00
import QueryUrl from 'components/RequestPane/QueryUrl' ;
import GraphQLRequestPane from 'components/RequestPane/GraphQLRequestPane' ;
import HttpRequestPane from 'components/RequestPane/HttpRequestPane' ;
2022-03-13 22:13:36 +01:00
import ResponsePane from 'components/ResponsePane' ;
import Welcome from 'components/Welcome' ;
2022-03-18 10:15:27 +01:00
import { findItemInCollection } from 'utils/collections' ;
2022-03-22 13:48:20 +01:00
import { sendRequest } from 'providers/ReduxStore/slices/collections' ;
import { updateRequestPaneTabWidth } from 'providers/ReduxStore/slices/tabs' ;
import RequestNotFound from './RequestNotFound' ;
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' ;
2022-03-13 22:13:36 +01:00
const RequestTabPanel = ( ) => {
2021-12-04 12:30:03 +01:00
if ( typeof window == 'undefined' ) {
return < div > < / d i v > ;
}
2022-03-22 13:48:20 +01:00
const dispatch = useDispatch ( ) ;
2022-03-18 10:15:27 +01:00
const tabs = useSelector ( ( state ) => state . tabs . tabs ) ;
const activeTabUid = useSelector ( ( state ) => state . tabs . activeTabUid ) ;
const collections = useSelector ( ( state ) => state . collections . collections ) ;
2022-03-22 13:48:20 +01:00
const screenWidth = useSelector ( ( state ) => state . app . screenWidth ) ;
2022-03-13 22:13:36 +01:00
2022-03-20 19:33:03 +01:00
let asideWidth = useSelector ( ( state ) => state . app . leftSidebarWidth ) ;
2022-03-22 13:48:20 +01:00
const focusedTab = find ( tabs , ( t ) => t . uid === activeTabUid ) ;
const [ leftPaneWidth , setLeftPaneWidth ] = useState ( focusedTab && focusedTab . requestPaneWidth ? focusedTab . requestPaneWidth : ( ( screenWidth - asideWidth ) / 2.2 ) ) ; // 2.2 so that request pane is relatively smaller
const [ rightPaneWidth , setRightPaneWidth ] = useState ( screenWidth - asideWidth - leftPaneWidth - 5 ) ;
2021-12-04 12:30:03 +01:00
const [ dragging , setDragging ] = useState ( false ) ;
2022-03-22 13:48:20 +01:00
useEffect ( ( ) => {
const leftPaneWidth = ( screenWidth - asideWidth ) / 2.2 ;
setLeftPaneWidth ( leftPaneWidth ) ;
} , [ screenWidth ] ) ;
useEffect ( ( ) => {
setRightPaneWidth ( screenWidth - asideWidth - leftPaneWidth - 5 ) ;
} , [ screenWidth , asideWidth , leftPaneWidth ] ) ;
2021-12-04 12:30:03 +01:00
const handleMouseMove = ( e ) => {
if ( dragging ) {
2022-01-26 11:04:46 +01:00
e . preventDefault ( ) ;
2022-03-22 13:48:20 +01:00
setLeftPaneWidth ( e . clientX - asideWidth - 5 ) ;
setRightPaneWidth ( screenWidth - ( e . clientX ) - 5 ) ;
2021-12-04 12:30:03 +01:00
}
} ;
const handleMouseUp = ( e ) => {
2022-01-26 11:04:46 +01:00
if ( dragging ) {
e . preventDefault ( ) ;
setDragging ( false ) ;
2022-03-22 13:48:20 +01:00
dispatch ( updateRequestPaneTabWidth ( {
uid : activeTabUid ,
requestPaneWidth : e . clientX - asideWidth - 5
} ) ) ;
2022-01-26 11:04:46 +01:00
}
2021-12-04 12:30:03 +01:00
} ;
2022-01-26 11:04:46 +01:00
const handleDragbarMouseDown = ( e ) => {
2021-12-04 12:30:03 +01:00
e . preventDefault ( ) ;
setDragging ( true ) ;
} ;
2022-03-22 13:48:20 +01:00
let {
schema
} = useGraphqlSchema ( 'https://api.spacex.land/graphql' ) ;
2022-01-26 11:04:46 +01:00
useEffect ( ( ) => {
document . addEventListener ( 'mouseup' , handleMouseUp ) ;
document . addEventListener ( 'mousemove' , handleMouseMove ) ;
return ( ) => {
document . removeEventListener ( 'mouseup' , handleMouseUp ) ;
document . removeEventListener ( 'mousemove' , handleMouseMove ) ;
} ;
2022-03-22 13:48:20 +01:00
} , [ dragging , asideWidth ] ) ;
2022-01-18 17:11:27 +01:00
2021-12-04 12:30:03 +01:00
2022-03-18 10:15:27 +01:00
if ( ! activeTabUid ) {
2021-12-04 12:30:03 +01:00
return (
2022-03-18 16:50:11 +01:00
< Welcome / >
2021-12-04 12:30:03 +01:00
) ;
}
2022-03-14 18:46:49 +01:00
if ( ! focusedTab || ! focusedTab . uid || ! focusedTab . collectionUid ) {
2021-12-04 12:30:03 +01:00
return (
< div className = "pb-4 px-4" > An error occured ! < / d i v >
) ;
}
2022-03-14 18:46:49 +01:00
let collection = find ( collections , ( c ) => c . uid === focusedTab . collectionUid ) ;
if ( ! collection || ! collection . uid ) {
return (
< div className = "pb-4 px-4" > Collection not found ! < / d i v >
) ;
2022-01-04 18:00:15 +01:00
}
2021-12-04 12:30:03 +01:00
2022-03-18 10:15:27 +01:00
const item = findItemInCollection ( collection , activeTabUid ) ;
2022-03-18 20:35:43 +01:00
if ( ! item || ! item . uid ) {
return (
2022-03-22 13:48:20 +01:00
< RequestNotFound itemUid = { activeTabUid } / >
2022-03-18 20:35:43 +01:00
) ;
2022-03-15 20:30:35 +01:00
} ;
2022-03-22 13:48:20 +01:00
const onGraphqlQueryChange = ( value ) => { } ;
const runQuery = async ( ) => { } ;
const sendNetworkRequest = async ( ) => dispatch ( sendRequest ( item , collection . uid ) ) ;
2022-03-14 18:46:49 +01:00
2021-12-04 12:30:03 +01:00
return (
2022-03-22 13:48:20 +01:00
< StyledWrapper className = { ` flex flex-col flex-grow ${ dragging ? 'dragging' : '' } ` } >
2021-12-04 12:30:03 +01:00
< div
2022-03-22 13:48:20 +01:00
className = "px-4 pt-6 pb-4"
2021-12-04 12:30:03 +01:00
style = { {
2022-03-22 13:48:20 +01:00
borderBottom : 'solid 1px var(--color-request-dragbar-background)'
2021-12-04 12:30:03 +01:00
} }
>
< QueryUrl
2022-03-22 13:48:20 +01:00
item = { item }
collection = { collection }
2022-03-14 18:46:49 +01:00
handleRun = { sendNetworkRequest }
2021-12-04 12:30:03 +01:00
/ >
< / d i v >
2022-01-07 20:26:10 +01:00
< section className = "main flex flex-grow" >
2022-03-22 13:48:20 +01:00
< section className = "request-pane mt-2" >
2022-01-26 11:04:46 +01:00
< div
className = "px-4"
style = { { width : ` ${ leftPaneWidth } px ` , height : 'calc(100% - 5px)' } }
>
2022-03-14 18:46:49 +01:00
{ item . type === 'graphql-request' ? (
2022-01-20 17:04:44 +01:00
< GraphQLRequestPane
onRunQuery = { runQuery }
schema = { schema }
leftPaneWidth = { leftPaneWidth }
value = { item . request . body . graphql . query }
onQueryChange = { onGraphqlQueryChange }
/ >
) : null }
2022-03-14 18:46:49 +01:00
{ item . type === 'http-request' ? (
2022-01-20 17:04:44 +01:00
< HttpRequestPane
2022-03-19 14:03:16 +01:00
item = { item }
collection = { collection }
2022-01-20 17:04:44 +01:00
leftPaneWidth = { leftPaneWidth }
/ >
) : null }
2021-12-04 12:30:03 +01:00
< / d i v >
< / s e c t i o n >
2022-01-26 11:04:46 +01:00
< div className = "drag-request" onMouseDown = { handleDragbarMouseDown } >
2022-03-22 13:48:20 +01:00
< div className = "drag-request-border" / >
2021-12-04 12:30:03 +01:00
< / d i v >
2022-03-22 13:48:20 +01:00
< section className = "response-pane flex-grow mt-2" >
2021-12-04 12:30:03 +01:00
< ResponsePane
2022-03-22 13:48:20 +01:00
item = { item }
2021-12-04 12:30:03 +01:00
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
/ >
< / s e c t i o n >
< / s e c t i o n >
< / S t y l e d W r a p p e r >
)
} ;
export default RequestTabPanel ;