diff --git a/Services/client/src/stores/tree-data.ts b/Services/client/src/stores/tree-data.ts deleted file mode 100644 index 3f8e624..0000000 --- a/Services/client/src/stores/tree-data.ts +++ /dev/null @@ -1,535 +0,0 @@ -import { ref } from 'vue' -import { defineStore } from 'pinia' -import { useLoader } from '@/stores/loader' -import axiosClient from '@/services/HttpService' - -const apiEndpoint: string = import.meta.env.VITE_API_ENDPOINT - -export interface EhrFolder { - pathname: string - name: string - createdAt: string - createdBy: string -} - -export interface EhrFile { - pathname: 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 TreeDataFolder { - pathname: string - name: string - status: boolean - folder: TreeDataFolder[] - file?: EhrFile[] -} - -export const useTreeDataStore = defineStore('changeCabinet', () => { - const loader = useLoader() - const data = ref([]) - const currentFolder = ref([]) - const currentFile = ref([]) - const currentPath = ref('') - const currentDept = ref(0) - const listDataFolder = ref([]) - const listDataFile = ref([]) - async function getCabinet() { - const res = await axiosClient.get(`${apiEndpoint}cabinet`) - - data.value = currentFolder.value = res.data.map((v) => ({ - ...v, - status: false, - folder: [], - })) - - listDataFolder.value = data.value - } - - async function getFolder(pathname: string, updateStatus = true) { - loader.show() - - sessionStorage.setItem('currentPath', pathname) - - const pathArray: string[] = pathname.split('/').filter(Boolean) - - currentDept.value = pathArray.length - if (pathArray.length >= 4) { - currentFolder.value = [] - - await getFile(pathname) - - let current: (typeof data.value)[number] | undefined - current = data.value.find((v) => v.name === pathArray[0]) - - if (current && updateStatus) current.status = true - current = current?.folder.find((v) => v.name === pathArray[1]) - if (current && updateStatus) current.status = true - current = current?.folder.find((v) => v.name === pathArray[2]) - if (current && updateStatus) current.status = true - current = current?.folder.find((v) => v.name === pathArray[3]) - if (current && updateStatus) current.status = true - - currentPath.value = pathname - return loader.hide() - } - - let requestPath = 'cabinet' - if (pathArray.length >= 1) requestPath += `/${pathArray[0]}/drawer` - if (pathArray.length >= 2) requestPath += `/${pathArray[1]}/folder` - if (pathArray.length >= 3) requestPath += `/${pathArray[2]}/subfolder` - - currentPath.value = pathname - - const res = await axiosClient.get( - `${apiEndpoint}${requestPath}`, - ) - - const list = res.data.map((v) => ({ - ...v, - status: false, - folder: [], - })) - - let current: (typeof data.value)[number] | undefined - - if (pathArray.length >= 1) { - current = data.value.find((v) => v.name === pathArray[0]) - if (current && updateStatus) { - current.status = true - } - } - - if (pathArray.length >= 2) { - current = current?.folder.find((v) => v.name === pathArray[1]) - if (current && updateStatus) { - current.status = true - } - } - if (pathArray.length >= 3) { - current = current?.folder.find((v) => v.name === pathArray[2]) - if (current && updateStatus) { - current.status = true - } - } - - if (current) current.folder = list - - currentFolder.value = list - - await getFile(pathname) - - listDataFolder.value = currentFolder.value - - return loader.hide() - } - - async function getFile(pathname: string) { - loader.show() - - const pathArray: string[] = pathname.split('/').filter(Boolean) - - if (pathArray.length <= 2) { - currentFile.value = [] - return loader.hide() - } - - let requestPath = `cabinet/${pathArray[0]}/drawer/${pathArray[1]}` - if (pathArray.length >= 3) requestPath += `/folder/${pathArray[2]}` - if (pathArray.length >= 4) requestPath += `/subfolder/${pathArray[3]}` - requestPath += '/file' - - const res = await axiosClient.get(`${apiEndpoint}${requestPath}`) - - currentFile.value = res.data - - listDataFile.value = currentFile.value - - return loader.hide() - } - - async function createFolder(name: string | undefined) { - loader.show() - - if (!name) return loader.hide() - - const pathArray: string[] = currentPath.value.split('/').filter(Boolean) - let requestPath = 'cabinet' - if (pathArray.length >= 1) requestPath += `/${pathArray[0]}/drawer` - if (pathArray.length >= 2) requestPath += `/${pathArray[1]}/folder` - if (pathArray.length >= 3) requestPath += `/${pathArray[2]}/subfolder` - - await axiosClient.post(`${apiEndpoint}${requestPath}`, { - name, - }) - - if (currentDept.value === 0) await getCabinet() - else await getFolder(currentPath.value) - - return loader.hide() - } - - async function editFolder(name: string | undefined, pathname: string) { - loader.show() - - if (!name) return loader.hide() - - const pathArray: string[] = pathname.split('/').filter(Boolean) - - let requestPath = 'cabinet' - if (pathArray.length >= 1) requestPath += `/${pathArray[0]}` - if (pathArray.length >= 2) requestPath += `/drawer/${pathArray[1]}` - if (pathArray.length >= 3) requestPath += `/folder/${pathArray[2]}` - if (pathArray.length >= 4) requestPath += `/subfolder/${pathArray[3]}` - - await axiosClient.put(`${apiEndpoint}${requestPath}`, { - name: name, - }) - - if (currentDept.value === 0) await getCabinet() - else await getFolder(currentPath.value) - - return loader.hide() - } - - async function deleteFolder(pathname: string) { - loader.show() - - const pathArray: string[] = pathname.split('/').filter(Boolean) - - let requestPath = 'cabinet' - if (pathArray.length >= 1) requestPath += `/${pathArray[0]}` - if (pathArray.length >= 2) requestPath += `/drawer/${pathArray[1]}` - if (pathArray.length >= 3) requestPath += `/folder/${pathArray[2]}` - if (pathArray.length >= 4) requestPath += `/subfolder/${pathArray[3]}` - - await axiosClient.delete(`${apiEndpoint}${requestPath}`) - - if (currentDept.value === 0) await getCabinet() - else await getFolder(currentPath.value) - - return loader.hide() - } - - async function gotoParent() { - const pathname = currentPath.value.split('/').filter(Boolean) - - pathname.pop() - - if (pathname.length === 0) { - const cabinet = currentFolder.value.find((v) => v.status === true) - if (cabinet) { - cabinet.status = false - } - } - - await getFolder(pathname.join('/') + '/') - } - - async function uploadFile( - pathname: string, - file: File, - newData: { - title: string - description: string - keyword: string[] - category: string[] - }, - ) { - loader.show() - - const pathArray: string[] = pathname.split('/').filter(Boolean) - - if (pathArray.length <= 2) return - - let requestPath = 'cabinet' - if (pathArray.length >= 1) requestPath += `/${pathArray[0]}` - if (pathArray.length >= 2) requestPath += `/drawer/${pathArray[1]}` - if (pathArray.length >= 3) requestPath += `/folder/${pathArray[2]}` - if (pathArray.length >= 4) requestPath += `/subfolder/${pathArray[3]}` - requestPath += '/file' - - const res = await axiosClient.post( - `${apiEndpoint}${requestPath}`, - { - file: file.name, - ...newData, - }, - ) - - if (res && res.data.upload) { - await fetch(res.data.upload, { - method: 'PUT', - body: file, - }) - - loader.hide() - } - - if (currentDept.value === 0) { - await getCabinet() - } else await getFolder(currentPath.value) - - await getFile(currentPath.value) - - return loader.hide() - } - - async function updateFile( - pathname: string, - newData: { - title: string - description: string - keyword: string[] - category: string[] - }, - file?: File, - ) { - loader.show() - - const pathArray: string[] = pathname.split('/').filter(Boolean) - - if (pathArray.length < 4) return loader.hide() - - let requestPath = `cabinet/${pathArray[0]}/drawer/${pathArray[1]}` - if (pathArray.length >= 4) requestPath += `/folder/${pathArray[2]}` - if (pathArray.length >= 5) requestPath += `/subfolder/${pathArray[3]}` - requestPath += `/file/${pathArray.at(-1)}` - - const res = await axiosClient.patch<{ upload: string }>( - `${apiEndpoint}${requestPath}`, - { file: file?.name, ...newData }, - ) - - if (res && res.data.upload) { - await fetch(res.data.upload, { - method: 'PUT', - body: file, - }) - - loader.hide() - } - - if (currentDept.value === 0) { - await getCabinet() - } else await getFolder(currentPath.value) - - await getFile(currentPath.value) - - return loader.hide() - } - - async function deleteFile(pathname: string) { - loader.show() - - const pathArray: string[] = pathname.split('/').filter(Boolean) - - if (pathArray.length < 4) return loader.hide() - - let requestPath = `cabinet/${pathArray[0]}/drawer/${pathArray[1]}` - if (pathArray.length >= 4) requestPath += `/folder/${pathArray[2]}` - if (pathArray.length >= 5) requestPath += `/subfolder/${pathArray[3]}` - requestPath += `/file/${pathArray.at(-1)}` - - await axiosClient.delete(`${apiEndpoint}${requestPath}`) - - if (currentDept.value === 0) { - await getCabinet() - } else await getFolder(currentPath.value) - - await getFile(currentPath.value) - - currentFile.value = currentFile.value.filter((v) => v.pathname !== pathname) - listDataFile.value = currentFile.value - return loader.hide() - } - - function checkFile(fileName: string) { - return currentFile.value.some((element) => element.fileName === fileName) - } - - function checkFileName(fileName: string) { - return fileName.length >= 85 - } - - async function refaceFile(pathname: string) { - const pathArray: string[] = pathname.split('/').filter(Boolean) - - if (pathArray.length <= 2) { - currentFile.value = [] - return loader.hide() - } - - let requestPath = `cabinet/${pathArray[0]}/drawer/${pathArray[1]}` - if (pathArray.length >= 3) requestPath += `/folder/${pathArray[2]}` - if (pathArray.length >= 4) requestPath += `/subfolder/${pathArray[3]}` - requestPath += '/file' - - const res = await axiosClient.get(`${apiEndpoint}${requestPath}`) - - currentFile.value = res.data - - listDataFile.value = currentFile.value - } - - function updateEditFolder( - data: TreeDataFolder[], - targetPathname: string, - - newPath: string, - ): TreeDataFolder[] { - return data.map((item) => { - if (item.pathname === targetPathname) { - const pathArray: string[] = newPath.split('/').filter(Boolean) - item.pathname = newPath - item.name = pathArray[pathArray.length - 1] - } - - if (item.folder) { - return { - ...item, - folder: updateEditFolder(item.folder, targetPathname, newPath), - } - } - - return item - }) - } - - function updateDeleteFolder( - data: TreeDataFolder[], - targetPathname: string, - ): TreeDataFolder[] { - return data - .filter((item) => item.pathname !== targetPathname) - .map((item) => { - if (item.folder) { - item.folder = updateDeleteFolder(item.folder, targetPathname) - if (item.folder.length === 0) item.folder = [] - } - - return item - }) - } - - function updateCreateFolder( - data: TreeDataFolder[], - pathArrayLength: number, - pathfolder: string, - targetPathname: string, - newData: TreeDataFolder, - ): TreeDataFolder[] { - let updatedData: TreeDataFolder[] = [] - - if (pathArrayLength === 1) { - updatedData.push(newData) - } - - for (const item of data) { - if (item.pathname === pathfolder) { - updatedData.push({ - ...item, - folder: [...(item.folder || []), newData], - }) - } else { - updatedData.push({ - ...item, - folder: item.folder - ? updateCreateFolder( - item.folder, - pathArrayLength, - pathfolder, - targetPathname, - newData, - ) - : [], - }) - } - } - - return updatedData - } - - function updateDeleteFile( - data: EhrFile[], - targetPathname: string, - ): EhrFile[] { - return data.filter((item) => item.pathname !== targetPathname) - } - - function updateNewFile( - data: EhrFile[], - targetPathname: string, - newData: EhrFile, - ): EhrFile[] { - let isUpdated = false - - const updatedData = data.map((item) => { - if (item.pathname === targetPathname) { - isUpdated = true - - return { - ...item, - pathname: newData.pathname, - fileName: newData.fileName, - fileSize: newData.fileSize, - fileType: newData.fileType, - title: newData.title, - description: newData.description, - category: newData.category, - keyword: newData.keyword, - updatedAt: newData.updatedAt, - updatedBy: newData.updatedBy, - createdAt: newData.createdAt, - createdBy: newData.createdBy, - } - } - return item - }) - if (!isUpdated) { - updatedData.push(newData) - } - - return updatedData - } - - return { - data, - currentFolder, - currentFile, - currentDept, - currentPath, - listDataFile, - listDataFolder, - getCabinet, - getFolder, - uploadFile, - updateFile, - deleteFile, - gotoParent, - createFolder, - deleteFolder, - editFolder, - checkFile, - checkFileName, - refaceFile, - updateEditFolder, - updateDeleteFolder, - updateCreateFolder, - updateDeleteFile, - updateNewFile, - } -})