|
| 1 | +import { useState, useEffect } from "react"; |
| 2 | +import { useForm, FormProvider } from "react-hook-form"; |
| 3 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 4 | +import { assetSchema, AssetFormData } from "../schemas/assetSchema"; |
| 5 | +import FormStep1 from "../../components/form/FormStep1"; |
| 6 | +import FormStep2 from "../components/form/FormStep2"; |
| 7 | +import FormStep3 from "../components/form/FormStep3"; |
| 8 | +import FormStep4 from "../components/form/FormStep4"; |
| 9 | +import ProgressBar from "../components/ProgressBar"; |
| 10 | +import { saveToStorage, getFromStorage, clearStorage } from "../utils/storage"; |
| 11 | +import { toast } from "react-toastify"; |
| 12 | +import "react-toastify/dist/ReactToastify.css"; |
| 13 | +import { createAsset } from "../utils/api"; |
| 14 | + |
| 15 | +const steps = [FormStep1, FormStep2, FormStep3, FormStep4]; |
| 16 | + |
| 17 | +const AssetFormPage = () => { |
| 18 | + const [currentStep, setCurrentStep] = useState(0); |
| 19 | + |
| 20 | + const methods = useForm<AssetFormData>({ |
| 21 | + resolver: zodResolver(assetSchema), |
| 22 | + defaultValues: getFromStorage("assetForm") || {}, |
| 23 | + mode: "onChange", |
| 24 | + }); |
| 25 | + |
| 26 | + const CurrentStepComponent = steps[currentStep]; |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + const subscription = methods.watch((data) => saveToStorage("assetForm", data)); |
| 30 | + return () => subscription.unsubscribe(); |
| 31 | + }, [methods]); |
| 32 | + |
| 33 | + const nextStep = async () => { |
| 34 | + const isValid = await methods.trigger(); |
| 35 | + if (isValid) setCurrentStep((prev) => Math.min(prev + 1, steps.length - 1)); |
| 36 | + }; |
| 37 | + |
| 38 | + const prevStep = () => setCurrentStep((prev) => Math.max(prev - 1, 0)); |
| 39 | + |
| 40 | + const onSubmit = async (data: AssetFormData) => { |
| 41 | + try { |
| 42 | + await createAsset(data); |
| 43 | + toast.success("Asset created successfully!"); |
| 44 | + clearStorage("assetForm"); |
| 45 | + methods.reset(); |
| 46 | + setCurrentStep(0); |
| 47 | + } catch (error: any) { |
| 48 | + toast.error(error.message || "Failed to create asset"); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + return ( |
| 53 | + <div className="max-w-2xl mx-auto p-4"> |
| 54 | + <ProgressBar step={currentStep + 1} total={steps.length} /> |
| 55 | + <FormProvider {...methods}> |
| 56 | + <form onSubmit={methods.handleSubmit(onSubmit)}> |
| 57 | + <CurrentStepComponent /> |
| 58 | + <div className="flex justify-between mt-4"> |
| 59 | + {currentStep > 0 && ( |
| 60 | + <button |
| 61 | + type="button" |
| 62 | + onClick={prevStep} |
| 63 | + className="px-4 py-2 bg-gray-200 rounded hover:bg-gray-300" |
| 64 | + > |
| 65 | + Back |
| 66 | + </button> |
| 67 | + )} |
| 68 | + {currentStep < steps.length - 1 ? ( |
| 69 | + <button |
| 70 | + type="button" |
| 71 | + onClick={nextStep} |
| 72 | + className="ml-auto px-4 py-2 bg-emerald-500 text-white rounded hover:bg-emerald-600" |
| 73 | + > |
| 74 | + Next |
| 75 | + </button> |
| 76 | + ) : ( |
| 77 | + <button |
| 78 | + type="submit" |
| 79 | + className="ml-auto px-4 py-2 bg-emerald-500 text-white rounded hover:bg-emerald-600" |
| 80 | + > |
| 81 | + Submit |
| 82 | + </button> |
| 83 | + )} |
| 84 | + </div> |
| 85 | + </form> |
| 86 | + </FormProvider> |
| 87 | + </div> |
| 88 | + ); |
| 89 | +}; |
| 90 | + |
| 91 | +export default AssetFormPage; |
0 commit comments