Skip to content

Commit 17efe5f

Browse files
committed
❇️ [refactor][frontend] AccountSelect data via props, drop client from transaction fab
1 parent bcbac66 commit 17efe5f

4 files changed

Lines changed: 34 additions & 25 deletions

File tree

frontend/src/components/account-select.tsx

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useQuery } from '@tanstack/react-query'
21
import { ChevronsUpDown } from 'lucide-react'
32

43
import { Button } from '@/components/ui/button'
@@ -15,11 +14,10 @@ import {
1514
ItemMedia,
1615
ItemTitle
1716
} from '@/components/ui/item'
18-
import { type AccountRead, readAccounts } from '@/lib/client'
19-
import type { Client } from '@/lib/client/client'
17+
import { type AccountRead } from '@/lib/client'
2018

2119
interface AccountSelectProps {
22-
client: Client
20+
accounts: AccountRead[]
2321
value: AccountRead | null
2422
onValueChange: (account: AccountRead) => void
2523
id?: string
@@ -31,21 +29,11 @@ interface AccountItemProps {
3129
}
3230

3331
export function AccountSelect({
34-
client,
32+
accounts,
3533
value,
3634
onValueChange,
3735
id
3836
}: AccountSelectProps) {
39-
const { data: accounts } = useQuery({
40-
queryKey: ['accounts'],
41-
queryFn: async () => {
42-
const response = await readAccounts({ client })
43-
if (response.error) throw new Error('Failed to fetch accounts')
44-
if (!response.data) throw new Error('No data returned')
45-
return response.data
46-
}
47-
})
48-
4937
return (
5038
<DropdownMenu>
5139
<DropdownMenuTrigger asChild>
@@ -67,7 +55,7 @@ export function AccountSelect({
6755
align="start"
6856
className="w-(--radix-dropdown-menu-trigger-width)"
6957
>
70-
{accounts?.map((account) => (
58+
{accounts.map((account) => (
7159
<DropdownMenuItem
7260
key={account.id}
7361
className="p-0"

frontend/src/components/link-transactions-table.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
type AccountRead,
3636
type TransactionRead,
3737
type TransactionStatus,
38+
readAccounts,
3839
readTransactions
3940
} from '@/lib/client'
4041
import type { Client } from '@/lib/client/client'
@@ -196,6 +197,16 @@ export function LinkTransactionsTable({
196197
)
197198
}
198199

200+
const { data: accounts } = useQuery({
201+
queryKey: ['accounts'],
202+
queryFn: async () => {
203+
const response = await readAccounts({ client })
204+
if (response.error) throw new Error('Failed to fetch accounts')
205+
if (!response.data) throw new Error('No data returned')
206+
return response.data
207+
}
208+
})
209+
199210
const { data } = useQuery({
200211
queryKey: ['transactions', selectedAccount?.id, 'unlinked'],
201212
enabled: selectedAccount !== null,
@@ -231,7 +242,7 @@ export function LinkTransactionsTable({
231242
return (
232243
<div className="space-y-3">
233244
<AccountSelect
234-
client={client}
245+
accounts={accounts ?? []}
235246
value={selectedAccount}
236247
onValueChange={setSelectedAccount}
237248
/>

frontend/src/components/transaction-fab.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ import {
1919
type TransactionCreate,
2020
type TransactionRead
2121
} from '@/lib/client'
22-
import type { Client } from '@/lib/client/client'
2322
import { CLIENT_TIMEZONE } from '@/lib/constants'
2423
import { formatZonedDateTime, toZonedISOString } from '@/lib/utils'
2524

2625
interface TransactionFabProps {
27-
client: Client
26+
accounts: AccountRead[]
2827
account?: AccountRead
2928
open: boolean
3029
onOpenChange: (open: boolean) => void
@@ -34,15 +33,15 @@ interface TransactionFabProps {
3433
}
3534

3635
interface TransactionFabBodyProps {
37-
client: Client
36+
accounts: AccountRead[]
3837
account?: AccountRead
3938
editingTransaction: TransactionRead | null
4039
onSubmit: (body: TransactionCreate) => void
4140
isPending: boolean
4241
}
4342

4443
export function TransactionFab({
45-
client,
44+
accounts,
4645
account,
4746
open,
4847
onOpenChange,
@@ -65,7 +64,7 @@ export function TransactionFab({
6564
{/* Keyed so the form re-initializes from the picked transaction. */}
6665
<TransactionFabBody
6766
key={editingTransaction?.id ?? 'new'}
68-
client={client}
67+
accounts={accounts}
6968
account={account}
7069
editingTransaction={editingTransaction}
7170
onSubmit={onSubmit}
@@ -76,7 +75,7 @@ export function TransactionFab({
7675
}
7776

7877
function TransactionFabBody({
79-
client,
78+
accounts,
8079
account,
8180
editingTransaction,
8281
onSubmit,
@@ -125,7 +124,7 @@ function TransactionFabBody({
125124
<FieldLabel htmlFor="txn-account">Account</FieldLabel>
126125
<AccountSelect
127126
id="txn-account"
128-
client={client}
127+
accounts={accounts}
129128
value={selectedAccount}
130129
onValueChange={setPickedAccount}
131130
/>

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
createTransaction,
1616
readAccount,
1717
readAccountTransactionsWithRunningBalance,
18+
readAccounts,
1819
updateTransactions
1920
} from '@/lib/client'
2021
import { cn, endExclusive, formatCurrency } from '@/lib/utils'
@@ -92,6 +93,16 @@ function AccountTransactionPage() {
9293
}
9394
})
9495

96+
const { data: accounts } = useQuery({
97+
queryKey: ['accounts'],
98+
queryFn: async () => {
99+
const response = await readAccounts({ client })
100+
if (response.error) throw new Error('Failed to fetch accounts')
101+
if (!response.data) throw new Error('No data returned')
102+
return response.data
103+
}
104+
})
105+
95106
const {
96107
isError: isTransactionsError,
97108
data: transactions,
@@ -243,7 +254,7 @@ function AccountTransactionPage() {
243254
</div>
244255

245256
<TransactionFab
246-
client={client}
257+
accounts={accounts ?? []}
247258
account={account}
248259
open={fabOpen}
249260
onOpenChange={handleFabOpenChange}

0 commit comments

Comments
 (0)