From 1e719ae0aad73e96cfdb3a7977402c9bbc3a215f Mon Sep 17 00:00:00 2001 From: Tatiana Nikolaeva Date: Sat, 24 May 2025 16:46:52 +0500 Subject: [PATCH] request to change the survey --- .../SurveyPage/SurveyPage.module.css | 7 +++ .../src/components/SurveyPage/SurveyPage.tsx | 43 ++++++++++++++++--- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/SurveyFrontend/src/components/SurveyPage/SurveyPage.module.css b/SurveyFrontend/src/components/SurveyPage/SurveyPage.module.css index 4b4b31e..ebe030f 100644 --- a/SurveyFrontend/src/components/SurveyPage/SurveyPage.module.css +++ b/SurveyFrontend/src/components/SurveyPage/SurveyPage.module.css @@ -1,3 +1,10 @@ .survey_page{ width: 85%; +} + +.error{ + color: #C0231F; + text-align: center; + margin: 10px 0; + font-size: 18px; } \ No newline at end of file diff --git a/SurveyFrontend/src/components/SurveyPage/SurveyPage.tsx b/SurveyFrontend/src/components/SurveyPage/SurveyPage.tsx index c4a2d85..399b807 100644 --- a/SurveyFrontend/src/components/SurveyPage/SurveyPage.tsx +++ b/SurveyFrontend/src/components/SurveyPage/SurveyPage.tsx @@ -1,16 +1,22 @@ import SurveyInfo from "../SurveyInfo/SurveyInfo.tsx"; import QuestionsList, {Question} from "../QuestionsList/QuestionsList.tsx"; import {useEffect, useState} from "react"; -import {getSurveyById, ISurvey} from "../../api/SurveyApi.ts"; +import {getSurveyById, ISurvey, updateSurvey} from "../../api/SurveyApi.ts"; import {useParams} from "react-router-dom"; import {getListQuestions} from "../../api/QuestionApi.ts"; import styles from "./SurveyPage.module.css"; +import SaveButton from "../SaveButton/SaveButton.tsx"; export const SurveyPage: React.FC = () => { const [survey, setSurvey] = useState(null); const [questions, setQuestions] = useState([]); const [loading, setLoading] = useState(true); - const { surveyId } = useParams<{ surveyId: string }>(); // Изменили параметр на surveyId + const [error, setError] = useState(null); + const { surveyId } = useParams<{ surveyId: string }>(); + + const [description, setDescription] = useState(''); + const [title, setTitle] = useState(''); + useEffect(() => { if (!surveyId) { @@ -30,6 +36,9 @@ export const SurveyPage: React.FC = () => { const surveyData = await getSurveyById(id); setSurvey(surveyData); + setTitle(surveyData.title); + setDescription(surveyData.description); + const questionsData = await getListQuestions(id); const formattedQuestions = questionsData.map(q => ({ id: q.id, @@ -39,6 +48,7 @@ export const SurveyPage: React.FC = () => { setQuestions(formattedQuestions); } catch (error) { console.error('Ошибка:', error); + setError('Не удалось загрузить опрос') } finally { setLoading(false); } @@ -50,19 +60,40 @@ export const SurveyPage: React.FC = () => { if (loading) return
Загрузка...
; if (!survey) return
Опрос не найден
; + const handleSave = async() => { + if (!surveyId || !survey) return; + + try{ + setError(null); + const id = parseInt(surveyId); + const surveyUpdated = await updateSurvey(id, { + title: title, + description: description, + }) + setSurvey(surveyUpdated); + } + catch(error){ + console.error('Ошибка при сохранении опроса:', error); + setError('Не удалось сохранить изменения'); + throw error; + } + } + return (
{}} - setTitleSurvey={() => {}} + titleSurvey={title} + descriptionSurvey={description} + setDescriptionSurvey={setDescription} + setTitleSurvey={setTitle} /> + {error &&
{error}
} +
); }; \ No newline at end of file