diff --git a/sk1/frontend/src/components/ChatInput.jsx b/sk1/frontend/src/components/ChatInput.jsx index 7f88423..745b167 100644 --- a/sk1/frontend/src/components/ChatInput.jsx +++ b/sk1/frontend/src/components/ChatInput.jsx @@ -1,51 +1,67 @@ -import React, { useState } from 'react'; -import { Send } from 'lucide-react'; - -const ChatInput = ({ onSubmit }) => { - const [input, setInput] = useState(''); - - const handleSubmit = async (e) => { - e.preventDefault(); - console.log("Formulár bol odoslaný!", input); // Добавили лог - if (input.trim()) { - try { - const response = await fetch("https://hate-backend-production.up.railway.app/api/predict", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ text: input }), - }); - const data = await response.json(); - //alert(`Výsledok analýzy: ${data.prediction}`); - - if (onSubmit) - { - onSubmit(input, data.prediction); - } - setInput(''); - } catch (error) { - console.error("Ошибка запроса:", error); - alert("Chyba pri analýze textu!"); - } - } - }; - - - return ( -
-
- setInput(e.target.value)} - placeholder="Zadajte text na analýzu" - className="text-input" - /> - -
-
- ); -}; - -export default ChatInput; +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("https://hate-backend-production.up.railway.app/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 ( +
+
+ setInput(e.target.value)} + placeholder="Zadajte text na analýzu" + className="text-input" + /> + +
+ + {error && ( +
+ ⚠️ {error} +
+ )} +
+ ); +}; + +export default ChatInput; + +