Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mobile): fix transaction caching missing populated fields #376

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading