79 lines
No EOL
2.7 KiB
TypeScript
79 lines
No EOL
2.7 KiB
TypeScript
import React, {useState} from "react";
|
||
import SurveyInfo from "../SurveyInfo/SurveyInfo.tsx";
|
||
import QuestionsList, {Question} from "../QuestionsList/QuestionsList.tsx";
|
||
import styles from './Survey.module.css'
|
||
import SaveButton from "../SaveButton/SaveButton.tsx";
|
||
import {ISurvey, postNewSurvey} from "../../api/SurveyApi.ts";
|
||
import {addNewQuestion} from "../../api/QuestionApi.ts";
|
||
import {useNavigate} from "react-router-dom";
|
||
|
||
const Survey: React.FC = () => {
|
||
const navigate = useNavigate();
|
||
const [descriptionSurvey, setDescriptionSurvey] = useState('');
|
||
const [titleSurvey, setTitleSurvey] = useState('Название опроса');
|
||
const [survey] = useState<ISurvey | null>(null);
|
||
|
||
const [questions, setQuestions] = useState<Question[]>([
|
||
{ id: 1, text: '', questionType: 'singleanswerquestion'},
|
||
]);
|
||
|
||
// const handleSave = async () => {
|
||
// const savedSurvey = await postNewSurvey({title: titleSurvey, description: descriptionSurvey});
|
||
// setSurvey(savedSurvey);
|
||
// Promise.all(
|
||
// questions
|
||
// .map((question) => addNewQuestion( savedSurvey.id, {title: question.text, questionType: question.questionType })),
|
||
// )
|
||
// .then(() => {
|
||
// alert('Все удачно сохранилось');
|
||
// })
|
||
// .catch(() => {
|
||
// alert('Пиздец');
|
||
// });
|
||
// };
|
||
|
||
|
||
const handleSave = async () => {
|
||
try {
|
||
const savedSurvey = await postNewSurvey({
|
||
title: titleSurvey,
|
||
description: descriptionSurvey
|
||
});
|
||
|
||
await Promise.all(
|
||
questions.map(question =>
|
||
addNewQuestion(savedSurvey.id, {
|
||
title: question.text,
|
||
questionType: question.questionType
|
||
})
|
||
)
|
||
);
|
||
|
||
navigate('/my-surveys');
|
||
|
||
} catch (error) {
|
||
console.error('Ошибка при сохранении:', error);
|
||
alert('Не удалось сохранить опрос');
|
||
}
|
||
};
|
||
|
||
return (
|
||
<div className={styles.survey}>
|
||
<SurveyInfo
|
||
titleSurvey={titleSurvey}
|
||
descriptionSurvey={descriptionSurvey}
|
||
setDescriptionSurvey={setDescriptionSurvey}
|
||
setTitleSurvey={setTitleSurvey}
|
||
/>
|
||
<QuestionsList
|
||
questions={questions}
|
||
setQuestions={setQuestions}
|
||
surveyId={survey?.id}
|
||
/>
|
||
|
||
<SaveButton onClick={handleSave}/>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
export default Survey; |