🔨 Message can be readed

This commit is contained in:
SebClem 2022-10-26 14:09:10 +02:00
parent 2cd2725fb7
commit 48d33906fc
Signed by: sebclem
GPG Key ID: 5A4308F6A359EA50
5 changed files with 44 additions and 8 deletions

View File

@ -5,6 +5,14 @@ const messageRouter = express.Router();
messageRouter.get('/', (req, res, next)=>{ messageRouter.get('/', (req, res, next)=>{
res.json(messageManager.get()) res.json(messageManager.get())
}) });
messageRouter.patch('/:messageId/readed', (req, res, next)=>{
if(messageManager.markReaded(req.params.messageId)){
res.json(messageManager.get());
}else{
res.status(404).send();
}
});
export default messageRouter; export default messageRouter;

View File

@ -40,6 +40,19 @@ class MessageManager {
public get(){ public get(){
return this.messages; return this.messages;
} }
public getById(id: string){
return this.messages.find(value=>value.id == id);
}
public markReaded(id: string){
const index = this.messages.findIndex(value=>value.id == id);
if(index == -1){
return false;
}
this.messages[index].viewed = true;
return true;
}
} }

View File

@ -36,7 +36,7 @@
<v-btn <v-btn
color="success" color="success"
variant="text" variant="text"
@click.stop @click="markReaded(item.id)"
icon icon
v-if="!item.viewed" v-if="!item.viewed"
size="small" size="small"
@ -66,7 +66,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { getMessages } from "@/services/messageService"; import * as messageService from "@/services/messageService";
import { useMessageStore } from "@/stores/message"; import { useMessageStore } from "@/stores/message";
import { MessageType } from "@/types/messages"; import { MessageType } from "@/types/messages";
import { DateTime } from "luxon"; import { DateTime } from "luxon";
@ -76,10 +76,10 @@ import { onBeforeUnmount, ref } from "vue";
const messagesStore = useMessageStore(); const messagesStore = useMessageStore();
const { messages } = storeToRefs(messagesStore); const { messages } = storeToRefs(messagesStore);
const interval = setInterval(refreshMessages, 1000); const interval = setInterval(refreshMessages, 2000);
function refreshMessages() { function refreshMessages() {
getMessages().then((values) => { messageService.getMessages().then((values) => {
messages.value = values; messages.value = values;
}); });
} }
@ -142,6 +142,12 @@ function getTimeDelta(time: string) {
const show = ref<boolean[]>([]); const show = ref<boolean[]>([]);
refreshMessages(); refreshMessages();
function markReaded(id: string) {
messageService.markRead(id).then((values) => {
messages.value = values;
});
}
onBeforeUnmount(() => { onBeforeUnmount(() => {
clearInterval(interval); clearInterval(interval);
}); });

View File

@ -10,9 +10,14 @@
</v-app-bar-title> </v-app-bar-title>
<v-spacer></v-spacer> <v-spacer></v-spacer>
<v-btn icon id="message-btn"> <v-btn icon id="message-btn">
<v-badge color="error" :content="countUnreadMessages"> <v-badge
<v-icon :class="{ shake: haveUnreadMessages }">mdi-bell</v-icon> color="error"
:content="countUnreadMessages"
v-if="haveUnreadMessages"
>
<v-icon class="shake">mdi-bell</v-icon>
</v-badge> </v-badge>
<v-icon v-else>mdi-bell</v-icon>
</v-btn> </v-btn>
<v-menu width="210px"> <v-menu width="210px">
<template v-slot:activator="{ props }"> <template v-slot:activator="{ props }">

View File

@ -4,3 +4,7 @@ import kyClient from "./kyClient";
export function getMessages() { export function getMessages() {
return kyClient.get("messages").json<Message[]>(); return kyClient.get("messages").json<Message[]>();
} }
export function markRead(id: string) {
return kyClient.patch(`messages/${encodeURI(id)}/readed`).json<Message[]>();
}