fix survey id
This commit is contained in:
parent
5a1cc7c43c
commit
8622f9e9db
4 changed files with 40 additions and 30 deletions
|
|
@ -7,7 +7,7 @@ import {MySurveysPage} from "./pages/MySurveysPage/MySurveysPage.tsx";
|
||||||
import {Results} from "./components/Results/Results.tsx";
|
import {Results} from "./components/Results/Results.tsx";
|
||||||
import {MySurveyList} from "./components/MySurveyList/MySurveyList.tsx";
|
import {MySurveyList} from "./components/MySurveyList/MySurveyList.tsx";
|
||||||
import AuthForm from "./pages/AuthForm/AuthForm.tsx";
|
import AuthForm from "./pages/AuthForm/AuthForm.tsx";
|
||||||
// import {SurveyPage} from "./components/SurveyPage/SurveyPage.tsx";
|
import {SurveyPage} from "./components/SurveyPage/SurveyPage.tsx";
|
||||||
|
|
||||||
const App = () => {
|
const App = () => {
|
||||||
return(
|
return(
|
||||||
|
|
@ -26,7 +26,7 @@ const App = () => {
|
||||||
</Route>
|
</Route>
|
||||||
|
|
||||||
<Route path='survey/:surveyId' element={<SurveyCreateAndEditingPage />}>
|
<Route path='survey/:surveyId' element={<SurveyCreateAndEditingPage />}>
|
||||||
<Route path="questions" element={<Survey />} />
|
<Route path="questions" element={<SurveyPage />} />
|
||||||
<Route path="settings" element={<SettingSurvey />} />
|
<Route path="settings" element={<SettingSurvey />} />
|
||||||
<Route path="results" element={<Results />} />
|
<Route path="results" element={<Results />} />
|
||||||
</Route>
|
</Route>
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ export const MySurveyList = () => {
|
||||||
}, [navigate]);
|
}, [navigate]);
|
||||||
|
|
||||||
const handleSurveyClick = (surveyId: number) => {
|
const handleSurveyClick = (surveyId: number) => {
|
||||||
navigate(`/survey/${surveyId}/questions`)
|
navigate(`/survey/${surveyId}/questions`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleDeleteClick = async (id: number, e: React.MouseEvent) => {
|
const handleDeleteClick = async (id: number, e: React.MouseEvent) => {
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ const Survey: React.FC = () => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const [descriptionSurvey, setDescriptionSurvey] = useState('');
|
const [descriptionSurvey, setDescriptionSurvey] = useState('');
|
||||||
const [titleSurvey, setTitleSurvey] = useState('Название опроса');
|
const [titleSurvey, setTitleSurvey] = useState('Название опроса');
|
||||||
const [survey, setSurvey] = useState<ISurvey | null>(null);
|
const [survey] = useState<ISurvey | null>(null);
|
||||||
|
|
||||||
const [questions, setQuestions] = useState<Question[]>([
|
const [questions, setQuestions] = useState<Question[]>([
|
||||||
{ id: 1, text: '', questionType: 'singleanswerquestion'},
|
{ id: 1, text: '', questionType: 'singleanswerquestion'},
|
||||||
|
|
@ -34,22 +34,26 @@ const Survey: React.FC = () => {
|
||||||
|
|
||||||
|
|
||||||
const handleSave = async () => {
|
const handleSave = async () => {
|
||||||
const savedSurvey = await postNewSurvey({title: titleSurvey, description: descriptionSurvey});
|
|
||||||
setSurvey(savedSurvey);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const savedSurvey = await postNewSurvey({
|
||||||
|
title: titleSurvey,
|
||||||
|
description: descriptionSurvey
|
||||||
|
});
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
questions.map(question =>
|
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');
|
navigate('/my-surveys');
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Ошибка при сохранении:', error);
|
console.error('Ошибка при сохранении:', error);
|
||||||
|
alert('Не удалось сохранить опрос');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,42 +1,48 @@
|
||||||
import React, { useEffect, useState } from 'react';
|
import SurveyInfo from "../SurveyInfo/SurveyInfo.tsx";
|
||||||
import { useParams } from 'react-router-dom';
|
import QuestionsList, {Question} from "../QuestionsList/QuestionsList.tsx";
|
||||||
import SurveyInfo from '../SurveyInfo/SurveyInfo.tsx';
|
import {useEffect, useState} from "react";
|
||||||
import QuestionsList, {Question} from '../QuestionsList/QuestionsList.tsx';
|
|
||||||
import {getSurveyById, ISurvey} from "../../api/SurveyApi.ts";
|
import {getSurveyById, ISurvey} from "../../api/SurveyApi.ts";
|
||||||
|
import {useParams} from "react-router-dom";
|
||||||
import {getListQuestions} from "../../api/QuestionApi.ts";
|
import {getListQuestions} from "../../api/QuestionApi.ts";
|
||||||
|
|
||||||
export const SurveyPage: React.FC = () => {
|
export const SurveyPage: React.FC = () => {
|
||||||
// const navigate = useNavigate();
|
|
||||||
const [survey, setSurvey] = useState<ISurvey | null>(null);
|
const [survey, setSurvey] = useState<ISurvey | null>(null);
|
||||||
const [questions, setQuestions] = useState<Question[]>([]);
|
const [questions, setQuestions] = useState<Question[]>([]);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const { id: surveyId } = useParams<{ id: string }>(); // Переименовываем для ясности
|
const { surveyId } = useParams<{ surveyId: string }>(); // Изменили параметр на surveyId
|
||||||
console.log(surveyId);
|
|
||||||
|
|
||||||
useEffect(() => {
|
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');
|
console.error('Invalid survey ID');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const fetchData = async () => {
|
const fetchData = async () => {
|
||||||
try {
|
try {
|
||||||
// Получаем опрос по surveyId
|
setLoading(true);
|
||||||
const surveyData = await getSurveyById(Number(surveyId));
|
const surveyData = await getSurveyById(id);
|
||||||
setSurvey(surveyData);
|
setSurvey(surveyData);
|
||||||
|
|
||||||
// Получаем вопросы опроса по surveyId
|
const questionsData = await getListQuestions(id);
|
||||||
const questionsData = await getListQuestions(Number(surveyId));
|
|
||||||
const formattedQuestions = questionsData.map(q => ({
|
const formattedQuestions = questionsData.map(q => ({
|
||||||
id: q.id, // ID вопроса
|
id: q.id,
|
||||||
text: q.title,
|
text: q.title,
|
||||||
questionType: q.questionType,
|
questionType: q.questionType as 'singleanswerquestion' | 'multipleanswerquestion',
|
||||||
}));
|
}));
|
||||||
setQuestions(formattedQuestions as Question[]);
|
setQuestions(formattedQuestions);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Ошибка:', error);
|
console.error('Ошибка:', error);
|
||||||
// navigate('/my-surveys');
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
fetchData();
|
fetchData();
|
||||||
}, [surveyId]);
|
}, [surveyId]);
|
||||||
|
|
||||||
|
|
@ -53,7 +59,7 @@ export const SurveyPage: React.FC = () => {
|
||||||
/>
|
/>
|
||||||
<QuestionsList
|
<QuestionsList
|
||||||
questions={questions}
|
questions={questions}
|
||||||
setQuestions={() => {}}
|
setQuestions={setQuestions}
|
||||||
surveyId={survey.id}
|
surveyId={survey.id}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue