All files / src/components/form/XMultiForm XMultiForm.tsx

0% Statements 0/20
0% Branches 0/10
0% Functions 0/9
0% Lines 0/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77                                                                                                                                                         
import React, { useState } from "react";
import { XForm, XFormInterface } from "../XForm/XForm";
// import { Progress } from "@/components/ui/progress";
import { ProgressBar } from "../Progress/Progress";
import { FormValues } from "@/apps/user/pages/agents/NewAgent";
import { AxiosResponse } from "axios";
 
export interface XMultiFormInterface {
  forms: XFormInterface[];
  values?: Record<string, Record<string, unknown>>;
  post_callable: (data: any) => void;
  redirect_to?: string;
}
 
export const XMultiForm: React.FC<XMultiFormInterface> = (
  props: XMultiFormInterface
) => {
  const [formData, setFormData] = useState<Record<string, FormValues>>(
    props.values ?? {}
  );
  const [stepIdx, setStepIdx] = useState(0);
  const handleNext = () => setStepIdx((prev) => prev + 1);
  const handleBack = () => setStepIdx((prev) => prev - 1);
 
  // 2. Define a submit handler.
  function onSubmit(values: FormValues) {
    // Do something with the form values.
    // ✅ This will be type-safe and validated.
    // console.log(values);
    // console.log("HIIII");
    formData[stepIdx] = values;
    props.forms[stepIdx].onSubmit(values);
    // console.log(formData);
 
    if (stepIdx === props.forms.length - 1) {
      // const text = await api_handler.project_router.new();
      const flat_vals = Object.entries(formData)
        .map(([, data]) => {
          return data as Record<string, unknown>;
        })
        .flat();
      props.post_callable(Object.assign({}, ...flat_vals));
      if (props.redirect_to !== undefined) {
        window.location.pathname = props.redirect_to;
      }
    }
  }
 
  return (
    <div className="flex flex-col gap-6 rounded-lg">
      {/* <div className="flex flex-row gap-2 items-center"> */}
      {/* <Progress
          value={(100 * (stepIdx + 1)) / props.forms.length}
          className="h-2"
        /> */}
      <ProgressBar
        steps={props.forms.map((form) => {
          return form.title;
        })}
        currentStep={stepIdx}
      />
      {/* <h3 className="text-xl bg-black p-2 text-white rounded-2xl">
          {stepIdx + 1}/{props.forms.length}
        </h3>
      </div> */}
      <XForm
        key={stepIdx}
        {...props.forms[stepIdx]}
        goBack={stepIdx > 0 ? handleBack : undefined}
        goForward={stepIdx < props.forms.length - 1 ? handleNext : undefined}
        values={formData[stepIdx]}
        onSubmit={(values) => onSubmit(values)}
      />
    </div>
  );
};