12 Сделать добавление транзакций на фронте, добавить уже сгенерированые экраны в проект
This commit is contained in:
@@ -41,3 +41,67 @@ export async function getTransactions(options: GetTransactionsOptions = {}): Pro
|
||||
const payload = await response.json() as TransactionsResponse
|
||||
return Array.isArray(payload.items) ? payload.items : []
|
||||
}
|
||||
|
||||
export interface CreateTransactionData {
|
||||
family_id: number
|
||||
type?: string
|
||||
category?: string
|
||||
amount?: number
|
||||
datetime?: string
|
||||
description?: string
|
||||
receipt_number?: string
|
||||
receipt_date?: string
|
||||
}
|
||||
|
||||
// TODO: Replace with the authenticated user id when frontend auth is implemented.
|
||||
const TRANSACTION_CREATOR_ID = 1
|
||||
|
||||
export async function createTransaction(data: CreateTransactionData): Promise<Transaction> {
|
||||
const response = await fetch('/api/v1/transactions', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
...data,
|
||||
created_by: TRANSACTION_CREATOR_ID,
|
||||
}),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({ message: response.statusText }))
|
||||
throw new Error(error.message || `Failed to create transaction: ${response.status}`)
|
||||
}
|
||||
|
||||
return response.json() as Promise<Transaction>
|
||||
}
|
||||
|
||||
export interface CreateTransactionPhotoData {
|
||||
photo: File
|
||||
family_id: number
|
||||
type?: string
|
||||
category?: string
|
||||
description?: string
|
||||
}
|
||||
|
||||
export async function createTransactionFromPhoto(data: CreateTransactionPhotoData): Promise<Transaction> {
|
||||
const formData = new FormData()
|
||||
formData.append('photo', data.photo)
|
||||
formData.append('family_id', String(data.family_id))
|
||||
formData.append('created_by', String(TRANSACTION_CREATOR_ID))
|
||||
if (data.type) formData.append('type', data.type)
|
||||
if (data.category) formData.append('category', data.category)
|
||||
if (data.description) formData.append('description', data.description)
|
||||
|
||||
const response = await fetch('/api/v1/transactions', {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({ message: response.statusText }))
|
||||
throw new Error(error.message || `Failed to create transaction from photo: ${response.status}`)
|
||||
}
|
||||
|
||||
return response.json() as Promise<Transaction>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user