2022-02-07 21:11:25 +01:00
|
|
|
import * as React from 'react';
|
|
|
|
import * as Survey from 'survey-react';
|
|
|
|
import generateSurveyQuestions from './generate-survey-questions';
|
|
|
|
import RawAnswers from './raw-answers';
|
2022-02-07 21:17:44 +01:00
|
|
|
import verbs, { v } from './verbs';
|
2022-02-07 21:11:25 +01:00
|
|
|
|
|
|
|
export default function useSurvey(
|
|
|
|
hosts: string[],
|
|
|
|
{ onComplete }: { onComplete: (sender: { data: RawAnswers }) => void }
|
|
|
|
): Survey.ReactSurveyModel {
|
|
|
|
const [survey, setSurvey] = React.useState<Survey.Model>(null);
|
|
|
|
React.useEffect(() => {
|
|
|
|
const model = generateSurveyQuestions(hosts);
|
2022-02-08 22:27:12 +01:00
|
|
|
console.log(model);
|
2022-02-07 21:11:25 +01:00
|
|
|
const survey = new Survey.Model(model);
|
|
|
|
survey.onProcessTextValue.add(function (
|
|
|
|
sender: Survey.SurveyModel,
|
|
|
|
options: { name: string; value?: string }
|
|
|
|
) {
|
|
|
|
if (verbs[options.name.toLowerCase()]) {
|
2022-02-07 21:17:44 +01:00
|
|
|
options.value = v(options.name, sender.valuesHash.zaimek);
|
2022-02-07 21:11:25 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
survey.onComplete.add(onComplete);
|
|
|
|
setSurvey(survey);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return survey;
|
|
|
|
}
|