rentgen/components/report-window/use-survey.ts

30 lines
1.0 KiB
TypeScript
Raw Permalink Normal View History

import * as React from 'react';
import * as Survey from 'survey-react';
2022-07-07 19:59:16 +02:00
import { RequestCluster } from '../../request-cluster';
import generateSurveyQuestions from './generate-survey-questions';
import RawAnswers from './raw-answers';
import verbs, { v } from './verbs';
export default function useSurvey(
2022-07-07 19:59:16 +02:00
clusters: RequestCluster[],
{ onComplete }: { onComplete: (sender: { data: RawAnswers }) => void }
2022-07-09 15:28:37 +02:00
): Survey.ReactSurveyModel | null {
const [survey, setSurvey] = React.useState<Survey.Model | null>(null);
React.useEffect(() => {
2022-07-07 19:59:16 +02:00
const model = generateSurveyQuestions(clusters);
const survey = new Survey.Model(model);
survey.onProcessTextValue.add(function (
sender: Survey.SurveyModel,
options: { name: string; value?: string }
) {
if (verbs[options.name.toLowerCase()]) {
options.value = v(options.name, sender.valuesHash.zaimek);
}
});
survey.onComplete.add(onComplete);
setSurvey(survey);
}, []);
return survey;
}