60 lines
1.6 KiB
TypeScript
60 lines
1.6 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import { ref } from 'vue'
|
|
import type { Project } from '@/shared/types/api'
|
|
import * as projectApi from '../api'
|
|
|
|
export const useProjectStore = defineStore('project', () => {
|
|
const projects = ref<Project[]>([])
|
|
const currentProject = ref<Project | null>(null)
|
|
const loading = ref(false)
|
|
const error = ref<string | null>(null)
|
|
|
|
async function fetchProjects() {
|
|
loading.value = true
|
|
error.value = null
|
|
try {
|
|
projects.value = await projectApi.listProjects()
|
|
} catch (e: any) {
|
|
error.value = e.message
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
async function createProject(name: string, designDir: string, codeDir?: string) {
|
|
loading.value = true
|
|
error.value = null
|
|
try {
|
|
const project = await projectApi.createProject(name, designDir, codeDir)
|
|
projects.value.push(project)
|
|
return project
|
|
} catch (e: any) {
|
|
error.value = e.response?.data?.detail || e.message
|
|
throw e
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
async function selectProject(id: string) {
|
|
loading.value = true
|
|
try {
|
|
currentProject.value = await projectApi.getProject(id)
|
|
} catch (e: any) {
|
|
error.value = e.message
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
async function removeProject(id: string) {
|
|
await projectApi.deleteProject(id)
|
|
projects.value = projects.value.filter(p => p.id !== id)
|
|
if (currentProject.value?.id === id) {
|
|
currentProject.value = null
|
|
}
|
|
}
|
|
|
|
return { projects, currentProject, loading, error, fetchProjects, createProject, selectProject, removeProject }
|
|
})
|