(
+ tempSurvey.questions.length > 0
+ ? tempSurvey.questions
+ : [{ id: 1, text: '', questionType: 'SingleAnswerQuestion', answerVariants: [{ text: '' }] }]
+ );
+
+ useEffect(() => {
+ if (!tempSurvey.title && !tempSurvey.description && tempSurvey.questions.length === 0) {
+ setTempSurvey({
+ title: "Название опроса",
+ description: "",
+ questions: [{ id: 1, text: '', questionType: 'SingleAnswerQuestion', answerVariants: [{ text: '' }] }]
+ });
+ setQuestions([{ id: 1, text: '', questionType: 'SingleAnswerQuestion', answerVariants: [{ text: '' }] }]);
+ }
+ }, []);
+
+ useEffect(() => {
+ setTempSurvey({
+ ...tempSurvey,
+ questions: questions,
+ });
+ }, [questions]);
+
+ useEffect(() => {
+ const isCreateSurveyRoute = location.pathname.includes('/survey/create');
+
+ if (!isCreateSurveyRoute) {
+ clearTempSurvey();
+ setQuestions([{ id: 1, text: '', questionType: 'SingleAnswerQuestion', answerVariants: [{ text: '' }] }]);
+ }
+ }, [location.pathname]);
const handleSave = async () => {
try {
const savedSurvey = await postNewSurvey({
- title: titleSurvey,
- description: descriptionSurvey
+ title: tempSurvey.title,
+ description: tempSurvey.description,
});
- const updatedQuestions: Question[] = [];
- for (const question of questions) {
+ const questionPromises = questions.map(async (question) => {
const newQuestion = await addNewQuestion(savedSurvey.id, {
title: question.text,
- questionType: question.questionType
+ questionType: question.questionType,
});
- const updatedQuestion: Question = {
- ...question,
- id: newQuestion.id,
- answerVariants: []
- };
-
- if (question.answerVariants && question.answerVariants.length > 0) {
- const newVariants = await Promise.all(
+ if (question.answerVariants?.length > 0) {
+ await Promise.all(
question.answerVariants.map(answer =>
- addNewAnswerVariant(
- savedSurvey.id,
- newQuestion.id,
- { text: answer.text }
- )
+ addNewAnswerVariant(savedSurvey.id, newQuestion.id, { text: answer.text })
)
);
-
- updatedQuestion.answerVariants = newVariants.map((variant: { id: number, text: string }) => ({
- id: variant.id,
- text: variant.text
- }));
}
+ });
- updatedQuestions.push(updatedQuestion);
- }
+ await Promise.all(questionPromises);
- setQuestions(updatedQuestions);
+ clearTempSurvey();
navigate('/my-surveys');
} catch (error) {
console.error('Ошибка при сохранении:', error);
@@ -68,20 +82,19 @@ const Survey: React.FC = () => {
return (
setTempSurvey({ ...tempSurvey, description: value })}
+ setTitleSurvey={(value) => setTempSurvey({ ...tempSurvey, title: value })}
/>
-
-
+
);
-}
+};
export default Survey;
\ No newline at end of file
diff --git a/SurveyFrontend/src/context/SurveyContext.tsx b/SurveyFrontend/src/context/SurveyContext.tsx
new file mode 100644
index 0000000..a3f8437
--- /dev/null
+++ b/SurveyFrontend/src/context/SurveyContext.tsx
@@ -0,0 +1,37 @@
+import React, {createContext, useContext, useState} from "react";
+import { INewSurvey } from "../api/SurveyApi.ts";
+import { Question } from "../components/QuestionsList/QuestionsList.tsx";
+
+interface SurveyContextType {
+ tempSurvey: INewSurvey & { questions: Question[] };
+ setTempSurvey: (survey: INewSurvey & { questions: Question[] }) => void;
+ clearTempSurvey: () => void;
+}
+
+const SurveyContext = createContext(undefined);
+
+export const SurveyProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
+ const [tempSurvey, setTempSurvey] = useState({
+ title: "",
+ description: "",
+ questions: [],
+ });
+
+ const clearTempSurvey = () => {
+ setTempSurvey({ title: "", description: "", questions: [] });
+ };
+
+ return (
+
+ {children}
+
+ );
+};
+
+export const useSurveyContext = () => {
+ const context = useContext(SurveyContext);
+ if (!context) {
+ throw new Error("useSurveyContext must be used within a SurveyProvider");
+ }
+ return context;
+};
\ No newline at end of file