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

@ -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))
}