Skip to content

Commit

Permalink
Merge pull request #43 from Facundodandrea/main
Browse files Browse the repository at this point in the history
feat: ✨ Connect frontend with backend ia endpoint
  • Loading branch information
felixicaza authored Jul 24, 2024
2 parents 842f83b + 8837aff commit 0a6e90d
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions frontend/src/components/ChatBox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import SenderButton from './SenderButton'
type ChatMode = 'text' | 'audio'

const ChatBox = () => {
const [loading, setLoading] = useState(false)
const [inputText, setInputText] = useState('')
const initialMode = useMemo<ChatMode>(
() => (inputText ? 'text' : 'audio'),
Expand All @@ -31,16 +32,30 @@ const ChatBox = () => {
}
}, [recognitionText])

const askToAPI = async (prompt: string) => {
const res = await fetch(`https://localhost:3000/ai/ask`, {
method: 'POST',
body: JSON.stringify({
prompt
}),
headers: {
'Content-Type': 'application/json'
}
})
const data = await res.json()
return data.response
}

const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault()
if (!inputText) return

sendMessage(inputText, 'user')
setInputText('')
// Simular respuesta
sendMessage(
'Mi no hablar idioma humano, mi ser Elena Banana, yo ser la super IA.',
'assistant'
)

setLoading(true)
askToAPI(inputText)
.then((res) => sendMessage(res, 'assistant'))
.finally(() => setLoading(false))
}

return (
Expand All @@ -58,6 +73,7 @@ const ChatBox = () => {
setInputText(e.target.value)
}}
maxLength={1000}
disabled={loading}
/>
{showRecorder ? (
<RecorderButton />
Expand Down

0 comments on commit 0a6e90d

Please sign in to comment.