api requests
This commit is contained in:
parent
fe74490440
commit
5a1cc7c43c
22 changed files with 665 additions and 133 deletions
61
SurveyFrontend/src/components/SurveyPage/SurveyPage.tsx
Normal file
61
SurveyFrontend/src/components/SurveyPage/SurveyPage.tsx
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
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 {getSurveyById, ISurvey} from "../../api/SurveyApi.ts";
|
||||
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);
|
||||
|
||||
useEffect(() => {
|
||||
if (!surveyId || isNaN(Number(surveyId))) {
|
||||
console.error('Invalid survey ID');
|
||||
return;
|
||||
}
|
||||
const fetchData = async () => {
|
||||
try {
|
||||
// Получаем опрос по surveyId
|
||||
const surveyData = await getSurveyById(Number(surveyId));
|
||||
setSurvey(surveyData);
|
||||
|
||||
// Получаем вопросы опроса по surveyId
|
||||
const questionsData = await getListQuestions(Number(surveyId));
|
||||
const formattedQuestions = questionsData.map(q => ({
|
||||
id: q.id, // ID вопроса
|
||||
text: q.title,
|
||||
questionType: q.questionType,
|
||||
}));
|
||||
setQuestions(formattedQuestions as Question[]);
|
||||
} catch (error) {
|
||||
console.error('Ошибка:', error);
|
||||
// navigate('/my-surveys');
|
||||
}
|
||||
};
|
||||
fetchData();
|
||||
}, [surveyId]);
|
||||
|
||||
if (loading) return <div>Загрузка...</div>;
|
||||
if (!survey) return <div>Опрос не найден</div>;
|
||||
|
||||
return (
|
||||
<div className="survey-page">
|
||||
<SurveyInfo
|
||||
titleSurvey={survey.title}
|
||||
descriptionSurvey={survey.description}
|
||||
setDescriptionSurvey={() => {}}
|
||||
setTitleSurvey={() => {}}
|
||||
/>
|
||||
<QuestionsList
|
||||
questions={questions}
|
||||
setQuestions={() => {}}
|
||||
surveyId={survey.id}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue