mandant-crm/frontend/composables/useHistory.ts

43 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

import type { HistoryEntry } from '~/types'
export const useHistory = (clientId: number) => {
const entries = ref<HistoryEntry[]>([])
const fetchEntries = async (): Promise<void> => {
const response = await useApiFetch<{ data: HistoryEntry[] }>(`/api/clients/${clientId}/history`)
entries.value = response.data
}
const addEntry = async (payload: {
history_entry_type_id: number
body: string
occurred_at: string | null
}): Promise<void> => {
await useApiFetch(`/api/clients/${clientId}/history`, {
method: 'POST',
body: payload,
})
await fetchEntries()
}
const updateEntry = async (
id: number,
payload: { body: string; occurred_at: string | null }
): Promise<void> => {
await useApiFetch(`/api/history/${id}`, {
method: 'PUT',
body: payload,
})
await fetchEntries()
}
const deleteEntry = async (id: number): Promise<void> => {
await useApiFetch(`/api/history/${id}`, {
method: 'DELETE',
})
entries.value = entries.value.filter(e => e.id !== id)
}
return { entries, fetchEntries, addEntry, updateEntry, deleteEntry }
}