Files
profi-frontend/src/pages/SurveyPage.tsx

110 lines
3.2 KiB
TypeScript
Raw Normal View History

2025-05-31 10:57:27 +03:00
import {LoadingList} from "../components/LoadingList.tsx";
2025-05-31 18:15:52 +03:00
import {AgreementQuestion, Question, QuestionPair, QuestionType, Survey} from "../types/survey.ts";
2025-05-31 10:57:27 +03:00
import {Link, useParams} from "react-router-dom";
2025-05-31 18:15:52 +03:00
import React, { useState } from "react";
2025-05-31 10:57:27 +03:00
import {useData, useDataPage} from "../API/common.ts";
import {LoadingData} from "../components/LoadingData.tsx";
export function SurveyPage() {
const {surveyId} = useParams();
const survey = useData<Survey>("surveys", "surveys", surveyId);
return (
<div className="survey-page">
<LoadingData data={survey}>
<h1>
{survey?.title}
</h1>
<div>
{survey?.description}
</div>
<SurveyQuestions survey={survey}/>
</LoadingData>
</div>
);
}
function SurveyQuestions({survey}: { survey: Survey | null | undefined }) {
const _survey = survey as Survey;
2025-05-31 18:15:52 +03:00
const [questions, setQuestions] = useState<Question[]|null>(null);
2025-05-31 10:57:27 +03:00
2025-05-31 18:15:52 +03:00
const questionsPairs = useDataPage<QuestionPair>("surveys", "question-pairs", {survey:_survey.id});
const questionsAgreement = useDataPage<AgreementQuestion>("surveys", "agreement-questions", {survey:_survey.id});
2025-05-31 10:57:27 +03:00
2025-05-31 18:15:52 +03:00
if (questions === null && questionsPairs.items !== null && questionsAgreement.items !== null){
function setQuestionType(question: Question, questionType: QuestionType) {
question.questionType = questionType;
return question;
}
var _questions = [...questionsPairs.items.map(q=>setQuestionType(q, QuestionType.QuestionPair)), ...questionsAgreement.items.map(q=>setQuestionType(q, QuestionType.AgreementQuestion))];
_questions.sort(()=>Math.random() - .5);
console.log(questionsPairs.items.map(q=>setQuestionType(q, QuestionType.QuestionPair)));
setQuestions(_questions);
}
//console.log(questions);
2025-05-31 10:57:27 +03:00
return (
<>
2025-05-31 18:15:52 +03:00
<LoadingList data={questions} listElement={(question=>(
<QuestionBlock key={(question as Question).questionType+"#"+question.id} question={question as Question}/>
2025-05-31 10:57:27 +03:00
2025-05-31 18:15:52 +03:00
))}/>
2025-05-31 10:57:27 +03:00
</>
);
}
2025-05-31 18:15:52 +03:00
function QuestionBlock({question}:{question: Question}) {
switch (question.questionType) {
case QuestionType.AgreementQuestion:
const qa = question as AgreementQuestion;
return (
<div className={"question agreement"}>
(Вы согласны с утверждением?)
{qa.question}
<button>
Согласен
</button>
<button>
Несогласен
</button>
</div>
);
case QuestionType.QuestionPair:
const qp = question as QuestionPair;
return (
<div className={"question pair"}>
(Выберите подходящий ответ)
{qp.statements.map(s=>(<button>{s.text}</button>))}
</div>
);
default:
break;
}
return null;
}
2025-05-31 10:57:27 +03:00