77 lines
1.8 KiB
TypeScript
77 lines
1.8 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import { ref } from 'vue'
|
|
import { invoke } from '@tauri-apps/api/core'
|
|
|
|
export interface Client {
|
|
id?: number
|
|
name: string
|
|
email?: string
|
|
address?: string
|
|
company?: string
|
|
phone?: string
|
|
tax_id?: string
|
|
payment_terms?: string
|
|
notes?: string
|
|
}
|
|
|
|
export const useClientsStore = defineStore('clients', () => {
|
|
const clients = ref<Client[]>([])
|
|
const loading = ref(false)
|
|
|
|
async function fetchClients() {
|
|
loading.value = true
|
|
try {
|
|
clients.value = await invoke<Client[]>('get_clients')
|
|
} catch (error) {
|
|
console.error('Failed to fetch clients:', error)
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
async function createClient(client: Client): Promise<number | null> {
|
|
try {
|
|
const id = await invoke<number>('create_client', { client })
|
|
clients.value.push({ ...client, id: Number(id) })
|
|
return Number(id)
|
|
} catch (error) {
|
|
console.error('Failed to create client:', error)
|
|
return null
|
|
}
|
|
}
|
|
|
|
async function updateClient(client: Client): Promise<boolean> {
|
|
try {
|
|
await invoke('update_client', { client })
|
|
const index = clients.value.findIndex(c => c.id === client.id)
|
|
if (index !== -1) {
|
|
clients.value[index] = client
|
|
}
|
|
return true
|
|
} catch (error) {
|
|
console.error('Failed to update client:', error)
|
|
return false
|
|
}
|
|
}
|
|
|
|
async function deleteClient(id: number): Promise<boolean> {
|
|
try {
|
|
await invoke('delete_client', { id })
|
|
clients.value = clients.value.filter(c => c.id !== id)
|
|
return true
|
|
} catch (error) {
|
|
console.error('Failed to delete client:', error)
|
|
return false
|
|
}
|
|
}
|
|
|
|
return {
|
|
clients,
|
|
loading,
|
|
fetchClients,
|
|
createClient,
|
|
updateClient,
|
|
deleteClient
|
|
}
|
|
})
|