เพิ่ม socket แจ้งเตือน backup
This commit is contained in:
parent
55756ef9c3
commit
ebae0d9170
4 changed files with 67 additions and 0 deletions
48
src/stores/socket.ts
Normal file
48
src/stores/socket.ts
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import { defineStore } from "pinia";
|
||||
import { Notify } from "quasar";
|
||||
|
||||
import { io, Socket } from "socket.io-client";
|
||||
|
||||
import config from "@/app.config";
|
||||
import { getToken } from "@/plugins/auth";
|
||||
|
||||
interface sockeBackup {
|
||||
message: string;
|
||||
success: boolean;
|
||||
}
|
||||
|
||||
export const useSocketStore = defineStore("socket", () => {
|
||||
let socket: Socket;
|
||||
|
||||
async function init() {
|
||||
socket = io(new URL(config.API.socket).origin, {
|
||||
auth: { token: await getToken() },
|
||||
path: "/api/v1/backup-socket",
|
||||
});
|
||||
socket.on("backup-notification", (payload) => {
|
||||
let body: sockeBackup = JSON.parse(payload);
|
||||
notifyStatus(body.message, body.success);
|
||||
});
|
||||
}
|
||||
|
||||
function notifyStatus(message: string, success: boolean) {
|
||||
Notify.create({
|
||||
group: false,
|
||||
type: success ? "positive" : "negative",
|
||||
message: `${message}`,
|
||||
position: "top",
|
||||
timeout: 0,
|
||||
actions: [
|
||||
{
|
||||
icon: "close",
|
||||
color: "white",
|
||||
round: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
init();
|
||||
|
||||
return {};
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue