From 4bdf1f620b01bd6a75d2ccdcaca9d4985a7c2c6f Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Mon, 11 Dec 2023 15:51:27 +0700 Subject: [PATCH] chore: interface and type --- Services/client/src/stores/socket.ts | 109 -------------------------- Services/client/src/stores/storage.ts | 87 ++++++++++---------- 2 files changed, 46 insertions(+), 150 deletions(-) delete mode 100644 Services/client/src/stores/socket.ts diff --git a/Services/client/src/stores/socket.ts b/Services/client/src/stores/socket.ts deleted file mode 100644 index 68e580b..0000000 --- a/Services/client/src/stores/socket.ts +++ /dev/null @@ -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') - }) -}) diff --git a/Services/client/src/stores/storage.ts b/Services/client/src/stores/storage.ts index 4921ece..a2f1e76 100644 --- a/Services/client/src/stores/storage.ts +++ b/Services/client/src/stores/storage.ts @@ -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(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>({}) + const file = ref>({}) 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)) }