Skip to content
Snippets Groups Projects
Commit 28caa556 authored by Sander August Heggland Schrader's avatar Sander August Heggland Schrader
Browse files

added chat notification

parent 5e635cb3
No related branches found
No related tags found
1 merge request!119New chat2
...@@ -19,11 +19,14 @@ ...@@ -19,11 +19,14 @@
/> />
</li> </li>
<li> <li>
<ChatAlt2Icon <div class="notification-container">
class="m-6 cursor-pointer h-7" <ChatAlt2Icon
alt="Meldinger" class="m-6 cursor-pointer h-7"
@click="$router.push('/messages')" alt="Meldinger"
/> @click="loadMessages()"
/>
<p @click="loadMessages()" class="notification" v-if="newMessages > 0">{{notifications}}</p>
</div>
</li> </li>
<li> <li>
<UserCircleIcon <UserCircleIcon
...@@ -39,10 +42,25 @@ ...@@ -39,10 +42,25 @@
<script> <script>
import { parseUserFromToken } from "@/utils/token-utils"; import { parseUserFromToken } from "@/utils/token-utils";
import { PlusIcon, ChatAlt2Icon, UserCircleIcon } from "@heroicons/vue/outline"; import { PlusIcon, ChatAlt2Icon, UserCircleIcon } from "@heroicons/vue/outline";
import ws from '@/services/ws';
export default { export default {
name: "NavBar.vue", name: "NavBar.vue",
data() {
return {
newMessages: 0,
}
},
computed: {
notifications() {
// if new messages is greater than 99 show +99
if (this.newMessages > 99) {
return '+99'
} else {
return this.newMessages
}
}
},
components: { components: {
PlusIcon, PlusIcon,
ChatAlt2Icon, ChatAlt2Icon,
...@@ -61,8 +79,37 @@ export default { ...@@ -61,8 +79,37 @@ export default {
await this.$router.push("/login"); await this.$router.push("/login");
} }
}, },
loadMessages() {
this.newMessages = 0;
this.$router.push('/messages');
}
}, },
created() {
ws.on('NEW_MESSAGE', () => {
this.newMessages += 1;
}, "header");
}
}; };
</script> </script>
<style scoped></style> <style scoped>
.notification-container {
position: relative;
}
.notification {
position: absolute;
background-color: #ff5a5f;
top: 0;
min-width: 20px;
min-height: 20px;
padding: 0.25rem;
transform: translate(-80%, -30%);
color: white;
font-size: 10px;
border-radius: 50%;
font-weight: bold;
text-align: center;
right: 0;
cursor: pointer;
}
</style>
...@@ -84,10 +84,13 @@ export default { ...@@ -84,10 +84,13 @@ export default {
ws.on("NEW_MESSAGE", () => { ws.on("NEW_MESSAGE", () => {
this.reloadMessages(); this.reloadMessages();
}); }, "chats");
this.recipientID = (this.$route.query?.userID || null) this.recipientID = (this.$route.query?.userID || null)
if(!this.recipientID) this.hambugerDisplay = "block"; if(!this.recipientID) this.hambugerDisplay = "block";
}, },
unmounted() {
ws.end("NEW_MESSAGE", "chats");
}
}; };
</script> </script>
<style scoped> <style scoped>
......
...@@ -6,11 +6,15 @@ import { parseCurrentUser } from "@/utils/token-utils"; ...@@ -6,11 +6,15 @@ import { parseCurrentUser } from "@/utils/token-utils";
// Events fired by websocket, MESSAGE // Events fired by websocket, MESSAGE
const ws = (function () { const ws = (function () {
// Object of all injected functions that the client may want // Object of all injected functions that the client may want
// These functions will be in a object
const handlers = {}; const handlers = {};
const fire = function (event, data) { const fire = function (event, data) {
if (handlers[event]) { if (handlers[event]) {
handlers[event](data); // for each object in object fire event
for(const key in handlers[event]) {
handlers[event][key](data);
}
} }
}; };
...@@ -42,16 +46,20 @@ const ws = (function () { ...@@ -42,16 +46,20 @@ const ws = (function () {
stompClient.connect({}, onConnected, onError); stompClient.connect({}, onConnected, onError);
return { return {
on: function (event, callback) { on: function (event, callback, id = "none") {
handlers[event] = callback; // Generate random id
if(!handlers[event]) {
handlers[event] = {}
};
handlers[event][id] = callback;
}, },
fire: fire, fire: fire,
isActive: function (event) { isActive: function (event, id = "none") {
return !!handlers[event]; return !!handlers[event]?.[id];
}, },
end: function (event) { end: function (event, id = "none") {
if (handlers[event]) { if (handlers[event]) {
delete handlers[event]; delete handlers[event][id];
} else { } else {
throw new Error("No handler for event: " + event); throw new Error("No handler for event: " + event);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment