fix survey id
This commit is contained in:
parent
5a1cc7c43c
commit
8622f9e9db
4 changed files with 40 additions and 30 deletions
|
|
@ -1,42 +1,48 @@
|
|||
import React, { useEffect, useState } from 'react';
|
||||
import { useParams } from 'react-router-dom';
|
||||
import SurveyInfo from '../SurveyInfo/SurveyInfo.tsx';
|
||||
import QuestionsList, {Question} from '../QuestionsList/QuestionsList.tsx';
|
||||
import SurveyInfo from "../SurveyInfo/SurveyInfo.tsx";
|
||||
import QuestionsList, {Question} from "../QuestionsList/QuestionsList.tsx";
|
||||
import {useEffect, useState} from "react";
|
||||
import {getSurveyById, ISurvey} from "../../api/SurveyApi.ts";
|
||||
import {useParams} from "react-router-dom";
|
||||
import {getListQuestions} from "../../api/QuestionApi.ts";
|
||||
|
||||
export const SurveyPage: React.FC = () => {
|
||||
// const navigate = useNavigate();
|
||||
const [survey, setSurvey] = useState<ISurvey | null>(null);
|
||||
const [questions, setQuestions] = useState<Question[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const { id: surveyId } = useParams<{ id: string }>(); // Переименовываем для ясности
|
||||
console.log(surveyId);
|
||||
const { surveyId } = useParams<{ surveyId: string }>(); // Изменили параметр на surveyId
|
||||
|
||||
useEffect(() => {
|
||||
if (!surveyId || isNaN(Number(surveyId))) {
|
||||
if (!surveyId) {
|
||||
console.error('Survey ID is missing');
|
||||
return;
|
||||
}
|
||||
|
||||
const id = parseInt(surveyId);
|
||||
if (isNaN(id)) {
|
||||
console.error('Invalid survey ID');
|
||||
return;
|
||||
}
|
||||
|
||||
const fetchData = async () => {
|
||||
try {
|
||||
// Получаем опрос по surveyId
|
||||
const surveyData = await getSurveyById(Number(surveyId));
|
||||
setLoading(true);
|
||||
const surveyData = await getSurveyById(id);
|
||||
setSurvey(surveyData);
|
||||
|
||||
// Получаем вопросы опроса по surveyId
|
||||
const questionsData = await getListQuestions(Number(surveyId));
|
||||
const questionsData = await getListQuestions(id);
|
||||
const formattedQuestions = questionsData.map(q => ({
|
||||
id: q.id, // ID вопроса
|
||||
id: q.id,
|
||||
text: q.title,
|
||||
questionType: q.questionType,
|
||||
questionType: q.questionType as 'singleanswerquestion' | 'multipleanswerquestion',
|
||||
}));
|
||||
setQuestions(formattedQuestions as Question[]);
|
||||
setQuestions(formattedQuestions);
|
||||
} catch (error) {
|
||||
console.error('Ошибка:', error);
|
||||
// navigate('/my-surveys');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchData();
|
||||
}, [surveyId]);
|
||||
|
||||
|
|
@ -53,7 +59,7 @@ export const SurveyPage: React.FC = () => {
|
|||
/>
|
||||
<QuestionsList
|
||||
questions={questions}
|
||||
setQuestions={() => {}}
|
||||
setQuestions={setQuestions}
|
||||
surveyId={survey.id}
|
||||
/>
|
||||
</div>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue