fix survey id

This commit is contained in:
Tatiana Nikolaeva 2025-05-24 11:48:55 +05:00
parent 5a1cc7c43c
commit 8622f9e9db
4 changed files with 40 additions and 30 deletions

View file

@ -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) => {

View file

@ -11,7 +11,7 @@ const Survey: React.FC = () => {
const navigate = useNavigate();
const [descriptionSurvey, setDescriptionSurvey] = useState('');
const [titleSurvey, setTitleSurvey] = useState('Название опроса');
const [survey, setSurvey] = useState<ISurvey | null>(null);
const [survey] = useState<ISurvey | null>(null);
const [questions, setQuestions] = useState<Question[]>([
{ 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('Не удалось сохранить опрос');
}
};

View file

@ -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>