import React, { useState } from "react"; import { Form, Field } from "react-final-form"; import Slider from "@mui/material/Slider"; import { useLazySendTestVersionQuery } from "../store/api/chatApi"; import { LuLoader2 } from "react-icons/lu"; import { Link } from "react-router-dom"; interface FormValues { age?: number; height?: number; weight?: number; healthGoal?: string; dietType?: string; exerciseLevel?: string; hydrationGoal?: string; userInput?: string; } const MultiStepForm: React.FC = () => { const [formValues, setFormValues] = useState({}); const [stage, setStage] = useState(1); const [data, setData] = useState(null) const [sendTestMessage, { isLoading, isFetching }] = useLazySendTestVersionQuery() const nextStage = () => setStage((prev) => prev + 1); const previousStage = () => setStage((prev) => prev - 1); const saveFormData = (values: FormValues) => { setFormValues((prev) => ({ ...prev, ...values, })); }; const onSubmit = (values: FormValues) => { saveFormData(values); nextStage(); }; console.log(isLoading) const finalSubmit = async () => { const res = await sendTestMessage(formValues).unwrap() setData(res) }; const selectEmoji = ( value: number | undefined, thresholds: number[], emojis: string[] ) => { if (value === undefined) return null; if (value <= thresholds[0]) return emojis[0]; if (value <= thresholds[1]) return emojis[1]; return emojis[2]; }; return !data ? (

Fill in your profile and get some advices

onSubmit={onSubmit} initialValues={formValues} render={({ handleSubmit, values }) => (
{stage === 1 && (<>

Stage 1: Base information

{selectEmoji(values.age, [17, 50], ["👶", "🧑", "👴"])}
(value === "" ? 0 : Number(value))} > {({ input }) => ( )}
{selectEmoji(values.height, [150, 175], ["🌱", "🌳", "🌲"])}
(value === "" ? 0 : Number(value))} > {({ input }) => ( )}

name="dietType" component="select" className="w-full p-3 border border-gray-300 rounded-lg text-gray-800 focus:outline-none focus:border-blue-500">
{selectEmoji(values.weight, [70, 99], ["🐭", "🐱", "🐘"])}
(value === "" ? 0 : Number(value))} > {({ input }) => (
input.onChange( Array.isArray(value) ? value[0] : value ) } min={0} max={200} className="text-indigo-500" />
Current Weight: {input.value || 0} kg
)}
)} {stage === 2 && ( <>

Stage 2: Details

name="dietType" component="select" className="w-full p-3 border border-gray-300 rounded-lg text-gray-800 focus:outline-none focus:border-blue-500">
name="exerciseLevel" component="select" className="w-full p-3 border border-gray-300 rounded-lg text-gray-800 focus:outline-none focus:border-blue-500">
name="hydrationGoal" component="select" className="w-full p-3 border border-gray-300 rounded-lg text-gray-800 focus:outline-none focus:border-blue-500">
name="userInput" component="input" type="text" placeholder="Enter your preferences or comments" className="w-full p-3 border border-gray-300 rounded-lg text-gray-800 focus:outline-none focus:border-blue-500" />
)} {stage === 3 && (

Summary

Age: {formValues.age}

Height: {formValues.height} cm

Weight: {formValues.weight} kg

Health Goal: {formValues.healthGoal}

Diet Type: {formValues.dietType || "Not specified"}

Exercise Level: {formValues.exerciseLevel || "Not specified"}

Hydration Goal: {formValues.hydrationGoal || "Not specified"}

User Input: {formValues.userInput || "Not specified"}

)}
)} />
) : (

Advices for your health

{data}

) }; export default MultiStepForm;