request to change the survey
This commit is contained in:
parent
3912bc0044
commit
1e719ae0aa
2 changed files with 44 additions and 6 deletions
|
|
@ -1,3 +1,10 @@
|
||||||
.survey_page{
|
.survey_page{
|
||||||
width: 85%;
|
width: 85%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.error{
|
||||||
|
color: #C0231F;
|
||||||
|
text-align: center;
|
||||||
|
margin: 10px 0;
|
||||||
|
font-size: 18px;
|
||||||
}
|
}
|
||||||
|
|
@ -1,16 +1,22 @@
|
||||||
import SurveyInfo from "../SurveyInfo/SurveyInfo.tsx";
|
import SurveyInfo from "../SurveyInfo/SurveyInfo.tsx";
|
||||||
import QuestionsList, {Question} from "../QuestionsList/QuestionsList.tsx";
|
import QuestionsList, {Question} from "../QuestionsList/QuestionsList.tsx";
|
||||||
import {useEffect, useState} from "react";
|
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 {useParams} from "react-router-dom";
|
||||||
import {getListQuestions} from "../../api/QuestionApi.ts";
|
import {getListQuestions} from "../../api/QuestionApi.ts";
|
||||||
import styles from "./SurveyPage.module.css";
|
import styles from "./SurveyPage.module.css";
|
||||||
|
import SaveButton from "../SaveButton/SaveButton.tsx";
|
||||||
|
|
||||||
export const SurveyPage: React.FC = () => {
|
export const SurveyPage: React.FC = () => {
|
||||||
const [survey, setSurvey] = useState<ISurvey | null>(null);
|
const [survey, setSurvey] = useState<ISurvey | null>(null);
|
||||||
const [questions, setQuestions] = useState<Question[]>([]);
|
const [questions, setQuestions] = useState<Question[]>([]);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const { surveyId } = useParams<{ surveyId: string }>(); // Изменили параметр на surveyId
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
const { surveyId } = useParams<{ surveyId: string }>();
|
||||||
|
|
||||||
|
const [description, setDescription] = useState('');
|
||||||
|
const [title, setTitle] = useState('');
|
||||||
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!surveyId) {
|
if (!surveyId) {
|
||||||
|
|
@ -30,6 +36,9 @@ export const SurveyPage: React.FC = () => {
|
||||||
const surveyData = await getSurveyById(id);
|
const surveyData = await getSurveyById(id);
|
||||||
setSurvey(surveyData);
|
setSurvey(surveyData);
|
||||||
|
|
||||||
|
setTitle(surveyData.title);
|
||||||
|
setDescription(surveyData.description);
|
||||||
|
|
||||||
const questionsData = await getListQuestions(id);
|
const questionsData = await getListQuestions(id);
|
||||||
const formattedQuestions = questionsData.map(q => ({
|
const formattedQuestions = questionsData.map(q => ({
|
||||||
id: q.id,
|
id: q.id,
|
||||||
|
|
@ -39,6 +48,7 @@ export const SurveyPage: React.FC = () => {
|
||||||
setQuestions(formattedQuestions);
|
setQuestions(formattedQuestions);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Ошибка:', error);
|
console.error('Ошибка:', error);
|
||||||
|
setError('Не удалось загрузить опрос')
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
|
|
@ -50,19 +60,40 @@ export const SurveyPage: React.FC = () => {
|
||||||
if (loading) return <div>Загрузка...</div>;
|
if (loading) return <div>Загрузка...</div>;
|
||||||
if (!survey) return <div>Опрос не найден</div>;
|
if (!survey) return <div>Опрос не найден</div>;
|
||||||
|
|
||||||
|
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 (
|
return (
|
||||||
<div className={styles.survey_page}>
|
<div className={styles.survey_page}>
|
||||||
<SurveyInfo
|
<SurveyInfo
|
||||||
titleSurvey={survey.title}
|
titleSurvey={title}
|
||||||
descriptionSurvey={survey.description}
|
descriptionSurvey={description}
|
||||||
setDescriptionSurvey={() => {}}
|
setDescriptionSurvey={setDescription}
|
||||||
setTitleSurvey={() => {}}
|
setTitleSurvey={setTitle}
|
||||||
/>
|
/>
|
||||||
<QuestionsList
|
<QuestionsList
|
||||||
questions={questions}
|
questions={questions}
|
||||||
setQuestions={setQuestions}
|
setQuestions={setQuestions}
|
||||||
surveyId={survey.id}
|
surveyId={survey.id}
|
||||||
/>
|
/>
|
||||||
|
{error && <div className={styles.error}>{error}</div>}
|
||||||
|
<SaveButton onClick={handleSave}/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
Loading…
Add table
Add a link
Reference in a new issue