Skip to content

Commit

Permalink
fix(mobile): fix transaction caching missing populated fields
Browse files Browse the repository at this point in the history
  • Loading branch information
duongdev committed Sep 23, 2024
1 parent fa96aca commit ece9fa2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
20 changes: 15 additions & 5 deletions apps/mobile/components/transaction/transaction-item.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { TRANSACTION_ICONS } from '@/lib/icons/category-icons'
import { useBudgetList } from '@/stores/budget/hooks'
import { useCategoryList } from '@/stores/category/hooks'
import { useWalletList } from '@/stores/wallet/hooks'
import type { TransactionPopulated } from '@6pm/validation'
import { t } from '@lingui/macro'
import { useLingui } from '@lingui/react'
Expand All @@ -19,6 +21,8 @@ export const TransactionItem: FC<TransactionItemProps> = ({ transaction }) => {
const { i18n } = useLingui()
const { categoryId } = transaction
const { categoriesDict } = useCategoryList()
const { budgetsDict } = useBudgetList()
const { walletsDict } = useWalletList()
const category = (categoryId && categoriesDict[categoryId]) || null

const iconName = useMemo(() => {
Expand All @@ -31,6 +35,12 @@ export const TransactionItem: FC<TransactionItemProps> = ({ transaction }) => {
)
}, [transaction.note, category?.name, i18n])

const budget =
(transaction.budgetId && budgetsDict[transaction.budgetId]) || null
const walletAccount =
(transaction.walletAccountId && walletsDict[transaction.walletAccountId]) ||
null

return (
<Link
asChild
Expand Down Expand Up @@ -78,29 +88,29 @@ export const TransactionItem: FC<TransactionItemProps> = ({ transaction }) => {
/>
</View>
<View className="flex-row items-center gap-3 self-start">
{transaction.walletAccount && (
{walletAccount && (
<View className="flex-row items-center gap-2">
<GenericIcon
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
name={transaction.walletAccount.icon as any}
name={walletAccount.icon as any}
className="size-4 text-muted-foreground"
/>
<Text
numberOfLines={1}
className="text-muted-foreground text-sm"
>
{transaction.walletAccount.name}
{walletAccount.name}
</Text>
</View>
)}
{transaction.budget && (
{budget && (
<View className="flex-row items-center gap-2">
<LandPlotIcon className="size-4 text-muted-foreground" />
<Text
numberOfLines={1}
className="text-muted-foreground text-sm"
>
{transaction.budget.name}
{budget.name}
</Text>
</View>
)}
Expand Down
11 changes: 3 additions & 8 deletions apps/mobile/stores/transaction/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
type TransactionFormValues,
type TransactionPopulated,
TransactionPopulatedSchema,
TransactionSchema,
} from '@6pm/validation'
import { useMutation, useQuery } from '@tanstack/react-query'
import { keyBy } from 'lodash-es'
Expand Down Expand Up @@ -203,7 +202,7 @@ export function useCreateTransaction() {
}

const response = await result.json()
const transaction = TransactionSchema.merge(
const transaction = TransactionPopulatedSchema.merge(
z.object({
id: z.string(),
amount: z.number({ coerce: true }),
Expand Down Expand Up @@ -309,12 +308,8 @@ export function useUpdateTransaction() {
}

const res = await result.json()
const transaction: TransactionPopulated = TransactionSchema.merge(
z.object({
amount: z.number({ coerce: true }),
amountInVnd: z.number({ coerce: true }),
}),
).parse(res)
const transaction: TransactionPopulated =
TransactionPopulatedSchema.parse(res)

updateTransactionInStore(transaction)

Expand Down

0 comments on commit ece9fa2

Please sign in to comment.