diff --git a/SurveyFrontend/src/App.tsx b/SurveyFrontend/src/App.tsx index cdc3005..8838013 100644 --- a/SurveyFrontend/src/App.tsx +++ b/SurveyFrontend/src/App.tsx @@ -7,7 +7,7 @@ import {MySurveysPage} from "./pages/MySurveysPage/MySurveysPage.tsx"; import {Results} from "./components/Results/Results.tsx"; import {MySurveyList} from "./components/MySurveyList/MySurveyList.tsx"; import AuthForm from "./pages/AuthForm/AuthForm.tsx"; -// import {SurveyPage} from "./components/SurveyPage/SurveyPage.tsx"; +import {SurveyPage} from "./components/SurveyPage/SurveyPage.tsx"; const App = () => { return( @@ -26,7 +26,7 @@ const App = () => { }> - } /> + } /> } /> } /> diff --git a/SurveyFrontend/src/components/MySurveyList/MySurveyList.tsx b/SurveyFrontend/src/components/MySurveyList/MySurveyList.tsx index 2694f4c..20c2f0d 100644 --- a/SurveyFrontend/src/components/MySurveyList/MySurveyList.tsx +++ b/SurveyFrontend/src/components/MySurveyList/MySurveyList.tsx @@ -37,7 +37,7 @@ export const MySurveyList = () => { }, [navigate]); const handleSurveyClick = (surveyId: number) => { - navigate(`/survey/${surveyId}/questions`) + navigate(`/survey/${surveyId}/questions`); } const handleDeleteClick = async (id: number, e: React.MouseEvent) => { diff --git a/SurveyFrontend/src/components/Survey/Survey.tsx b/SurveyFrontend/src/components/Survey/Survey.tsx index 8d9f5d8..3874aa6 100644 --- a/SurveyFrontend/src/components/Survey/Survey.tsx +++ b/SurveyFrontend/src/components/Survey/Survey.tsx @@ -11,7 +11,7 @@ const Survey: React.FC = () => { const navigate = useNavigate(); const [descriptionSurvey, setDescriptionSurvey] = useState(''); const [titleSurvey, setTitleSurvey] = useState('Название опроса'); - const [survey, setSurvey] = useState(null); + const [survey] = useState(null); const [questions, setQuestions] = useState([ { id: 1, text: '', questionType: 'singleanswerquestion'}, @@ -34,22 +34,26 @@ const Survey: React.FC = () => { const handleSave = async () => { - const savedSurvey = await postNewSurvey({title: titleSurvey, description: descriptionSurvey}); - setSurvey(savedSurvey); - try { + const savedSurvey = await postNewSurvey({ + title: titleSurvey, + description: descriptionSurvey + }); + await Promise.all( questions.map(question => - addNewQuestion(savedSurvey.id, {title: question.text, questionType: question.questionType}) - )) + addNewQuestion(savedSurvey.id, { + title: question.text, + questionType: question.questionType + }) + ) + ); - // Сбрасываем состояние после сохранения - setQuestions([{ id: 1, text: '', questionType: 'singleanswerquestion' }]); - setTitleSurvey('Новый опрос'); - setDescriptionSurvey(''); navigate('/my-surveys'); + } catch (error) { console.error('Ошибка при сохранении:', error); + alert('Не удалось сохранить опрос'); } }; diff --git a/SurveyFrontend/src/components/SurveyPage/SurveyPage.tsx b/SurveyFrontend/src/components/SurveyPage/SurveyPage.tsx index 96bbc55..d9ea682 100644 --- a/SurveyFrontend/src/components/SurveyPage/SurveyPage.tsx +++ b/SurveyFrontend/src/components/SurveyPage/SurveyPage.tsx @@ -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(null); const [questions, setQuestions] = useState([]); 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 = () => { /> {}} + setQuestions={setQuestions} surveyId={survey.id} />