api requests
This commit is contained in:
parent
fe74490440
commit
5a1cc7c43c
22 changed files with 665 additions and 133 deletions
|
|
@ -1,13 +1,73 @@
|
|||
import React from "react";
|
||||
import React, {useState} from "react";
|
||||
import SurveyInfo from "../SurveyInfo/SurveyInfo.tsx";
|
||||
import QuestionsList from "../QuestionsList/QuestionsList.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, setSurvey] = 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 () => {
|
||||
const savedSurvey = await postNewSurvey({title: titleSurvey, description: descriptionSurvey});
|
||||
setSurvey(savedSurvey);
|
||||
|
||||
try {
|
||||
await Promise.all(
|
||||
questions.map(question =>
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={styles.survey}>
|
||||
<SurveyInfo />
|
||||
<QuestionsList />
|
||||
<SurveyInfo
|
||||
titleSurvey={titleSurvey}
|
||||
descriptionSurvey={descriptionSurvey}
|
||||
setDescriptionSurvey={setDescriptionSurvey}
|
||||
setTitleSurvey={setTitleSurvey}
|
||||
/>
|
||||
<QuestionsList
|
||||
questions={questions}
|
||||
setQuestions={setQuestions}
|
||||
surveyId={survey?.id}
|
||||
/>
|
||||
|
||||
<SaveButton onClick={handleSave}/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue