Skip to content

Commit

Permalink
feat(mobile): [Wallet] add WalletAccountWithBalance schema for better…
Browse files Browse the repository at this point in the history
… type and fix balance display (#100)
  • Loading branch information
bkdev98 committed Jul 11, 2024
1 parent 33e3f83 commit 03298a3
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion apps/mobile/app/(app)/wallet/[walletId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export default function EditAccountScreen() {
defaultValues={{
name: walletAccount.name,
preferredCurrency: walletAccount.preferredCurrency,
balance: walletAccount.balance as number,
balance: walletAccount.balance,
icon: walletAccount.icon ?? 'CreditCard',
description: walletAccount.description ?? '',
lastDigits: walletAccount.lastDigits ?? '',
Expand Down
8 changes: 1 addition & 7 deletions apps/mobile/app/(app)/wallet/accounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,7 @@ export default function WalletAccountsScreen() {
className="bg-card"
contentContainerClassName="py-3"
data={walletAccounts}
renderItem={({ item }) => (
<WalletAccountItem
// Date is typed as string in rpc output, not sure why
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
data={item as any}
/>
)}
renderItem={({ item }) => <WalletAccountItem data={item} />}
keyExtractor={(item) => item.id}
refreshing={isLoading}
onRefresh={refetch}
Expand Down
2 changes: 1 addition & 1 deletion apps/mobile/components/form-fields/input-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const InputField = forwardRef(
ref={ref}
onChangeText={onChange}
onBlur={onBlur}
value={value}
value={value?.toString()}
className={cn(
className,
leftSection && 'pl-10',
Expand Down
4 changes: 2 additions & 2 deletions apps/mobile/components/transaction/select-account-field.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { sleep } from '@/lib/utils'
import { useWallets } from '@/queries/wallet'
import type { UserWalletAccount } from '@6pm/validation'
import type { WalletAccountWithBalance } from '@6pm/validation'
import {
BottomSheetBackdrop,
BottomSheetFlatList,
Expand All @@ -20,7 +20,7 @@ import { Text } from '../ui/text'
export function SelectAccountField({
onSelect,
}: {
onSelect?: (walletAccount: UserWalletAccount) => void
onSelect?: (walletAccount: WalletAccountWithBalance) => void
}) {
const { bottom } = useSafeAreaInsets()
const { data: walletAccounts, isLoading } = useWallets()
Expand Down
4 changes: 2 additions & 2 deletions apps/mobile/components/wallet/wallet-account-item.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { UserWalletAccount } from '@6pm/validation'
import type { WalletAccountWithBalance } from '@6pm/validation'
import { Link } from 'expo-router'
import { ChevronRightIcon } from 'lucide-react-native'
import type { FC } from 'react'
Expand All @@ -8,7 +8,7 @@ import { MenuItem } from '../common/menu-item'
import { Text } from '../ui/text'

type WalletAccountItemProps = {
data: UserWalletAccount & { balance: number }
data: WalletAccountWithBalance
}

export const WalletAccountItem: FC<WalletAccountItemProps> = ({ data }) => {
Expand Down
4 changes: 2 additions & 2 deletions apps/mobile/queries/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getHonoClient } from '@/lib/client'
import { UserWalletAccountSchema } from '@6pm/validation'
import { WalletAccountWithBalanceSchema } from '@6pm/validation'
import { createQueryKeys } from '@lukemorales/query-key-factory'
import { useQuery } from '@tanstack/react-query'

Expand All @@ -14,7 +14,7 @@ export const walletQueries = createQueryKeys('wallet', {
}
const rawResult = await res.json()
const result = rawResult.map((item) =>
UserWalletAccountSchema.parse(item),
WalletAccountWithBalanceSchema.parse(item),
)
return result
},
Expand Down
9 changes: 9 additions & 0 deletions packages/validation/src/wallet.zod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { z } from 'zod'
import { UserWalletAccountSchema } from './prisma'

export const zCreateWallet = z.object({
name: z.string(),
Expand All @@ -22,3 +23,11 @@ export const zAccountFormValues = zCreateWallet.extend({
balance: z.number({ coerce: true }).positive().optional(),
})
export type AccountFormValues = z.infer<typeof zAccountFormValues>

export const WalletAccountWithBalanceSchema = UserWalletAccountSchema.extend({
balance: z.number({ coerce: true }).optional(),
})

export type WalletAccountWithBalance = z.infer<
typeof WalletAccountWithBalanceSchema
>

0 comments on commit 03298a3

Please sign in to comment.