diff --git a/src/components/invoice/invoice-creator.tsx b/src/components/invoice/invoice-creator.tsx index eb29013..93e1bcd 100644 --- a/src/components/invoice/invoice-creator.tsx +++ b/src/components/invoice/invoice-creator.tsx @@ -49,7 +49,7 @@ export function InvoiceCreator({ } toast.success("Invoice created successfully"); await utils.invoice.getAll.invalidate(); - router.push("/dashboard"); + router.push("/invoices"); }, onError: (error) => { toast.error("Failed to create invoice", { @@ -64,7 +64,7 @@ export function InvoiceCreator({ onSuccess: async () => { toast.success("Invoice created successfully"); await utils.invoice.getAll.invalidate(); - router.push("/dashboard"); + router.push("/invoices"); }, onError: (error) => { toast.error("Failed to create invoice", { diff --git a/src/components/invoice/invoice-form/invoice-form.tsx b/src/components/invoice/invoice-form/invoice-form.tsx index ee60eee..2fabb49 100644 --- a/src/components/invoice/invoice-form/invoice-form.tsx +++ b/src/components/invoice/invoice-form/invoice-form.tsx @@ -363,20 +363,8 @@ export function InvoiceForm({ async (data: InvoiceFormValues) => { // Prevent multiple submissions if (isSubmitting) return; - setIsSubmitting(true); - // If Crypto-to-fiat is enabled but no payment details are linked, show error - if (data.isCryptoToFiatAvailable && !data.paymentDetailsId) { - // Set form error for paymentDetailsId - form.setError("paymentDetailsId", { - type: "required", - message: "Please select a payment method for Crypto-to-fiat payment", - }); - setIsSubmitting(false); - return; - } - // Check if payment details have approved status if (data.isCryptoToFiatAvailable && data.paymentDetailsId) { const selectedPaymentDetail = linkedPaymentDetails?.find( @@ -429,13 +417,7 @@ export function InvoiceForm({ setIsSubmitting(false); } }, - [ - linkedPaymentDetails, - onSubmit, - form.setError, - isSubmitting, - handleBankAccountSuccess, - ], + [linkedPaymentDetails, onSubmit, isSubmitting, handleBankAccountSuccess], ); // Add timeout effect for pending approval modal @@ -755,7 +737,7 @@ export function InvoiceForm({
{fields.map((field, index) => ( -
+
{ - // If invoice is recurring, startDate and frequency must be provided - if (data.isRecurring) { - return !!data.startDate && !!data.frequency; + .superRefine((data, ctx) => { + if (data.isRecurring) { + if (!data.startDate) { + ctx.addIssue({ + code: "custom", + message: "Start date is required for recurring invoices", + path: ["startDate"], + }); } - return true; - }, - { - message: "Start date and frequency are required for recurring invoices", - path: ["isRecurring"], - }, - ) - .refine( - (data) => { - // Wallet address is required when crypto-to-fiat is not enabled - if (!data.isCryptoToFiatAvailable) { - return !!data.walletAddress && isEthereumAddress(data.walletAddress); + + if (!data.frequency) { + ctx.addIssue({ + code: "custom", + message: "Frequency is required for recurring invoices", + path: ["frequency"], + }); } - return true; - }, - { - message: "Valid wallet address is required for direct crypto payments", - path: ["walletAddress"], - }, - ) - .refine( - (data) => { - // Payment details are required when crypto-to-fiat is enabled - if (data.isCryptoToFiatAvailable) { - return !!data.paymentDetailsId; + } + + if (!data.isCryptoToFiatAvailable) { + if (!data.walletAddress) { + ctx.addIssue({ + code: "custom", + message: "Wallet address is required", + path: ["walletAddress"], + }); } - return true; - }, - { - message: "Please select a payment method for Crypto-to-fiat payment", - path: ["paymentDetailsId"], - }, - ); + } else { + if (!data.paymentDetailsId) { + ctx.addIssue({ + code: "custom", + message: "Payment details are required for crypto-to-fiat payments", + path: ["paymentDetailsId"], + }); + } + } + + const dueDate = new Date(data.dueDate).getTime(); + const now = new Date().getTime(); + if (dueDate < now) { + ctx.addIssue({ + code: "custom", + message: "Due date must be in future", + path: ["dueDate"], + }); + } + }); export type InvoiceFormValues = z.infer;