fix size components
This commit is contained in:
parent
08f827267d
commit
e6e2f68eb0
32 changed files with 238 additions and 175 deletions
|
|
@ -2,12 +2,16 @@ import SurveyInfo from "../SurveyInfo/SurveyInfo.tsx";
|
|||
import QuestionsList, {Question} from "../QuestionsList/QuestionsList.tsx";
|
||||
import {useEffect, useState} from "react";
|
||||
import styles from './CompletingSurvey.module.css'
|
||||
import { useParams} from "react-router-dom";
|
||||
import {useNavigate, useParams} from "react-router-dom";
|
||||
import {getSurveyById, ISurvey} from "../../api/SurveyApi.ts";
|
||||
import {getListQuestions} from "../../api/QuestionApi.ts";
|
||||
import {getAnswerVariants, IAnswerVariant} from "../../api/AnswerVariantsApi.ts";
|
||||
import {addNewCompletion} from "../../api/CompletionApi.ts";
|
||||
|
||||
|
||||
interface ISelectedAnswers{
|
||||
questionId: number;
|
||||
answerText: string;
|
||||
}
|
||||
|
||||
export const CompletingSurvey = () => {
|
||||
const {surveyId} = useParams<{surveyId: string}>();
|
||||
|
|
@ -16,6 +20,8 @@ export const CompletingSurvey = () => {
|
|||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const [selectedAnswers, setSelectedAnswers] = useState<ISelectedAnswers[]>([]);
|
||||
const navigate = useNavigate();
|
||||
|
||||
useEffect(() => {
|
||||
const fetchSurveyData = async () => {
|
||||
|
|
@ -51,6 +57,43 @@ export const CompletingSurvey = () => {
|
|||
fetchSurveyData();
|
||||
}, [surveyId]);
|
||||
|
||||
const handleAnswerSelect = (questionId: number, answerText: string) => {
|
||||
setSelectedAnswers(prev => {
|
||||
const question = questions.find(q => q.id === questionId);
|
||||
if (question?.questionType === 'SingleAnswerQuestion') {
|
||||
return [
|
||||
...prev.filter(a => a.questionId !== questionId),
|
||||
{ questionId, answerText }
|
||||
];
|
||||
}
|
||||
|
||||
const existingAnswerIndex = prev.findIndex(
|
||||
a => a.questionId === questionId && a.answerText === answerText
|
||||
);
|
||||
|
||||
if (existingAnswerIndex >= 0) {
|
||||
return prev.filter((_, index) => index !== existingAnswerIndex);
|
||||
} else {
|
||||
return [...prev, { questionId, answerText }];
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const handleSubmit = async () => {
|
||||
if (!surveyId) return;
|
||||
|
||||
try {
|
||||
await addNewCompletion(parseInt(surveyId), {
|
||||
answers: selectedAnswers
|
||||
});
|
||||
|
||||
navigate('/surveys');
|
||||
} catch (error) {
|
||||
console.error('Ошибка при отправке ответов:', error);
|
||||
alert('Произошла ошибка при отправке ответов');
|
||||
}
|
||||
};
|
||||
|
||||
if (loading) return <div>Загрузка...</div>;
|
||||
if (error) return <div>{error}</div>;
|
||||
if (!survey) return <div>Опрос не найден</div>;
|
||||
|
|
@ -67,8 +110,9 @@ export const CompletingSurvey = () => {
|
|||
<QuestionsList
|
||||
questions={questions}
|
||||
setQuestions={setQuestions}
|
||||
onAnswerSelect={handleAnswerSelect}
|
||||
/>
|
||||
<button className={styles.departur_button}>Отправить</button>
|
||||
<button className={styles.departur_button} onClick={handleSubmit} disabled={selectedAnswers.length === 0}>Отправить</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue