Skip to content

Commit

Permalink
Create SendRequest component
Browse files Browse the repository at this point in the history
This component handles sending test requests to the server.
  • Loading branch information
aaronvb committed Jul 8, 2021
1 parent 2c9a51b commit c2a8cbb
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 8 deletions.
28 changes: 20 additions & 8 deletions web/src/App.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Requests from './Requests'
import SendRequest from './SendRequest'
import {
useQuery,
gql
} from "@apollo/client"
import { useState } from 'react';

const SERVER_INFO = gql`
query GetServerInfo {
Expand All @@ -16,6 +18,15 @@ const SERVER_INFO = gql`
}
`;

const filters = [
"GET",
"POST",
"PUT",
"PATCH",
"DELETE",
"HEAD",
]

function ServerInfo(props) {
if (props.loading) return (
<div>Loading server info...</div>
Expand All @@ -37,41 +48,42 @@ function ServerInfo(props) {
}

function App() {
const {loading, error, data} = useQuery(SERVER_INFO);
const [ sendRequestVisible, setSendRequestVisible ] = useState(false);
const { loading, error, data } = useQuery(SERVER_INFO);
return (
<div>
<div className="">
<header className="text-gray-600 body-font border-b-2 bg-white">
<div className="container mx-auto flex flex-wrap p-5 flex-col md:flex-row items-center">
<a href="/" className="flex title-font font-medium items-center text-gray-900 mb-4 md:mb-0">
<span className="text-xl">Request Hole</span>
<h2 className="tracking-widest text-sm ml-2 title-font font-light text-gray-400">
{data ? data.serverInfo.build_info["version"] : ""}
{ data ? data.serverInfo.build_info["version"] : "" }
</h2>
</a>
<div className="md:mr-auto md:ml-4 md:py-1 md:pl-4 md:border-l md:border-gray-400 flex flex-wrap items-center text-base justify-center">
<ServerInfo loading={loading} error={error} data={data} />
</div>
<nav className="md:ml-auto flex flex-wrap items-center text-base justify-center">
<a href="/" className="mr-5 hover:text-gray-900 flex flex-wrap items-center text-base">
<button onClick={() => setSendRequestVisible(!sendRequestVisible)} className="focus:outline-none mr-5 hover:text-gray-900 flex flex-wrap items-center text-base">
<svg xmlns="https://www.w3.org/2000/svg" className="h-5 w-5 mr-1" viewBox="0 0 20 20" fill="currentColor">
<path d="M8.707 7.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l2-2a1 1 0 00-1.414-1.414L11 7.586V3a1 1 0 10-2 0v4.586l-.293-.293z" />
<path d="M3 5a2 2 0 012-2h1a1 1 0 010 2H5v7h2l1 2h4l1-2h2V5h-1a1 1 0 110-2h1a2 2 0 012 2v10a2 2 0 01-2 2H5a2 2 0 01-2-2V5z" />
</svg>
Send a Request
</a>
</button>
<a href="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/aaronvb/request_hole" className="hover:text-gray-900 flex flex-wrap items-center text-base">
<svg xmlns="https://www.w3.org/2000/svg" className="h-5 w-5 mr-1" viewBox="0 0 20 20" fill="currentColor">
<path fillRule="evenodd" d="M12.316 3.051a1 1 0 01.633 1.265l-4 12a1 1 0 11-1.898-.632l4-12a1 1 0 011.265-.633zM5.707 6.293a1 1 0 010 1.414L3.414 10l2.293 2.293a1 1 0 11-1.414 1.414l-3-3a1 1 0 010-1.414l3-3a1 1 0 011.414 0zm8.586 0a1 1 0 011.414 0l3 3a1 1 0 010 1.414l-3 3a1 1 0 11-1.414-1.414L16.586 10l-2.293-2.293a1 1 0 010-1.414z" clipRule="evenodd" />
</svg>
View Project on GitHub
</a>
</nav>

</div>
</header>
<Requests />
<SendRequest address={data ? data.serverInfo.request_address : ""} port={data ? data.serverInfo.request_port : ""} filters={filters} visible={sendRequestVisible} close={() => setSendRequestVisible(false)} />
<Requests filters={filters} />
</div>
);
}

export default App;
export default App;
93 changes: 93 additions & 0 deletions web/src/SendRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { useState, useEffect } from 'react';

function Filters(props) {
return props.filters.map((filter, i) => (
<option key={i}>{filter}</option>
));
}

function SendRequest(props) {
const [ method, setMethod ] = useState("GET");
const [ url, setUrl ] = useState("");
const [ body, setBody ] = useState(JSON.stringify({"hello": "world"}));

const sendRequest = () => {
fetch(url,
{
method: method,
body: (method === "GET" || method === "HEAD") ? null : body,
headers: {
'Content-Type': 'application/json'
}
}
)
};

useEffect(() => {
setUrl(`https://${props.address}:${props.port}`);
}, [props.address, props.port]);

if (!props.visible) {
return (
<div></div>
)
} else {
return(
<section className="text-gray-600 bg-gray-100 body-font h-full">
<div className="container p-5 mx-auto max-w-2xl">
<div className="bg-white rounded shadow py-4 px-4">
<h2 className="text-gray-900 text-lg mb-1 font-medium title-font">Send a Request</h2>
<div className="flex flex-wrap mb-4">
<div className="md:pr-1 md:w-2/6 sm:w-1/2 w-full">
<label htmlFor="method" className="tracking-midwest text-xs text-gray-400">METHOD</label>
<div className="flex">
<div className="relative w-full">
<select
name="method"
id="method"
className="w-full rounded border appearance-none border-gray-300 py-2 focus:outline-none focus:ring-2 focus:ring-red-200 focus:border-red-500 text-base pl-3 pr-10"
onChange={(e) => setMethod(e.target.value)}
value={method}>
<Filters filters={props.filters} />
</select>
<span className="absolute right-0 top-0 h-full w-10 text-center text-gray-600 pointer-events-none flex items-center justify-center">
<svg fill="none" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" className="w-4 h-4" viewBox="0 0 24 24">
<path d="M6 9l6 6 6-6"></path>
</svg>
</span>
</div>
</div>
</div>
<div className="md:pl-1 md:w-4/6 sm:w-1/2 w-full">
<div className="relative">
<label htmlFor="url" className="tracking-midwest text-xs text-gray-400">URL</label>
<input
type="text"
id="url"
name="url"
className="w-full rounded border border-gray-300 focus:border-red-500 focus:bg-white focus:ring-2 focus:ring-red-200 text-base outline-none text-gray-700 py-1 px-3 leading-8 transition-colors duration-200 ease-in-out"
value={url}
onChange={(e) => setUrl(e.target.value)}
/>
</div>
</div>
</div>
<div className="relative mb-4">
<label htmlFor="body" className="tracking-midwest text-xs text-gray-400">BODY</label>
<textarea
id="body"
name="body"
className="w-full bg-white rounded border border-gray-300 focus:border-red-500 focus:ring-2 focus:ring-red-200 h-32 text-base outline-none text-gray-700 py-1 px-3 resize-none leading-6 transition-colors duration-200 ease-in-out"
onChange={(e) => setBody(e.target.value)}
value={body} />
</div>
<button onClick={() => sendRequest()} className="mr-2 text-white bg-red-500 border-0 py-2 px-6 focus:outline-none hover:bg-red-600 rounded text-base">Send Request</button>
<button onClick={props.close} className="text-white bg-red-500 border-0 py-2 px-6 focus:outline-none hover:bg-red-600 rounded text-base">Close</button>
</div>
</div>
</section>
)
}
}

export default SendRequest;

0 comments on commit c2a8cbb

Please sign in to comment.