2 Сделать отображение транзакций на фронте
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
export interface Transaction {
|
||||
id: number
|
||||
family_id: number
|
||||
description: string | null
|
||||
type: string
|
||||
datetime: string
|
||||
category: string
|
||||
amount: number
|
||||
created_at: string
|
||||
created_by: number
|
||||
receipt_id: number | null
|
||||
}
|
||||
|
||||
interface TransactionsResponse {
|
||||
items: Transaction[]
|
||||
}
|
||||
|
||||
interface GetTransactionsOptions {
|
||||
familyId?: number
|
||||
limit?: number
|
||||
offset?: number
|
||||
}
|
||||
|
||||
export async function getTransactions(options: GetTransactionsOptions = {}): Promise<Transaction[]> {
|
||||
const params = new URLSearchParams()
|
||||
|
||||
if (typeof options.familyId === 'number' && Number.isFinite(options.familyId) && options.familyId > 0) {
|
||||
params.set('family_id', String(options.familyId))
|
||||
}
|
||||
|
||||
params.set('limit', String(options.limit ?? 100))
|
||||
params.set('offset', String(options.offset ?? 0))
|
||||
|
||||
const query = params.toString()
|
||||
const response = await fetch(`/api/v1/transactions${query ? `?${query}` : ''}`)
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch transactions: ${response.status}`)
|
||||
}
|
||||
|
||||
const payload = await response.json() as TransactionsResponse
|
||||
return Array.isArray(payload.items) ? payload.items : []
|
||||
}
|
||||
Reference in New Issue
Block a user