I’m doing a chat widget (a tab system where I have multiple tabs, each tab being a chat) and I’m trying to paint some sort of UX notification every time the user gets a new message from the backend (enduser). In order to do that what I’m trying to do is to emit a custom method from my child component onUpdate lifecycle hook that will trigger the parent component event that will add a class to my parent component in order to add a red border every time the final user send a new message.
This is my child component:
<template>
<div class="flex-col flex-nowrap">
<BaseTranslationBar />
<div id="chat-body" ref="chatBody" class="overflow-y-auto p-2">
<ul id="chatBox" class="mt-4">
<BaseChatMessage
v-for="(chatMessage, index) in chatSession.events"
:chatMessage="chatMessage"
:key="index"
:chatSession="chatSession"
:class="{ 'mt-0': index === 0, 'mt-4': index > 0 }"
/>
</ul>
</div>
<form @submit.prevent="sendMessage" class="chat-form relative mt-6">
<textarea
class="chat-textarea h-[160px] py-4"
placeholder="Add your message here..."
type="text"
v-model="message"
/>
<div
class="chat-input-buttons flex justify-end absolute bottom-0 right-0 pb-4"
>
<button
class="flex flex-col justify-center focus:outline-none focus:ring"
>
<i class="chat-input-button fa-paperclip fas text-blue-500"></i>
</button>
<button
type="submit"
value="Send"
class="flex flex-col justify-center focus:outline-none focus:ring"
>
<i class="chat-input-button fa-paper-plane fas text-blue-500"></i>
</button>
</div>
</form>
<BasePythiaBar class="mt-4" />
<BaseChatButtonRow :chatId="chatSession.id" class="mt-4" />
</div>
</template>
<script>
import { ref, onMounted, onUpdated } from "vue";
import ChatService from "@/services/ChatService.js";
import BaseChatMessage from "@/components/BaseChatMessage.vue";
import BaseTranslationBar from "@/components/BaseTranslationBar.vue";
import BasePythiaBar from "@/components/BasePythiaBar.vue";
import BaseChatButtonRow from "@/components/BaseChatButtonRow.vue";
export default {
emits: ["emittingChatTabUxState"],
components: {
BaseChatMessage,
BaseChatButtonRow,
BasePythiaBar,
BaseTranslationBar,
},
props: {
chatSession: {
type: Object,
default: function () {
return {};
},
},
},
setup(props, { emit }) {
const message = ref("");
const chatBody = ref(null);
onMounted(() => {
scrollChatBodyToEnd();
});
onUpdated(() => {
scrollChatBodyToEnd();
submitChatTabUxState();
});
const scrollChatBodyToEnd = () => {
if (chatBody.value) {
chatBody.value.scrollTop = chatBody.value.scrollHeight;
}
};
const submitChatTabUxState = () => {
emit("emittingChatTabUxState");
};
const sendMessage = () => {
const messageObject = {
aggregateId: props.chatSession.id,
message: message.value,
agentId: props.chatSession.agentId,
skippedValidationErrors: [],
};
ChatService.sendChatMessage(messageObject);
message.value = "";
};
return {
message,
sendMessage,
chatBody,
scrollChatBodyToEnd,
submitChatTabUxState,
};
},
};
</script>
As you can see everytime the dom of this component gets updated I’m emitting a custom event called “submitChatTabUxState” to the parent.
This is the parent component:
<template>
<div>
<ul
class="tag-menu flex space-x-2"
:class="defaultTagMenu ? 'default' : 'historic'"
role="tablist"
aria-label="Tabs Menu"
v-if="tabTitles && tabTitles.length"
>
<li
@click.stop.prevent="selectedTitle = title"
v-for="title in tabTitles"
:key="title"
:title="title"
role="presentation"
:class="{ selected: title === selectedTitle }"
>
<a href="#" role="tab" :class="updateChatClassValue"
@emittingChatTabUxState="updateChatClass()">
{{ title }}
</a>
</li>
</ul>
<slot />
</div>
</template>
<script>
import {
ref,
computed,
useSlots,
provide,
watch,
onMounted,
onUpdate,
} from "vue";
export default {
props: {
defaultTagMenu: {
type: Boolean,
default: true,
},
},
setup() {
const updateChatClassValue = ref("");
const slots = useSlots();
const tabTitles = computed(() =>
slots.default()[0].children.map((tab) => tab.props.title)
);
const tabTitlesLength = computed(() => tabTitles.value.length);
let selectedTitle = ref(tabTitles.value[0]);
provide("selectedTitle", selectedTitle);
provide("tabTitles", tabTitles);
watch(tabTitlesLength, (currentValue, oldValue) => {
if (currentValue < oldValue) {
selectedTitle.value = tabTitles.value[0];
}
});
onMounted(() => {
updateChatClass();
timing();
});
onUpdate(() => {
console.log("updating parent component");
});
const timing = () => {
setInterval(() => {
updateChatClassValue.value = "";
}, 2000);
};
const updateChatClass = () => {
console.log("Updating chat class");
updateChatClassValue.value = "chatUpdated";
};
return {
tabTitles,
selectedTitle,
tabTitlesLength,
timing,
updateChatClass,
updateChatClassValue,
};
},
};
</script>
Now what is confusing to me at this point is where to trigger the emitted event coming from my child component in the parent component. I don’t have a specific call to action to do this because I will need to trigger the custom event in my parent component every time the child component gets updated so I don’t know where should I place something like @emittingChatTabUxState=”updateChatClass()”. I’m adding it at the <a> tag at the moment but it’s not working.