90 lines
2.1 KiB
TypeScript
90 lines
2.1 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import { ref } from 'vue'
|
|
import {
|
|
type CreateIssueInput,
|
|
type Issue,
|
|
itemCreate,
|
|
itemDelete,
|
|
itemGetByKey,
|
|
itemsList,
|
|
itemStat,
|
|
itemUpdate,
|
|
type IssueStat,
|
|
type Meta,
|
|
type UpdateIssueInput,
|
|
} from '@/api/issue.api.ts'
|
|
|
|
export const useIssueStore = defineStore('item', () => {
|
|
const issues = ref<Issue[]>([])
|
|
const stats = ref<IssueStat>()
|
|
const error = ref<string | null>(null)
|
|
const lastMeta = ref<Meta>({
|
|
page: 1,
|
|
totalCount: 0,
|
|
totalPages: 0,
|
|
})
|
|
|
|
async function list(params: {
|
|
page: number
|
|
limit: number
|
|
text?: string | null
|
|
status?: string | string[] | null
|
|
assigneeId?: string | null
|
|
projectId?: string | null
|
|
}) {
|
|
try {
|
|
const res = await itemsList(params)
|
|
issues.value = res.list
|
|
return res
|
|
} catch (e) {
|
|
error.value = e instanceof Error ? e.message : 'error get list of items'
|
|
return { list: [], meta: lastMeta.value }
|
|
}
|
|
}
|
|
|
|
async function getByKey(ckey: string) {
|
|
try {
|
|
return await itemGetByKey(ckey)
|
|
} catch (e) {
|
|
error.value = e instanceof Error ? e.message : 'error get item by key'
|
|
}
|
|
}
|
|
|
|
async function create(input: CreateIssueInput) {
|
|
try {
|
|
return await itemCreate(input)
|
|
} catch (e) {
|
|
error.value = e instanceof Error ? e.message : 'error creating issue'
|
|
}
|
|
}
|
|
|
|
async function update(id: number, input: UpdateIssueInput) {
|
|
try {
|
|
return await itemUpdate(id, input)
|
|
} catch (e) {
|
|
error.value = e instanceof Error ? e.message : 'error updating issue'
|
|
}
|
|
}
|
|
|
|
async function remove(id: number) {
|
|
try {
|
|
return await itemDelete(id)
|
|
} catch (e) {
|
|
error.value = e instanceof Error ? e.message : 'error deleting issue'
|
|
return false
|
|
}
|
|
}
|
|
|
|
async function getStat(projectId?: string | null): Promise<IssueStat | undefined> {
|
|
try {
|
|
stats.value = await itemStat(projectId ?? undefined)
|
|
|
|
return stats.value
|
|
} catch (e) {
|
|
error.value = e instanceof Error ? e.message : 'error getting issue stat'
|
|
}
|
|
}
|
|
|
|
return { stats, list, getByKey, create, update, remove, getStat }
|
|
})
|