From 081c1e0589935b651c5cdff75b14733c3ef856ef Mon Sep 17 00:00:00 2001 From: net Date: Fri, 16 Feb 2024 16:20:18 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=80=E0=B8=9E=E0=B8=B4=E0=B9=88=E0=B8=A1?= =?UTF-8?q?=20=20Socket=20mark-read=20,=20notify-message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/00_support/api.support.ts | 4 + .../00_support/components/FormChat.vue | 115 +++++++++--------- .../00_support/components/MessageChat.vue | 62 ++++++++-- .../00_support/interface/index/Main.ts | 18 ++- src/modules/00_support/store/Main.ts | 59 +++++++-- src/modules/00_support/views/MainPage.vue | 11 ++ 6 files changed, 191 insertions(+), 78 deletions(-) diff --git a/src/api/00_support/api.support.ts b/src/api/00_support/api.support.ts index c015f43d1..3a7d1bac0 100644 --- a/src/api/00_support/api.support.ts +++ b/src/api/00_support/api.support.ts @@ -4,7 +4,11 @@ export const supportIssue = `${env.API_SUPPORT_URI}/issue`; export const supportMessage = (id: string) => `${env.API_SUPPORT_URI}/issue/${id}/message`; +export const supportMessageStatus = (id: string) => + `${env.API_SUPPORT_URI}/message-status?issueId=${id}`; + export default { supportIssue, supportMessage, + supportMessageStatus, }; diff --git a/src/modules/00_support/components/FormChat.vue b/src/modules/00_support/components/FormChat.vue index 8112648a1..b95f61ba8 100644 --- a/src/modules/00_support/components/FormChat.vue +++ b/src/modules/00_support/components/FormChat.vue @@ -4,10 +4,9 @@ import { useSupportStore } from "@/modules/00_support/store/Main.ts"; import MessageChat from "@/modules/00_support/components/MessageChat.vue"; const store = useSupportStore(); - const content = ref(""); const scrollContainerRef = ref(); -const readStatus = ref(false); +const readStatus = ref(true); onUpdated(() => { nextTick(() => { @@ -43,75 +42,77 @@ onMounted(async () => { - {{ currentTitle }} - + {{ store.currentTitle }} - -
- -
- + +
+ - - - + store.fetchMessageStatus(data.id); + store.issue.result = store.issue.result.map((v) => { + if (v.id === data.id) { + v.unreadCount = 0; + } + return v; + }); + " + active-class="my-menu-link" + > + + + - - {{ data.title }} + + {{ data.title }} - - {{ data.lastMessage }} - - - - - - - - - -
-
+ + {{ data.lastMessage }} + + +
+ + +
+
+ +
+ +
+
+
- - +
+ +
+
@@ -166,7 +167,7 @@ onMounted(async () => { grid-template-areas: "search toolbar" "menu chat" - "menu input"; + "manage input"; grid-template-rows: 60px 400px 60px; grid-template-columns: 400px 1fr; } @@ -206,6 +207,10 @@ onMounted(async () => { .i5 { grid-area: input; } + +.grid-manage { + grid-area: manage; +} .container-1 { display: grid; /* grid-template-columns: 1fr 2fr; */ diff --git a/src/modules/00_support/components/MessageChat.vue b/src/modules/00_support/components/MessageChat.vue index 5ffdd19d9..8e0dcbce9 100644 --- a/src/modules/00_support/components/MessageChat.vue +++ b/src/modules/00_support/components/MessageChat.vue @@ -7,21 +7,59 @@ import { useSupportStore } from "@/modules/00_support/store/Main"; const store = useSupportStore(); onMounted(() => {}); + +function onLoad() { + console.log("ทำงานเเล้ว"); +} diff --git a/src/modules/00_support/interface/index/Main.ts b/src/modules/00_support/interface/index/Main.ts index 637dcd81a..7b223125d 100644 --- a/src/modules/00_support/interface/index/Main.ts +++ b/src/modules/00_support/interface/index/Main.ts @@ -1,3 +1,14 @@ +export interface SupportMessageStatus { + result: SupportResult[]; +} + +export interface SupportResult { + fromUserId: string; + fromUserName: string; + lastAccessDate: string; + issueId: string; +} + export interface SupportStatusUser { socketId: string; userId: string; @@ -59,4 +70,9 @@ export interface SupportIssueMessage { issueId: string; } -export type { SupportMessageResponse, SupportIssueResponse, SupportStatusUser }; +export type { + SupportMessageStatus, + SupportMessageResponse, + SupportIssueResponse, + SupportStatusUser, +}; diff --git a/src/modules/00_support/store/Main.ts b/src/modules/00_support/store/Main.ts index 70222ec8c..251652bdf 100644 --- a/src/modules/00_support/store/Main.ts +++ b/src/modules/00_support/store/Main.ts @@ -1,5 +1,5 @@ import { defineStore } from "pinia"; -import { ref } from "vue"; +import { ref, onMounted } from "vue"; import { io } from "socket.io-client"; import http from "@/plugins/http"; import config from "@/app.config"; @@ -17,6 +17,7 @@ export const useSupportStore = defineStore("supportServiceStore", () => { const userId = ref(keycloak.subject); const issue = ref(); const message = ref(); + const messageStatus = ref(); const statusUser = ref([]); const currentIssue = ref(""); const currentTitle = ref(""); @@ -24,32 +25,56 @@ export const useSupportStore = defineStore("supportServiceStore", () => { const scrollContainer = ref(); function scrollToEnd() { - scrollContainer.value.setScrollPosition("vertical", 10000); + scrollContainer.value.setScrollPosition("vertical", 1000000); } const socket = io("http://192.168.1.10:3000/", { auth: { token: keycloak.token }, + autoConnect: false, }); - socket.on("users", (users) => { - // console.log(users); + socket.on("users", (data: SupportStatusUser[]) => { + statusUser.value = data; }); socket.on("online", (r) => { - console.log(r); statusUser.value.push({ socketId: r.socketId, userId: r.userId, name: r.name, role: r.role, }); + }); - console.log(statusUser.value); + socket.on("offline", (socketId: string) => { + if (socketId === socket.id) return; + statusUser.value = statusUser.value.filter((v) => v.socketId !== socketId); + }); - // console.log(JSON.stringify(r, null, -2)); + socket.on("notify-message", (r) => { + issue.value.result = issue.value.result.map((v) => { + if (v.id === r.issueId) { + v.unreadCount++; + v.lastMessage = r.content; + } + return v; + }); + }); + + socket.on("read", (r) => { + setTimeout(() => { + messageStatus.value.result = messageStatus.value.result.map((v) => { + if (v.issueId === r.issueId) { + v.lastAccessDate = r.lastAccessDate; + } + return v; + }); + }, 100); }); socket.on("message", (r) => { + console.log(r); + message.value.result.message.push({ id: r.id, fromUserId: r.fromUserId, @@ -60,17 +85,29 @@ export const useSupportStore = defineStore("supportServiceStore", () => { read: r.read, issueId: r.issueId, }); - socket.emit("mark-read", { currentIssue }); + socket.emit("mark-read", { issueId: currentIssue.value }); + scrollToEnd(); }); function sendMessage(content: string, to: string) { socket.emit("message", { to, content }); - setTimeout(() => { scrollToEnd(); }, 100); } + async function fetchMessageStatus(issueId: string) { + const res = await http + .get(config.API.supportMessageStatus(issueId)) + .catch((err) => { + messageError($q, err); + }) + .finally(() => {}); + if (res && res.data) { + messageStatus.value = res.data; + } + } + async function fetchMessage(issueId: string) { showLoader(); const res = await http @@ -106,7 +143,6 @@ export const useSupportStore = defineStore("supportServiceStore", () => { if (res && res.data) { issue.value = res.data; - // console.log(JSON.stringify(res.data, null, -2)); } } @@ -116,11 +152,14 @@ export const useSupportStore = defineStore("supportServiceStore", () => { message, fetchIssue, fetchMessage, + fetchMessageStatus, sendMessage, items, scrollToEnd, scrollContainer, currentIssue, currentTitle, + socket, + messageStatus, }; }); diff --git a/src/modules/00_support/views/MainPage.vue b/src/modules/00_support/views/MainPage.vue index 766ed6113..98f291642 100644 --- a/src/modules/00_support/views/MainPage.vue +++ b/src/modules/00_support/views/MainPage.vue @@ -1,5 +1,16 @@