Fix issues with TransactionFormSheet

This commit is contained in:
William Brawner 2022-05-12 18:49:22 -06:00
parent ab4d581936
commit 2cffa9c83c
6 changed files with 72 additions and 41 deletions

View file

@ -24,8 +24,22 @@ class TransactionDataStore: ObservableObject {
}
}
@Published var selectedTransaction: Transaction? = nil
private var budgetId: String = ""
private var categoryId: String? = nil
private var from: Date? = nil
private var count: Int? = nil
private var page: Int? = nil
func getTransactions(_ budgetId: String, categoryId: String? = nil, from: Date? = nil, count: Int? = nil, page: Int? = nil) async {
self.budgetId = budgetId
self.categoryId = categoryId
self.from = from
self.count = count
self.page = page
await self.getTransactions()
}
func getTransactions() async {
self.transactions = .loading
do {
var categoryIds: [String] = []
@ -57,6 +71,7 @@ class TransactionDataStore: ObservableObject {
savedTransaction = try await self.transactionRepository.createTransaction(transaction)
}
self.transaction = .success(savedTransaction)
await getTransactions()
} catch {
self.transaction = .error(error, transaction)
}

View file

@ -8,6 +8,7 @@
import SwiftUI
import TwigsCore
import XCTest
struct TransactionDetailsView: View {
@EnvironmentObject var apiService: TwigsApiService

View file

@ -16,54 +16,68 @@ struct TransactionFormSheet: View {
@ViewBuilder
var body: some View {
switch self.transactionDataStore.transaction {
case .loading:
EmbeddedLoadingView()
default:
Form {
TextField(LocalizedStringKey("prompt_name"), text: $transactionForm.title)
.textInputAutocapitalization(.words)
TextField(LocalizedStringKey("prompt_description"), text: $transactionForm.description)
.textInputAutocapitalization(.sentences)
DatePicker(selection: $transactionForm.date, label: { Text(LocalizedStringKey("prompt_date")) })
TextField(LocalizedStringKey("prompt_amount"), text: $transactionForm.amount)
.keyboardType(.decimalPad)
Picker(LocalizedStringKey("prompt_type"), selection: $transactionForm.type) {
ForEach(TransactionType.allCases) { type in
Text(type.localizedKey)
NavigationView {
switch self.transactionDataStore.transaction {
case .loading:
EmbeddedLoadingView()
default:
Form {
TextField(LocalizedStringKey("prompt_name"), text: $transactionForm.title)
.textInputAutocapitalization(.words)
TextField(LocalizedStringKey("prompt_description"), text: $transactionForm.description)
.textInputAutocapitalization(.sentences)
DatePicker(selection: $transactionForm.date, label: { Text(LocalizedStringKey("prompt_date")) })
TextField(LocalizedStringKey("prompt_amount"), text: $transactionForm.amount)
.keyboardType(.decimalPad)
Picker(LocalizedStringKey("prompt_type"), selection: $transactionForm.type) {
ForEach(TransactionType.allCases) { type in
Text(type.localizedKey)
}
}
}
BudgetPicker()
CategoryPicker()
if transactionForm.showDelete {
Button(action: {
self.showingAlert = true
}) {
Text(LocalizedStringKey("delete"))
.foregroundColor(.red)
BudgetPicker()
CategoryPicker()
if transactionForm.showDelete {
Button(action: {
self.showingAlert = true
}) {
Text(LocalizedStringKey("delete"))
.foregroundColor(.red)
}
.alert(isPresented:$showingAlert) {
Alert(
title: Text(LocalizedStringKey("confirm_delete")),
message: Text(LocalizedStringKey("cannot_undo")),
primaryButton: .destructive(
Text(LocalizedStringKey("delete")),
action: { Task { await transactionForm.delete() }}
),
secondaryButton: .cancel()
)
}
} else {
EmptyView()
}
.alert(isPresented:$showingAlert) {
Alert(
title: Text(LocalizedStringKey("confirm_delete")),
message: Text(LocalizedStringKey("cannot_undo")),
primaryButton: .destructive(
Text(LocalizedStringKey("delete")),
action: { Task { await transactionForm.delete() }}
),
secondaryButton: .cancel()
)
}.environmentObject(transactionForm)
.task {
await transactionForm.load()
}
} else {
EmptyView()
}
}.environmentObject(transactionForm)
.navigationTitle(transactionForm.transactionId.isEmpty ? "add_transaction" : "edit_transaction")
.navigationBarItems(
leading: Button("cancel", action: { transactionForm.transactionList.cancelEdit() }),
trailing: Button("save", action: {
Task {
await transactionForm.save()
}
})
)
}
}
}
}
struct BudgetPicker: View {
@EnvironmentObject var transactionForm: TransactionForm
@ViewBuilder
var body: some View {
if case let .success(budgets) = self.transactionForm.budgets {

View file

@ -81,7 +81,6 @@ struct TransactionListView<Content>: View where Content: View {
categoryId: self.category?.id,
transaction: nil
))
.navigationBarTitle("add_transaction")
})
.navigationBarItems(
trailing: HStack {

View file

@ -47,6 +47,7 @@
// MARK: Transactions
"transactions" = "Transactions";
"add_transaction" = "Add Transaction";
"edit_transaction" = "Edit Transaction";
"notes" = "Notes";
"registered_by" = "Registered by";
"no_transactions" = "No transactions found for the given criteria";

View file

@ -47,6 +47,7 @@
// MARK: Transactions
"transactions" = "Transacciones";
"add_transaction" = "Agregar Transacción";
"edit_transaction" = "Editar Transacción";
"notes" = "Notas";
"registered_by" = "Registrado por";
"no_transactions" = "No se encontraron transacciones con el criterio dado";