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

feat(mobile): populate category, budget, wallet on transaction hooks #366

Merged
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: 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
Loading