Skip to content

Commit bcbac66

Browse files
committed
❇️ [refactor][frontend] Lift transaction mutation into route
1 parent 1ff566c commit bcbac66

2 files changed

Lines changed: 54 additions & 43 deletions

File tree

frontend/src/components/transaction-fab.tsx

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { useMutation, useQueryClient } from '@tanstack/react-query'
21
import { Minus, Plus } from 'lucide-react'
32
import { useMemo, useState } from 'react'
4-
import { toast } from 'sonner'
53

64
import { AccountSelect } from '@/components/account-select'
75
import { FabForm } from '@/components/fab-form'
@@ -19,9 +17,7 @@ import { SheetHeader, SheetTitle } from '@/components/ui/sheet'
1917
import {
2018
type AccountRead,
2119
type TransactionCreate,
22-
type TransactionRead,
23-
createTransaction,
24-
updateTransactions
20+
type TransactionRead
2521
} from '@/lib/client'
2622
import type { Client } from '@/lib/client/client'
2723
import { CLIENT_TIMEZONE } from '@/lib/constants'
@@ -33,21 +29,26 @@ interface TransactionFabProps {
3329
open: boolean
3430
onOpenChange: (open: boolean) => void
3531
editingTransaction: TransactionRead | null
32+
onSubmit: (body: TransactionCreate) => void
33+
isPending: boolean
3634
}
3735

3836
interface TransactionFabBodyProps {
3937
client: Client
4038
account?: AccountRead
4139
editingTransaction: TransactionRead | null
42-
onClose: () => void
40+
onSubmit: (body: TransactionCreate) => void
41+
isPending: boolean
4342
}
4443

4544
export function TransactionFab({
4645
client,
4746
account,
4847
open,
4948
onOpenChange,
50-
editingTransaction
49+
editingTransaction,
50+
onSubmit,
51+
isPending
5152
}: TransactionFabProps) {
5253
return (
5354
<FabSheet
@@ -67,7 +68,8 @@ export function TransactionFab({
6768
client={client}
6869
account={account}
6970
editingTransaction={editingTransaction}
70-
onClose={() => onOpenChange(false)}
71+
onSubmit={onSubmit}
72+
isPending={isPending}
7173
/>
7274
</FabSheet>
7375
)
@@ -77,9 +79,9 @@ function TransactionFabBody({
7779
client,
7880
account,
7981
editingTransaction,
80-
onClose
82+
onSubmit,
83+
isPending
8184
}: TransactionFabBodyProps) {
82-
const queryClient = useQueryClient()
8385
const isEditing = editingTransaction != null
8486
const [amount, setAmount] = useState(() =>
8587
isEditing ? Math.abs(parseFloat(editingTransaction.amount)).toString() : ''
@@ -103,39 +105,9 @@ function TransactionFabBody({
103105
[createdAt, selectedAccount]
104106
)
105107

106-
const { mutate, isPending } = useMutation({
107-
mutationFn: async (body: TransactionCreate) => {
108-
const response = isEditing
109-
? await updateTransactions({
110-
client,
111-
body: [{ ...body, id: editingTransaction.id }]
112-
})
113-
: await createTransaction({ client, body })
114-
if (response.error)
115-
throw new Error(
116-
`Failed to ${isEditing ? 'update' : 'create'} transaction`
117-
)
118-
if (!response.data) throw new Error('No data returned')
119-
return response.data
120-
},
121-
onSuccess: () => {
122-
toast.success(`Transaction ${isEditing ? 'updated' : 'created'}`)
123-
queryClient.invalidateQueries({ queryKey: ['events'] })
124-
queryClient.invalidateQueries({ queryKey: ['transactions'] })
125-
onClose()
126-
},
127-
onError: (error) => {
128-
console.error(error)
129-
toast.error(`Failed to ${isEditing ? 'update' : 'create'} transaction`, {
130-
description:
131-
error instanceof Error ? error.message : 'An unknown error occurred'
132-
})
133-
}
134-
})
135-
136108
const handleSubmit = () => {
137109
if (selectedAccount == null || zonedCreatedAt == null) return
138-
mutate({
110+
onSubmit({
139111
account_id: selectedAccount.id,
140112
amount: negative ? `-${amount}` : amount,
141113
created_at: zonedCreatedAt

frontend/src/routes/_auth/account/$id/transaction.tsx

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useQuery } from '@tanstack/react-query'
1+
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
22
import { createFileRoute } from '@tanstack/react-router'
33
import { Fragment, useEffect, useState } from 'react'
44
import { type DateRange } from 'react-day-picker'
@@ -10,9 +10,12 @@ import { TransactionFab } from '@/components/transaction-fab'
1010
import { TransactionStatusBadge } from '@/components/transaction-status-badge'
1111
import { Separator } from '@/components/ui/separator'
1212
import {
13+
type TransactionCreate,
1314
type TransactionReadWithBalance,
15+
createTransaction,
1416
readAccount,
15-
readAccountTransactionsWithRunningBalance
17+
readAccountTransactionsWithRunningBalance,
18+
updateTransactions
1619
} from '@/lib/client'
1720
import { cn, endExclusive, formatCurrency } from '@/lib/utils'
1821

@@ -24,6 +27,7 @@ function AccountTransactionPage() {
2427
const { client } = Route.useRouteContext()
2528
const { id } = Route.useParams()
2629
const accountId = Number(id)
30+
const queryClient = useQueryClient()
2731

2832
const [fabOpen, setFabOpen] = useState(false)
2933
const [editingTransaction, setEditingTransaction] =
@@ -34,6 +38,39 @@ function AccountTransactionPage() {
3438
if (open) setEditingTransaction(null)
3539
}
3640

41+
const { mutate, isPending } = useMutation({
42+
mutationFn: async (body: TransactionCreate) => {
43+
const response = editingTransaction
44+
? await updateTransactions({
45+
client,
46+
body: [{ ...body, id: editingTransaction.id }]
47+
})
48+
: await createTransaction({ client, body })
49+
if (response.error)
50+
throw new Error(
51+
`Failed to ${editingTransaction ? 'update' : 'create'} transaction`
52+
)
53+
if (!response.data) throw new Error('No data returned')
54+
return response.data
55+
},
56+
onSuccess: () => {
57+
toast.success(`Transaction ${editingTransaction ? 'updated' : 'created'}`)
58+
queryClient.invalidateQueries({ queryKey: ['events'] })
59+
queryClient.invalidateQueries({ queryKey: ['transactions'] })
60+
setFabOpen(false)
61+
},
62+
onError: (error) => {
63+
console.error(error)
64+
toast.error(
65+
`Failed to ${editingTransaction ? 'update' : 'create'} transaction`,
66+
{
67+
description:
68+
error instanceof Error ? error.message : 'An unknown error occurred'
69+
}
70+
)
71+
}
72+
})
73+
3774
const [dateRange, setDateRange] = useState<DateRange | undefined>()
3875
const start = dateRange?.from?.toISOString() ?? null
3976
const end = endExclusive(dateRange?.to)
@@ -211,6 +248,8 @@ function AccountTransactionPage() {
211248
open={fabOpen}
212249
onOpenChange={handleFabOpenChange}
213250
editingTransaction={editingTransaction}
251+
onSubmit={mutate}
252+
isPending={isPending}
214253
/>
215254
</>
216255
)

0 commit comments

Comments
 (0)