File size: 1,422 Bytes
02a9316
5ebef9c
02a9316
5ebef9c
02a9316
 
5ebef9c
 
02a9316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ebef9c
 
 
02a9316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ebef9c
02a9316
5ebef9c
 
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
import React, { useState } from "react";

const App = () => {
  const [wordOne, setWordOne] = useState("");
  const [wordTwo, setWordTwo] = useState("");
  const [message, setMessage] = useState("");

  const onPressButton = () => {
    // Construct the URL with query parameters
    //const url = `https://abadesalex-emb-rep.hf.space/api/sum_of_lengths?word1=${wordOne}&word2=${wordTwo}`;
    const url = `http://localhost:8000/api/sum_of_lengths?word1=${wordOne}&word2=${wordTwo}`;

    fetch(url, {
      method: "GET",
      headers: {
        "Content-Type": "application/json",
      },
    })
      .then((response) => response.json())
      .then((data) => {
        if (data.sum !== undefined) {
          setMessage(`Sum of lengths: ${data.sum}`);
        } else {
          setMessage("Error: Unexpected response");
        }
      })
      .catch((error) => {
        console.error("Error:", error);
        setMessage("Error: Failed to fetch data");
      });
  };

  return (
    <div>
      <input
        type="text"
        value={wordOne}
        onChange={(e) => setWordOne(e.target.value)}
        placeholder="First Word"
      />
      <input
        type="text"
        value={wordTwo}
        onChange={(e) => setWordTwo(e.target.value)}
        placeholder="Second Word"
      />
      <button onClick={onPressButton}>Submit</button>
      <p>{message}</p>
    </div>
  );
};

export default App;