feat: refactored some things

This commit is contained in:
Lion Arar 2025-10-15 10:55:08 -04:00
parent 2a785d68ff
commit 579045d933
2 changed files with 27 additions and 28 deletions

View File

@ -10,6 +10,7 @@ import { chatbotService } from "../services/messageService";
import { pageContexts } from "src/page-contexts";
import type { contextObject } from "src/page-contexts/pages/types/context-object";
const isSessionReady = ref(false);
// Block to receive user from child
const userEmail = ref('')
@ -23,20 +24,6 @@ const HandleRole = (role: string) => {
userRole.value = role;
}
watch([userEmail, userRole], async ([email, role]) => {
if (email && role) {
// Both fields ready now send to your async service
try {
await chatbotService.sendUserCredentials(email, role)
console.log('✅ Sent user context successfully')
} catch (err) {
console.error('❌ Failed to send user context:', err)
}
}
})
// Block to enable editing the width of the drawer
const drawerWidth = ref(370);
@ -71,9 +58,14 @@ onMounted(() => {
const handleSend = async () => {
const userMessage = text.value.trim();
if (!userMessage) return
text.value = '';
if (!userMessage)
if (!isSessionReady.value) {
console.warn("Session not ready yet. Please wait...");
return;
}
text.value = '';
messages.push({
text: userMessage,
sent: true,
@ -103,7 +95,7 @@ const handleSend = async () => {
const index = messages.indexOf(thinkingMessage);
if (index !== -1) {
messages.splice(index, 1, {
text: "Sorry, something went wrong.",
text: "Sorry, Message wasn't able to sent.",
sent: false,
isThinking: false,
});
@ -112,6 +104,7 @@ const handleSend = async () => {
}
};
// Block to handle sending the url to n8n
const route = useRoute()
const sendPageContext = chatbotService.sendPageContext;
@ -120,8 +113,11 @@ const currentContext = computed(() =>
pageContexts.find(ctx => ctx.path === route.fullPath.replace('/', ''))
)
watch(currentContext, async (ctx) => {
if (!ctx) return;
watch([currentContext, userEmail, userRole], async ([ctx, email, role]) => {
if (!ctx || !email || !role) return;
if (isSessionReady.value) return;
const contextPayload: contextObject = {
name: ctx.name,
@ -129,7 +125,15 @@ watch(currentContext, async (ctx) => {
features: ctx.features,
path: ctx.path
}
await sendPageContext(contextPayload)
try {
await Promise.all([chatbotService.sendUserCredentials(email, role),
sendPageContext(contextPayload),
]);
isSessionReady.value = true;
} catch (err) {
console.error("Error", err)
}
},
{ immediate: true }
);
@ -161,18 +165,14 @@ watch(currentContext, async (ctx) => {
</div>
<div class="line-separator"></div>
<q-form
@submit="handleSend"
class="chat-footer row"
>
<q-form class="chat-footer row">
<q-input
v-model="text"
:label="$t('chatbot.chat_placeholder')"
autogrow
:autogrow="false"
class="col"
@keydown.enter="handleSend"
style="margin-left: 8px;"
@keyup.enter="handleSend"
/>
</q-form>
<div

View File

@ -21,7 +21,6 @@ export const chatbotService = {
email: string,
role: string
): Promise<boolean> => {
console.log("FrontService - Email: ", email);
const response = await api.post("/chat/user", { email, role });
return response.data;
},