diff --git a/SurveyFrontend/src/App.tsx b/SurveyFrontend/src/App.tsx index 7fa7945..9532ce7 100644 --- a/SurveyFrontend/src/App.tsx +++ b/SurveyFrontend/src/App.tsx @@ -9,6 +9,7 @@ import {MySurveyList} from "./components/MySurveyList/MySurveyList.tsx"; import AuthForm from "./pages/AuthForm/AuthForm.tsx"; import {SurveyPage} from "./components/SurveyPage/SurveyPage.tsx"; import CompleteSurvey from "./pages/CompleteSurvey/CompleteSurvey.tsx"; +import {SurveyProvider} from './context/SurveyContext.tsx'; const App = () => { return( @@ -18,8 +19,8 @@ const App = () => { } /> }> - } /> - } /> + } /> + } /> }> diff --git a/SurveyFrontend/src/components/SaveButton/SaveButton.tsx b/SurveyFrontend/src/components/SaveButton/SaveButton.tsx index 2f8481a..dc33adb 100644 --- a/SurveyFrontend/src/components/SaveButton/SaveButton.tsx +++ b/SurveyFrontend/src/components/SaveButton/SaveButton.tsx @@ -7,7 +7,7 @@ interface CreateSurveyButtonProps { const SaveButton: React.FC = ({onClick}) => { return ( - ); diff --git a/SurveyFrontend/src/components/SettingSurvey/SettingSurvey.tsx b/SurveyFrontend/src/components/SettingSurvey/SettingSurvey.tsx index f9d7c9a..ad2d5f7 100644 --- a/SurveyFrontend/src/components/SettingSurvey/SettingSurvey.tsx +++ b/SurveyFrontend/src/components/SettingSurvey/SettingSurvey.tsx @@ -1,10 +1,11 @@ -import React, {useState} from 'react'; +import React from 'react'; import SurveyInfo from "../SurveyInfo/SurveyInfo.tsx"; import styles from "./SettingSurvey.module.css"; import TimeEvent from "../TimeEvent/TimeEvent.tsx"; import SaveButton from "../SaveButton/SaveButton.tsx"; import {ISurvey} from "../../api/SurveyApi.ts"; import {useLocation, useOutletContext} from "react-router-dom"; +import {useSurveyContext} from "../../context/SurveyContext.tsx"; const SettingSurvey: React.FC = () => { @@ -14,9 +15,7 @@ const SettingSurvey: React.FC = () => { survey: ISurvey; setSurvey: (survey: ISurvey) => void; }>(); - - const [descriptionSurvey, setDescriptionSurvey] = useState(''); - const [titleSurvey, setTitleSurvey] = useState(''); + const { tempSurvey, setTempSurvey } = useSurveyContext(); const handleCopyLink = () => { if (!survey?.id) @@ -31,10 +30,10 @@ const SettingSurvey: React.FC = () => {
{isSettingCreatePage ? ( setTempSurvey({ ...tempSurvey, description: value })} + setTitleSurvey={(value) => setTempSurvey({ ...tempSurvey, title: value })} /> ) : ( { const navigate = useNavigate(); - const [descriptionSurvey, setDescriptionSurvey] = useState(''); - const [titleSurvey, setTitleSurvey] = useState('Название опроса'); + const location = useLocation(); + const { tempSurvey, setTempSurvey, clearTempSurvey } = useSurveyContext(); const [survey] = useState(null); - const [questions, setQuestions] = useState([ - { id: 1, text: '', questionType: 'SingleAnswerQuestion', answerVariants: [{ text: '' }]}, - ]); + const [questions, setQuestions] = useState( + 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