import React, { useEffect, useState } from "react"; import { AppFormProvider, useAppForm } from "../../hooks/appHooks/useAppForm"; import { zodResolver } from "@hookform/resolvers/zod"; import { defaultPurchaseValue, PurchaseSchema, getStepFields, } from "./PurchaseSchema"; import PurchasePartyDetails from "./PurchasePartyDetails"; import PurchaseTransportDetails from "./PurchaseTransportDetails"; import PurchasePaymentDetails from "./PurchasePaymentDetails"; import { useCreatePurchaseInvoice, usePurchase, useUpdatePurchaseInvoice, } from "../../hooks/usePurchase"; const ManagePurchase = ({ onClose, purchaseId }) => { const { data } = usePurchase(purchaseId); const [activeTab, setActiveTab] = useState(0); const [completedTabs, setCompletedTabs] = useState([]); const stepsConfig = [ { name: "Party Details", icon: "bx bx-user bx-md", subtitle: "Supplier & project information", component: , }, { name: "Invoice & Transport", icon: "bx bx-receipt bx-md", subtitle: "Invoice, eWay bill & transport info", component: , }, { name: "Payment Details", icon: "bx bx-credit-card bx-md", subtitle: "Amount, tax & due date", component: , }, ]; const purchaseOrder = useAppForm({ resolver: zodResolver(PurchaseSchema), defaultValues: defaultPurchaseValue, mode: "onChange", shouldUnregister: false, }); const { reset, formState: { errors }, } = purchaseOrder; useEffect(() => { if (purchaseId && data) { reset({ ...data, title: data.title, projectId: data.project.id, organizationId: data.organization.id, supplierId: data.supplier.id, }); setCompletedTabs([0, 1, 2]); } }, [purchaseId, data, reset]); const handleNext = async (e) => { e.preventDefault(); e.stopPropagation(); const currentStepFields = getStepFields(activeTab); const valid = await purchaseOrder.trigger(currentStepFields); if (valid) { setCompletedTabs((prev) => [...new Set([...prev, activeTab])]); setActiveTab((prev) => Math.min(prev + 1, stepsConfig.length - 1)); } }; const handlePrev = (e) => { e.preventDefault(); e.stopPropagation(); setActiveTab((prev) => Math.max(prev - 1, 0)); }; const { mutate: CreateInvoice, isPending } = useCreatePurchaseInvoice(() => { reset(); onClose(); }); const { mutate: updatePurchase, isPending: isUpdating } = useUpdatePurchaseInvoice(() => { reset(); onClose(); }); const onSubmit = (formData) => { if (activeTab !== 2) { console.warn("Submit blocked - not on last step"); return; } const dirtyFields = purchaseOrder.formState.dirtyFields; if (purchaseId) { const changedData = Object.keys(dirtyFields).reduce((acc, key) => { if (dirtyFields[key]) { acc.push({ operationType: 0, path: `/${key}`, op: "replace", from: null, value: formData[key], }); } return acc; }, []); updatePurchase({ purchaseId, payload: changedData, }); } else { CreateInvoice(formData); } }; return (
{/* Header */}
{stepsConfig.map((step, index) => { const isActive = activeTab === index; const isCompleted = completedTabs.includes(index); return (
{index < stepsConfig.length - 1 && (
)}
); })}
{/* Content */}
{ if (e.key === "Enter" && activeTab !== 2) { e.preventDefault(); } }} onSubmit={(e) => { e.preventDefault(); if (activeTab !== 2) { console.warn("BLOCKED SUBMIT on step:", activeTab); return; } purchaseOrder.handleSubmit(onSubmit)(e); }} > {stepsConfig[activeTab].component}
{activeTab < stepsConfig.length - 1 ? ( ) : ( )}
); }; export default ManagePurchase;