Files
tgCrewUser/src/stores/files.ts
2025-06-26 11:06:48 +03:00

54 lines
1.3 KiB
TypeScript

import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { api } from 'boot/axios'
import { useProjectsStore } from 'stores/projects'
import type { FileLink } from 'types/FileLink'
export const useFilesStore = defineStore('files', () => {
const files = ref<FileLink[]>([])
const isInit = ref<boolean>(false)
const projectsStore = useProjectsStore()
const currentProjectId = computed(() => projectsStore.currentProjectId)
async function init () {
const { data } = await api.get('/project/' + currentProjectId.value + '/file')
const filesAPI = data.data
files.value.push(...filesAPI)
isInit.value = true
}
function reset () {
files.value = []
isInit.value = false
}
async function fileUrl (fileId: number) {
const response = api.get('/project/' + currentProjectId.value + '/file/' + fileId)
return (await response).data.data
}
async function remove (fileId: number) {
const response = api.delete('/project/' + currentProjectId.value + '/file/' + fileId)
return (await response).data.data
}
const getFiles = computed(() => files.value)
function fileById (id: number) {
return files.value.find(el =>el.id === id)
}
return {
files,
isInit,
init,
reset,
fileUrl,
remove,
getFiles,
fileById
}
})