Skip to content

Commit

Permalink
fix(mobile): prevent set budget amount zero (#346)
Browse files Browse the repository at this point in the history
Resolves #322
  • Loading branch information
bkdev98 committed Sep 21, 2024
1 parent a8e46bc commit a233ddb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion apps/mobile/app/(app)/budget/[budgetId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default function BudgetDetailScreen() {
push
>
<Button size="icon" variant="ghost">
<SettingsIcon className="size-6 text-primary" />
<SettingsIcon className="size-6 text-foreground" />
</Button>
</Link>
</View>
Expand Down
15 changes: 9 additions & 6 deletions apps/mobile/stores/budget/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,15 @@ export function useBudgetPeriodStats(

const remainingAmount = budgetAmount.add(totalBudgetUsage)

const usagePercentage = (
totalBudgetUsage.gt(0) ? new Decimal(0) : totalBudgetUsage.abs()
)
.div(budgetAmount!)
.mul(100)
.toNumber()
const usagePercentage = useMemo(() => {
if (budgetAmount.eq(0)) {
return 0
}
return (totalBudgetUsage.gt(0) ? new Decimal(0) : totalBudgetUsage.abs())
.div(budgetAmount!)
.mul(100)
.toNumber()
}, [totalBudgetUsage, budgetAmount])

const averageAmountPerDay = budgetAmount.div(
dayjsExtended(periodConfig?.endDate!).diff(
Expand Down
4 changes: 2 additions & 2 deletions packages/validation/src/budget.zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const zCreateBudget = z.object({
amount: z
.string({ coerce: true })
.transform((val) => Number(`${val}`.replace(',', '.')))
.pipe(z.number({ coerce: true }).min(0))
.pipe(z.number({ coerce: true }).min(1))
.optional(),
startDate: z.date({ coerce: true }).optional(),
endDate: z.date({ coerce: true }).optional(),
Expand All @@ -41,7 +41,7 @@ export const zUpdateBudget = z.object({
amount: z
.string({ coerce: true })
.transform((val) => Number(`${val}`.replace(',', '.')))
.pipe(z.number({ coerce: true }).min(0))
.pipe(z.number({ coerce: true }).min(1))
.optional(),
startDate: z.date({ coerce: true }).optional(),
endDate: z.date({ coerce: true }).optional(),
Expand Down

0 comments on commit a233ddb

Please sign in to comment.