58 lines
1.5 KiB
TypeScript
58 lines
1.5 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import { ref } from 'vue'
|
|
import { invoke } from '@tauri-apps/api/core'
|
|
|
|
export interface Favorite {
|
|
id?: number
|
|
project_id: number
|
|
task_id?: number | null
|
|
description?: string | null
|
|
sort_order: number
|
|
}
|
|
|
|
export const useFavoritesStore = defineStore('favorites', () => {
|
|
const favorites = ref<Favorite[]>([])
|
|
|
|
async function fetchFavorites() {
|
|
try {
|
|
favorites.value = await invoke<Favorite[]>('get_favorites')
|
|
} catch (error) {
|
|
console.error('Failed to fetch favorites:', error)
|
|
}
|
|
}
|
|
|
|
async function createFavorite(fav: Favorite): Promise<number | null> {
|
|
try {
|
|
const id = await invoke<number>('create_favorite', { fav })
|
|
favorites.value.push({ ...fav, id: Number(id) })
|
|
return Number(id)
|
|
} catch (error) {
|
|
console.error('Failed to create favorite:', error)
|
|
return null
|
|
}
|
|
}
|
|
|
|
async function deleteFavorite(id: number): Promise<boolean> {
|
|
try {
|
|
await invoke('delete_favorite', { id })
|
|
favorites.value = favorites.value.filter(f => f.id !== id)
|
|
return true
|
|
} catch (error) {
|
|
console.error('Failed to delete favorite:', error)
|
|
return false
|
|
}
|
|
}
|
|
|
|
async function reorderFavorites(ids: number[]): Promise<boolean> {
|
|
try {
|
|
await invoke('reorder_favorites', { ids })
|
|
return true
|
|
} catch (error) {
|
|
console.error('Failed to reorder favorites:', error)
|
|
return false
|
|
}
|
|
}
|
|
|
|
return { favorites, fetchFavorites, createFavorite, deleteFavorite, reorderFavorites }
|
|
})
|