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 { getUnsortedFiles } from "@/models/files"
import { getProjects } from "@/models/projects" import { getProjects } from "@/models/projects"
import { getSettings } from "@/models/settings" 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 { Metadata } from "next"
import Link from "next/link" import Link from "next/link"

View File

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

View File

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