hrms-edm/Services/client/src/stores/socket.ts
2023-12-08 22:42:01 +07:00

108 lines
2.9 KiB
TypeScript

import { defineStore } from 'pinia'
import { reactive } from 'vue'
import { useFileInfoStore } from '@/stores/file-info-data'
import { io } from 'socket.io-client'
import { useTreeDataStore } from '@/stores/tree-data'
import { storeToRefs } from 'pinia'
const {
data,
listDataFolder,
currentFolder,
currentPath,
currentFile,
listDataFile,
} = storeToRefs(useTreeDataStore())
const {
updateEditFolder,
updateDeleteFolder,
updateCreateFolder,
updateDeleteFile,
updateNewFile,
} = useTreeDataStore()
const { fileInfo } = storeToRefs(useFileInfoStore())
export const state = reactive({
connected: false,
})
export const useSocketStore = defineStore('socket', () => {
const socket = io('http://localhost:25570')
socket.on('connect', () => {
state.connected = true
})
socket.on('CreateFolder', (dataSocket) => {
const { pathname } = dataSocket
const pathArray: string[] = pathname.split('/').filter(Boolean)
const currentPathResult = pathArray.slice(0, -1).join('/') + '/'
if (currentPath.value == currentPathResult) {
data.value = updateCreateFolder(
data.value,
pathArray.length,
currentPathResult,
pathname,
{
pathname: pathname,
name: pathArray[pathArray.length - 1],
status: true,
folder: [],
file: [],
},
)
currentFolder.value.push({
pathname: pathname,
name: pathArray[pathArray.length - 1],
status: true,
folder: [],
file: [],
})
listDataFolder.value = currentFolder.value
}
})
socket.on('EditFolder', (dataSocket) => {
const { from, to } = dataSocket
data.value = updateEditFolder(data.value, from, to)
currentFolder.value = updateEditFolder(currentFolder.value, from, to)
listDataFolder.value = updateEditFolder(listDataFolder.value, from, to)
})
socket.on('DeleteFolder', (dataSocket) => {
const { pathname } = dataSocket
data.value = updateDeleteFolder(data.value, pathname)
currentFolder.value = updateDeleteFolder(currentFolder.value, pathname)
listDataFolder.value = updateDeleteFolder(listDataFolder.value, pathname)
})
socket?.on('FileDelete', (dataSocket) => {
const { pathname } = dataSocket
currentFile.value = updateDeleteFile(currentFile.value, pathname)
listDataFile.value = updateDeleteFile(listDataFile.value, pathname)
})
socket.on('FileUpdate', (dataSocket) => {
const metadata = dataSocket
const pathArray: string[] = metadata.pathname.split('/').filter(Boolean)
const currentPathResult = pathArray.slice(0, -1).join('/') + '/'
if (currentPath.value == currentPathResult) {
listDataFile.value = currentFile.value = updateNewFile(
currentFile.value,
metadata.pathname,
metadata,
)
}
})
socket.on('disconnect', () => {
state.connected = false
})
})