summaryrefslogtreecommitdiff
path: root/web_page/webpage-shorlink/src/components/AddUrl.jsx
blob: 11124590f258cd6621678eef6064c853bc647570 (plain)
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
import "./AddUrl.css";

function AddUrl({ setShort }) {
  return (
    <div>
      <h1 id="title">ENCURTADOR DE URL</h1>
      <form
        id="id-form"
        method="post"
        onSubmit={(e) => {
          e.preventDefault();

          const form = e.target;
          const formData = new FormData(form);
          const formJson = Object.fromEntries(formData.entries());
          let data = formJson["url"];

          fetch("https://sl.azuminha.com/add/" + encodeURIComponent(data))
            .then((response) => {
              if (!response.ok) {
                throw new Error("Network response was not ok");
              }
              return response.text();
            })
            .then((result) => {
              console.log(result);
              setShort("sl.azuminha.com/" + result);
              //const res = document.createElement("p");
              //res.textContent = data + " => " + "sl.azuminha.com/" + result;
              //document.body.appendChild(res);
            });
        }}
      >
        <label id="id-label">
          URL:
          <input name="url" id="inp" type="text" />
        </label>
        <button
          id="btn1"
          onClick={() => {
            console.log("OLA");
          }}
        >
          enviar
        </button>
      </form>
    </div>
  );
}

export default AddUrl;