{"version":3,"file":"useFollowup.cjs","sources":["../../../../../src/components/feedback/followup/useFollowup.ts"],"sourcesContent":["import {\n    type Dispatch,\n    type FormEvent,\n    type SetStateAction,\n    useCallback,\n    useEffect,\n    useRef,\n    useState,\n} from \"react\";\nimport type {\n    FeedbackAnswer,\n    FeedbackOption,\n    FollowupQuestion,\n} from \"../types.js\";\n\ntype CurrentValue =\n    | FeedbackOption<string | number>\n    | FeedbackOption<string | number>[]\n    | undefined;\n\ntype Followup = {\n    questions: FollowupQuestion[];\n    values: FeedbackAnswer[] | undefined;\n    step: {\n        number: number;\n        question: FollowupQuestion;\n        isLast: boolean;\n    };\n    currentValue: CurrentValue;\n    setCurrentValue: Dispatch<SetStateAction<CurrentValue>>;\n    submitted: boolean;\n    handleNext: () => void;\n    handleAbort: () => void;\n};\n\nexport const useFollowup = (\n    questions: FollowupQuestion[],\n    onSubmit: (a: FeedbackAnswer[]) => void,\n): Followup => {\n    const [values, setValues] = useState<FeedbackAnswer[]>();\n    const [step, setStep] = useState({\n        number: 0,\n        question: questions[0],\n        isLast: questions.length === 1,\n    });\n    const [shouldSubmit, setShouldSubmit] = useState(false);\n    const [submitted, setSubmitted] = useState(false);\n    const [currentValue, setCurrentValue] = useState<\n        FeedbackOption | FeedbackOption[]\n    >();\n\n    // Store info in a ref to facilitate autosubmit on component unmount,\n    // or when leaving page\n    const followupRef = useRef({\n        values,\n        submitted,\n        onSubmit,\n    });\n\n    // Keep values in ref updated\n    useEffect(() => {\n        followupRef.current = {\n            ...followupRef.current,\n            values,\n            submitted,\n        };\n    }, [values, submitted]);\n\n    // General method for submitting info\n    const _handleSubmit = useCallback(() => {\n        if (\n            !followupRef.current.submitted &&\n            followupRef.current.values !== undefined\n        ) {\n            followupRef.current.onSubmit(followupRef.current.values);\n        }\n    }, []);\n\n    // Function for handling going to the next step in the form wizard\n    function handleNext(e?: FormEvent<HTMLFormElement>) {\n        e?.preventDefault();\n\n        const value = Array.isArray(currentValue)\n            ? currentValue.map((option) => option.value.toString())\n            : currentValue?.value;\n\n        const newValue = {\n            ...step.question,\n            name: step.question.name || step.question.label,\n            value,\n        };\n\n        setValues((oldValues) => {\n            const filteredValues =\n                oldValues?.filter(\n                    (oldValue) => oldValue.name !== newValue.name,\n                ) || [];\n            return [...filteredValues, newValue as FeedbackAnswer];\n        });\n        setCurrentValue(undefined);\n\n        if (step.isLast) {\n            setShouldSubmit(true);\n        } else {\n            setStep((currentStep) => {\n                const newStepNum = currentStep.number + 1;\n                return {\n                    number: newStepNum,\n                    question: questions[newStepNum],\n                    isLast: newStepNum + 1 >= questions.length,\n                };\n            });\n        }\n    }\n\n    // Let the user abort without submitting answers\n    function handleAbort() {\n        setValues(undefined);\n        setCurrentValue(undefined);\n        setStep({\n            number: 0,\n            question: questions[0],\n            isLast: questions.length === 1,\n        });\n        setSubmitted(true);\n    }\n\n    // Submit info after user submits last question\n    useEffect(() => {\n        if (shouldSubmit) {\n            _handleSubmit();\n            setSubmitted(true);\n        }\n    }, [shouldSubmit, _handleSubmit]);\n\n    // Submit info if component unmounts or page unloads\n    useEffect(() => {\n        if (typeof window !== \"undefined\") {\n            window.addEventListener(\"beforeunload\", _handleSubmit);\n        }\n        return () => {\n            _handleSubmit();\n            window.removeEventListener(\"beforeunload\", _handleSubmit);\n        };\n    }, [_handleSubmit]);\n\n    return {\n        questions,\n        values,\n        step,\n        currentValue,\n        setCurrentValue,\n        submitted,\n        handleNext,\n        handleAbort,\n    };\n};\n"],"names":["questions","onSubmit","values","setValues","useState","step","setStep","number","question","isLast","length","shouldSubmit","setShouldSubmit","submitted","setSubmitted","currentValue","setCurrentValue","followupRef","useRef","useEffect","current","_handleSubmit","useCallback","window","addEventListener","removeEventListener","handleNext","e","preventDefault","value","Array","isArray","map","option","toString","newValue","name","label","oldValues","filter","oldValue","currentStep","newStepNum","handleAbort"],"mappings":"6HAmC2B,CACvBA,EACAC,KAEA,MAAOC,EAAQC,GAAaC,cACrBC,EAAMC,GAAWF,WAAS,CAC7BG,OAA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