fix #23: allow empty convertedTotal

This commit is contained in:
vas3k
2025-05-22 19:42:41 +02:00
parent 347cf2a0e8
commit 289b436236
3 changed files with 18 additions and 14 deletions

View File

@@ -13,7 +13,7 @@ import { getFields } from "@/models/fields"
import { getUnsortedFiles } from "@/models/files"
import { getProjects } from "@/models/projects"
import { getSettings } from "@/models/settings"
import { FileText, PartyPopper, Settings, Upload, Brain } from "lucide-react"
import { FileText, PartyPopper, Settings, Upload } from "lucide-react"
import { Metadata } from "next"
import Link from "next/link"

View File

@@ -146,15 +146,17 @@ export default function TransactionEditForm({
/>
{formData.currencyCode !== settings.default_currency || formData.convertedTotal !== 0 ? (
<>
<FormInput
title={`Total converted to ${formData.convertedCurrencyCode || "UNKNOWN CURRENCY"}`}
type="number"
step="0.01"
name="convertedTotal"
defaultValue={formData.convertedTotal.toFixed(2)}
isRequired={fieldMap.convertedTotal.isRequired}
className="max-w-36"
/>
{formData.convertedTotal !== null && (
<FormInput
title={`Total converted to ${formData.convertedCurrencyCode || "UNKNOWN CURRENCY"}`}
type="number"
step="0.01"
name="convertedTotal"
defaultValue={formData.convertedTotal.toFixed(2)}
isRequired={fieldMap.convertedTotal.isRequired}
className="max-w-36"
/>
)}
{(!formData.convertedCurrencyCode || formData.convertedCurrencyCode !== settings.default_currency) && (
<FormSelectCurrency
title="Convert to"

View File

@@ -8,18 +8,21 @@ export const transactionFormSchema = z
type: z.string().optional(),
total: z
.string()
.optional()
.transform((val) => {
if (!val || val.trim() === '') return null
const num = parseFloat(val)
if (isNaN(num)) {
throw new z.ZodError([{ message: "Invalid total", path: ["total"], code: z.ZodIssueCode.custom }])
}
return Math.round(num * 100) // convert to cents
})
.optional(),
}),
currencyCode: z.string().max(5).optional(),
convertedTotal: z
.string()
.optional()
.transform((val) => {
if (!val || val.trim() === '') return null
const num = parseFloat(val)
if (isNaN(num)) {
throw new z.ZodError([
@@ -27,8 +30,7 @@ export const transactionFormSchema = z
])
}
return Math.round(num * 100) // convert to cents
})
.optional(),
}),
convertedCurrencyCode: z.string().max(5).optional(),
categoryCode: z.string().optional(),
projectCode: z.string().optional(),