|
<script lang="ts"> |
|
import { goto } from "$app/navigation"; |
|
import { base } from "$app/paths"; |
|
import ChatWindow from "$lib/components/chat/ChatWindow.svelte"; |
|
import { ERROR_MESSAGES, error } from "$lib/stores/errors"; |
|
import { pendingMessage } from "$lib/stores/pendingMessage"; |
|
|
|
let loading = false; |
|
|
|
async function createConversation(message: string) { |
|
try { |
|
loading = true; |
|
const res = await fetch(`${base}/conversation`, { |
|
method: 'POST', |
|
headers: { |
|
'Content-Type': 'application/json' |
|
} |
|
}); |
|
|
|
if (!res.ok) { |
|
error.set("Error while creating conversation, try again."); |
|
console.error("Error while creating conversation: " + (await res.text())); |
|
return; |
|
} |
|
|
|
const { conversationId } = await res.json(); |
|
|
|
|
|
pendingMessage.set(message); |
|
|
|
|
|
await goto(`${base}/conversation/${conversationId}`, { invalidateAll: true }); |
|
} catch (err) { |
|
error.set(ERROR_MESSAGES.default); |
|
console.error(err); |
|
} finally { |
|
loading = false; |
|
} |
|
} |
|
</script> |
|
|
|
<ChatWindow on:message={(ev) => createConversation(ev.detail)} {loading} /> |
|
|