Skip to content

Commit

Permalink
feat(mobile): populate category, budget, wallet on transaction hooks (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
duongdev authored Sep 22, 2024
1 parent 3275491 commit e0e8de5
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions apps/mobile/stores/transaction/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import { keyBy } from 'lodash-es'
import { usePostHog } from 'posthog-react-native'
import { useMemo } from 'react'
import { z } from 'zod'
import { useBudgetList } from '../budget/hooks'
import { useCategoryList } from '../category/hooks'
import type { StoreHookQueryOptions } from '../core/stores'
import { useWalletList } from '../wallet/hooks'
import { transactionQueries } from './queries'
import { useTransactionStore } from './store'

Expand Down Expand Up @@ -172,6 +174,8 @@ export function useCreateTransaction() {
(state) => state.updateTransaction,
)
const { categoriesDict } = useCategoryList()
const { budgetsDict } = useBudgetList()
const { walletsDict } = useWalletList()

const mutation = useMutation({
mutationFn: async ({
Expand Down Expand Up @@ -214,6 +218,10 @@ export function useCreateTransaction() {
onMutate({ id, data }) {
const category = data.categoryId ? categoriesDict[data.categoryId] : null
const categoryType = category?.type
const budget = data.budgetId ? budgetsDict[data.budgetId] : null
const wallet = data.walletAccountId
? walletsDict[data.walletAccountId]
: null

const amount = category
? categoryType === 'INCOME'
Expand All @@ -232,6 +240,9 @@ export function useCreateTransaction() {
budgetId: data.budgetId || null,
note: data.note || null,
categoryId: data.categoryId || null,
category,
budget,
walletAccount: wallet,
}

updateTransactionInStore(transaction)
Expand Down Expand Up @@ -264,6 +275,8 @@ export function useUpdateTransaction() {
)
const transactions = useTransactionStore().transactions
const { categoriesDict } = useCategoryList()
const { budgetsDict } = useBudgetList()
const { walletsDict } = useWalletList()

const mutation = useMutation({
mutationFn: async ({
Expand Down Expand Up @@ -311,6 +324,10 @@ export function useUpdateTransaction() {
const transactionInStore = transactions.find((t) => t.id === id)
const category = data.categoryId ? categoriesDict[data.categoryId] : null
const categoryType = category?.type
const budget = data.budgetId ? budgetsDict[data.budgetId] : null
const wallet = data.walletAccountId
? walletsDict[data.walletAccountId]
: null

const amount = getTransactionAmountBasedOnCategory(
data.amount,
Expand All @@ -329,6 +346,9 @@ export function useUpdateTransaction() {
budgetId: data.budgetId ?? transactionInStore?.budgetId ?? null,
note: data.note ?? transactionInStore?.note ?? null,
categoryId: data.categoryId ?? transactionInStore?.categoryId ?? null,
category,
budget,
walletAccount: wallet,
}

updateTransactionInStore(transaction)
Expand Down

0 comments on commit e0e8de5

Please sign in to comment.