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([]) const isInit = ref(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 } })