zkt25/sk1/frontend/src/components/ChatInput.jsx

68 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React, { useState } from 'react';
import { Send } from 'lucide-react';
const ChatInput = ({ onSubmit }) => {
const [input, setInput] = useState('');
const [error, setError] = useState('');
const handleSubmit = async (e) => {
e.preventDefault();
setError(""); // reset chyby
if (input.trim()) {
try {
const response = await fetch("/api/predict", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ text: input }),
});
const data = await response.json();
if (!response.ok) {
setError(data.error || "Chyba pri analýze textu.");
return;
}
if (onSubmit) {
onSubmit(input, data.prediction);
}
setInput('');
} catch (error) {
console.error("Chyba požiadavky:", error);
setError("Nepodarilo sa spojiť so serverom.");
}
} else {
setError("Text nesmie byť prázdny.");
}
};
return (
<div className="input-container">
<form onSubmit={handleSubmit} className="input-wrapper">
<input
type="text"
value={input}
onChange={(e) => setInput(e.target.value)}
placeholder="Zadajte text na analýzu"
className="text-input"
/>
<button type="submit" className="submit-button" title="Spustiť analýzu">
<Send size={24} />
</button>
</form>
{error && (
<div className="mt-8 text-red-500 font-semibold text-center">
{error}
</div>
)}
</div>
);
};
export default ChatInput;