update survey and questions
This commit is contained in:
parent
4c4e6ee619
commit
51f3469031
5 changed files with 122 additions and 40 deletions
|
|
@ -3,10 +3,27 @@ import QuestionsList, {Question} from "../QuestionsList/QuestionsList.tsx";
|
|||
import {useEffect, useState} from "react";
|
||||
import {getSurveyById, ISurvey, updateSurvey} from "../../api/SurveyApi.ts";
|
||||
import {useParams} from "react-router-dom";
|
||||
import {getListQuestions} from "../../api/QuestionApi.ts";
|
||||
import {addNewQuestion, getListQuestions, updateQuestion} from "../../api/QuestionApi.ts";
|
||||
import styles from "./SurveyPage.module.css";
|
||||
import SaveButton from "../SaveButton/SaveButton.tsx";
|
||||
|
||||
type ActionType = 'create' | 'update';
|
||||
|
||||
class QuestionAction {
|
||||
type: ActionType;
|
||||
data: {
|
||||
surveyId: number;
|
||||
id?: number;
|
||||
title?: string;
|
||||
questionType?: string;
|
||||
};
|
||||
|
||||
constructor(type: ActionType, data: { surveyId: number, id?: number, title?: string, questionType?: string }) {
|
||||
this.type = type;
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
|
||||
export const SurveyPage: React.FC = () => {
|
||||
const [survey, setSurvey] = useState<ISurvey | null>(null);
|
||||
const [questions, setQuestions] = useState<Question[]>([]);
|
||||
|
|
@ -60,24 +77,73 @@ export const SurveyPage: React.FC = () => {
|
|||
if (loading) return <div>Загрузка...</div>;
|
||||
if (!survey) return <div>Опрос не найден</div>;
|
||||
|
||||
const handleSave = async() => {
|
||||
const handleSave = async () => {
|
||||
if (!surveyId || !survey) return;
|
||||
|
||||
try{
|
||||
try {
|
||||
setError(null);
|
||||
const id = parseInt(surveyId);
|
||||
const surveyUpdated = await updateSurvey(id, {
|
||||
|
||||
const surveyUpdated = await updateSurvey(id, {
|
||||
title: title,
|
||||
description: description,
|
||||
})
|
||||
});
|
||||
setSurvey(surveyUpdated);
|
||||
}
|
||||
catch(error){
|
||||
console.error('Ошибка при сохранении опроса:', error);
|
||||
|
||||
const actions: QuestionAction[] = [];
|
||||
const serverQuestions = await getListQuestions(id);
|
||||
|
||||
questions.forEach(question => {
|
||||
const existsOnServer = serverQuestions.some(q => q.id === question.id);
|
||||
|
||||
if (existsOnServer) {
|
||||
actions.push(new QuestionAction("update", {
|
||||
surveyId: id,
|
||||
id: question.id,
|
||||
title: question.text,
|
||||
questionType: question.questionType
|
||||
}));
|
||||
} else {
|
||||
actions.push(new QuestionAction("create", {
|
||||
surveyId: id,
|
||||
title: question.text,
|
||||
questionType: question.questionType
|
||||
}));
|
||||
}
|
||||
});
|
||||
|
||||
for (const action of actions) {
|
||||
switch (action.type) {
|
||||
case "create":
|
||||
await addNewQuestion(id, {
|
||||
title: action.data.title as string,
|
||||
questionType: action.data.questionType as 'singleanswerquestion' | 'multipleanswerquestion',
|
||||
});
|
||||
break;
|
||||
case "update":
|
||||
if (action.data.id) {
|
||||
await updateQuestion(id, action.data.id, {
|
||||
title: action.data.title,
|
||||
questionType: action.data.questionType
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const updatedQuestions = await getListQuestions(id);
|
||||
const formattedQuestions = updatedQuestions.map(q => ({
|
||||
id: q.id,
|
||||
text: q.title,
|
||||
questionType: q.questionType as 'singleanswerquestion' | 'multipleanswerquestion',
|
||||
}));
|
||||
setQuestions(formattedQuestions);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Ошибка:', error);
|
||||
setError('Не удалось сохранить изменения');
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={styles.survey_page}>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue