Add client-side validation and improve error handling in invoice form

Co-authored-by: soconnor0919 <soconnor0919@gmail.com>
This commit is contained in:
Cursor Agent
2025-07-17 12:43:44 +00:00
parent 505d47918e
commit 117ba0832a
2 changed files with 22 additions and 2 deletions

View File

@@ -498,6 +498,25 @@ function InvoiceForm({ invoiceId }: InvoiceFormProps) {
e.preventDefault();
setLoading(true);
// Client-side validation
if (!formData.clientId) {
toast.error("Please select a client");
setLoading(false);
return;
}
if (formData.items.length === 0) {
toast.error("Please add at least one invoice item");
setLoading(false);
return;
}
if (formData.items.some(item => !item.description.trim())) {
toast.error("Please provide descriptions for all items");
setLoading(false);
return;
}
try {
if (invoiceId && hasOnlyStatusChanged) {
// Use dedicated status update mutation for status-only changes
@@ -526,7 +545,6 @@ function InvoiceForm({ invoiceId }: InvoiceFormProps) {
description: item.description,
hours: item.hours,
rate: item.rate,
amount: item.hours * item.rate,
})),
};
@@ -540,7 +558,8 @@ function InvoiceForm({ invoiceId }: InvoiceFormProps) {
}
} catch (error) {
console.error("Error saving invoice:", error);
toast.error("Failed to save invoice. Check console for details.");
const errorMessage = error instanceof Error ? error.message : "Unknown error";
toast.error(`Failed to save invoice: ${errorMessage}`);
} finally {
setLoading(false);
}

View File

@@ -30,6 +30,7 @@ const createInvoiceSchema = z.object({
const updateInvoiceSchema = createInvoiceSchema.partial().extend({
id: z.string(),
clientId: z.string().min(1, "Client is required"), // Keep clientId required for updates
});
const updateStatusSchema = z.object({