emb-rep / my-app /src /App.js
abadesalex's picture
test
02a9316
raw
history blame
1.42 kB
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;