fix surveyInfo

This commit is contained in:
Tatiana Nikolaeva 2025-05-26 10:11:53 +05:00
parent 6068c717d3
commit 33f2b5ef62
8 changed files with 91 additions and 62 deletions

View file

@ -1,8 +1,8 @@
import React, { useEffect, useState } from "react";
import SurveyInfo from "../SurveyInfo/SurveyInfo.tsx";
import QuestionsList, { Question } from "../QuestionsList/QuestionsList.tsx";
import { getSurveyById, ISurvey, updateSurvey } from "../../api/SurveyApi.ts";
import { useParams } from "react-router-dom";
import { ISurvey, updateSurvey } from "../../api/SurveyApi.ts";
import {useOutletContext} from "react-router-dom";
import { addNewQuestion, getListQuestions, updateQuestion, deleteQuestion } from "../../api/QuestionApi.ts";
import styles from "./SurveyPage.module.css";
import SaveButton from "../SaveButton/SaveButton.tsx";
@ -153,22 +153,28 @@ class ActionQueue {
}
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 [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const { surveyId } = useParams<{ surveyId: string }>();
// const { surveyId } = useParams<{ surveyId: string }>();
const [description, setDescription] = useState('');
const [title, setTitle] = useState('');
const { survey, setSurvey } = useOutletContext<{
survey: ISurvey;
setSurvey: (survey: ISurvey) => void;
}>();
useEffect(() => {
if (!surveyId) {
if (!survey.id) {
console.error('Survey ID is missing');
return;
}
const id = parseInt(surveyId);
// const id = parseInt(survey.id);
const id = survey.id;
if (isNaN(id)) {
console.error('Invalid survey ID');
return;
@ -177,10 +183,10 @@ export const SurveyPage: React.FC = () => {
const fetchData = async () => {
try {
setLoading(true);
const surveyData = await getSurveyById(id);
setSurvey(surveyData);
setTitle(surveyData.title);
setDescription(surveyData.description);
// const surveyData = await getSurveyById(id);
setSurvey(survey);
setTitle(survey.title);
setDescription(survey.description);
const questionsData = await getListQuestions(id);
const formattedQuestions = await Promise.all(questionsData.map(async q => {
@ -205,14 +211,15 @@ export const SurveyPage: React.FC = () => {
};
fetchData();
}, [surveyId]);
}, [survey.id]);
const handleSave = async () => {
if (!surveyId || !survey) return;
if (!survey.id || !survey) return;
try {
setError(null);
const id = parseInt(surveyId);
// const id = parseInt(survey.id);
const id = survey.id;
const actionQueue = new ActionQueue();
actionQueue.add({