Skip to content

Commit

Permalink
feat(mobile): update budget hooks (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
duongdev committed Jul 20, 2024
1 parent 1bf0573 commit 7b4770c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 26 deletions.
1 change: 1 addition & 0 deletions apps/mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"date-fns": "^3.6.0",
"decimal.js": "^10.4.3",
"expo": "~51.0.11",
"expo-auth-session": "~5.5.2",
"expo-camera": "~15.0.13",
Expand Down
53 changes: 40 additions & 13 deletions apps/mobile/stores/budget/hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { getHonoClient } from '@/lib/client'
import {
type BudgetFormValues,
BudgetSchema,
type BudgetWithRelations,
} from '@6pm/validation'
import { type BudgetFormValues, BudgetSchema } from '@6pm/validation'
import { createId } from '@paralleldrive/cuid2'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { keyBy } from 'lodash-es'
import Decimal from 'decimal.js'
import { first, keyBy, orderBy } from 'lodash-es'
import { useMemo } from 'react'
import { z } from 'zod'
import { budgetQueries } from './queries'
import { useBudgetStore } from './store'
import { type BudgetItem, useBudgetStore } from './store'

export const useBudgetList = () => {
const budgets = useBudgetStore().budgets
Expand Down Expand Up @@ -60,7 +57,7 @@ export const useBudgetList = () => {

export const useBudget = (budgetId: string) => {
const budgets = useBudgetStore().budgets
const budget: BudgetWithRelations | null = useMemo(
const budget: BudgetItem | null = useMemo(
() => budgets.find((budget) => budget.id === budgetId) || null,
[budgets, budgetId],
)
Expand Down Expand Up @@ -98,7 +95,26 @@ export const useUpdateBudget = () => {
return
}

budget = { ...budget, ...data, updatedAt: new Date() }
const latestPeriodConfig = first(
orderBy(budget.periodConfigs, 'startDate', 'desc'),
)

budget = {
...budget,
...data,
updatedAt: new Date(),
periodConfigs: budget.periodConfigs.map((pc) =>
pc.id === latestPeriodConfig?.id
? {
...latestPeriodConfig,
...data.period,
amount:
new Decimal(data.period.amount) ??
latestPeriodConfig.amount,
}
: pc,
),
}

updateBudgetInStore(budget)

Expand Down Expand Up @@ -135,14 +151,25 @@ export const useCreateBudget = () => {
throw result
},
onMutate({ id, data }) {
const budget: BudgetWithRelations = {
const period: BudgetItem['periodConfigs'][number] = {
...data.period,
id: data.period.id ?? createId(),
budgetId: id!,
createdAt: new Date(),
updatedAt: new Date(),
amount: new Decimal(data.period.amount),
startDate: data.period.startDate
? new Date(data.period.startDate)
: null,
endDate: data.period.endDate ? new Date(data.period.endDate) : null,
}

const budget: BudgetItem = {
id: id!,
createdAt: new Date(),
updatedAt: new Date(),
description: '',
budgetUsers: [],
invitations: [],
transactions: [],
periodConfigs: [period],
...data,
}

Expand Down
16 changes: 10 additions & 6 deletions apps/mobile/stores/budget/store.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import type { BudgetWithRelations } from '@6pm/validation'
import type { Budget, BudgetPeriodConfig } from '@6pm/validation'
import AsyncStorage from '@react-native-async-storage/async-storage'
import { create } from 'zustand'
import { createJSONStorage, persist } from 'zustand/middleware'

export type BudgetItem = Budget & {
periodConfigs: BudgetPeriodConfig[]
}

interface BudgetStore {
budgets: BudgetWithRelations[]
setBudgets: (budgets: BudgetWithRelations[]) => void
updateBudget: (budget: BudgetWithRelations) => void
budgets: BudgetItem[]
setBudgets: (budgets: BudgetItem[]) => void
updateBudget: (budget: BudgetItem) => void
}

export const useBudgetStore = create<BudgetStore>()(
persist(
(set) => ({
budgets: [],
setBudgets: (budgets: BudgetWithRelations[]) => set({ budgets }),
updateBudget: (budget: BudgetWithRelations) =>
setBudgets: (budgets: BudgetItem[]) => set({ budgets }),
updateBudget: (budget: BudgetItem) =>
set((state) => {
const index = state.budgets.findIndex((c) => c.id === budget.id)
if (index === -1) {
Expand Down
17 changes: 10 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7b4770c

Please sign in to comment.