chore: interface and type
This commit is contained in:
parent
8f716e6f44
commit
4bdf1f620b
2 changed files with 46 additions and 150 deletions
|
|
@ -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))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue