chore: interface and type

This commit is contained in:
Methapon2001 2023-12-11 15:51:27 +07:00
parent 8f716e6f44
commit 4bdf1f620b
No known key found for this signature in database
GPG key ID: 849924FEF46BD132
2 changed files with 46 additions and 150 deletions

View file

@ -1,109 +0,0 @@
import { defineStore } from 'pinia'
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()
export const useSocketStore = defineStore('socket', () => {
const socket = io('http://localhost:25570')
socket.on('connect', () => {
console.log('SocketIO Connected')
})
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: [],
},
)
if (
currentFolder.value.findIndex(
(v) => v.name === pathArray[pathArray.length - 1],
) === -1
) {
currentFolder.value.push({
pathname: pathname,
name: pathArray[pathArray.length - 1],
status: true,
folder: [],
file: [],
})
}
currentFolder.value.sort((a, b) => {
return a.name.localeCompare(b.name)
})
}
})
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', () => {
console.log('SocketIO Disconnected')
})
})

View file

@ -7,6 +7,38 @@ import api from '@/services/HttpService'
import { useLoader } from './loader'
type Path = string
export interface StorageFolder {
pathname: string
name: string
createdAt: string
createdBy: string
}
export interface StorageFile {
pathname: string
path: string
fileName: string
fileSize: string
fileType: string
title: string
description: string
category: string[]
keyword: string[]
updatedAt: string
updatedBy: string
createdAt: string
createdBy: string
}
export interface Structure extends StorageFolder {
folder: Structure[]
file: StorageFile[]
}
type Tree = Structure[]
function constructUrl(path: string | string[], append = true) {
const arr = Array.isArray(path) ? path : path.split('/').filter(Boolean)
const url =
@ -33,60 +65,29 @@ function constructUrl(path: string | string[], append = true) {
const useStorage = defineStore('storageStore', () => {
const loader = useLoader()
const init = ref<boolean>(false)
const folder = ref<
Record<
string, // path that contains folders
{
pathname: string
name: string
}[]
>
>({})
const file = ref<
Record<
string, // path that contains files
{
pathname: string
path: string
fileName: string
fileSize: string
fileType: string
title: string
description: string
category: string[]
keyword: string[]
updatedAt: string
updatedBy: string
createdAt: string
createdBy: string
}[]
>
>({})
const folder = ref<Record<Path, StorageFolder[]>>({})
const file = ref<Record<Path, StorageFile[]>>({})
const tree = computed(() => {
type Structure = {
pathname: string
name: string
folder: Structure
file: (typeof file.value)[string]
}[]
let structure: Structure = []
let structure: Tree = []
// parse list of folder and list of file into tree
Object.entries(folder.value).forEach(([key, value]) => {
const arr = key.split('/').filter(Boolean)
// init outer tree
// Once run then it is init
if (!init.value) init.value = true
if (arr.length === 0) {
if (!init.value) init.value = true
structure = value.map((v) => ({
pathname: v.pathname,
name: v.name,
createdAt: v.createdAt,
createdBy: v.createdBy,
folder: [],
file: [],
}))
} else {
let current: Structure[number] | undefined
let current: Structure | undefined
// traverse into tree
arr.forEach((v, i) => {
@ -101,6 +102,8 @@ const useStorage = defineStore('storageStore', () => {
current.folder = value.map((v) => ({
pathname: v.pathname,
name: v.name,
createdAt: v.createdAt,
createdBy: v.createdBy,
folder: [],
file: [],
}))
@ -116,7 +119,7 @@ const useStorage = defineStore('storageStore', () => {
dept: number
}>({
path: '',
dept: 1,
dept: 0,
})
if (!init.value) goto(sessionStorage.getItem('path') || '')
@ -186,6 +189,8 @@ const useStorage = defineStore('storageStore', () => {
folder.value[path].push({
pathname: data.pathname,
name: arr[arr.length - 1],
createdAt: 'n/a',
createdBy: 'n/a',
})
folder.value[path].sort((a, b) => a.pathname.localeCompare(b.pathname))
}