aboutsummaryrefslogtreecommitdiffstats
path: root/client/js/app/src/app/pages/querybuilder/query-endpoint/query-endpoint.jsx
blob: 4894f616b555771723983ea3e64fdae3867e4e94 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import React, { useState } from 'react';
import SimpleDropDownForm from 'app/pages/querybuilder/query-filters/SimpleDropDownForm';
import {
  ACTION,
  dispatch,
  useQueryBuilderContext,
} from 'app/pages/querybuilder/context/query-builder-provider';

function send(method, url, query) {
  dispatch(ACTION.SET_HTTP, { loading: true });
  fetch(url, {
    method,
    headers: { 'Content-Type': 'application/json;charset=utf-8' },
    body: query,
  })
    .then((response) => response.json())
    .then((result) =>
      dispatch(ACTION.SET_HTTP, {
        response: JSON.stringify(result, null, 4),
      })
    )
    .catch((error) => dispatch(ACTION.SET_HTTP, { error }));
}

export default function QueryEndpoint() {
  const messageMethods = { post: { name: 'POST' }, get: { name: 'GET' } };
  const [method, setMethod] = useState(messageMethods.post.name);
  const [url, setUrl] = useState('http://localhost:8080/search/');
  const query = useQueryBuilderContext((ctx) => ctx.query.input);

  const updateMethod = (e) => {
    e.preventDefault();
    const newMethod = e.target.value;
    setMethod(newMethod);
  };

  return (
    <>
      <SimpleDropDownForm
        options={messageMethods}
        value={method}
        className="methodselector"
        onChange={updateMethod}
      />
      <input
        size="30"
        className="textbox"
        value={url}
        onChange={({ target }) => setUrl(target.value)}
      />
      <button className="button" onClick={() => send(method, url, query)}>
        Send
      </button>
    </>
  );
}