fix add new answer
This commit is contained in:
parent
15ec6b9632
commit
6eb03dc112
10 changed files with 87 additions and 160 deletions
|
|
@ -8,7 +8,6 @@ import styles from "./SurveyPage.module.css";
|
|||
import SaveButton from "../SaveButton/SaveButton.tsx";
|
||||
import { addNewAnswerVariant, deleteAnswerVariant, getAnswerVariants, IAnswerVariant, updateAnswerVariant } from "../../api/AnswerApi.ts";
|
||||
|
||||
// Типы для действий
|
||||
type ActionType =
|
||||
| 'update-survey'
|
||||
| 'create-question'
|
||||
|
|
@ -18,7 +17,6 @@ type ActionType =
|
|||
| 'update-answer'
|
||||
| 'delete-answer';
|
||||
|
||||
// Интерфейсы для данных действий
|
||||
interface SurveyActionData {
|
||||
id: number;
|
||||
title: string;
|
||||
|
|
@ -54,16 +52,6 @@ class ActionQueue {
|
|||
}
|
||||
|
||||
async execute() {
|
||||
console.log(`Выполнение очереди с ${this.actions.length} действиями`);
|
||||
for (const [index, action] of this.actions.entries()) {
|
||||
console.log(`Обработка действия ${index + 1}/${this.actions.length}:`, action);
|
||||
try {
|
||||
// ... существующий код
|
||||
} catch (error) {
|
||||
console.error(`Ошибка в действии ${index + 1}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
for (const action of this.actions) {
|
||||
try {
|
||||
switch (action.type) {
|
||||
|
|
@ -147,21 +135,13 @@ class ActionQueue {
|
|||
}
|
||||
|
||||
private async handleUpdateAnswer(data: AnswerActionData & { id: number }) {
|
||||
console.log('1. Начало handleUpdateAnswer', data);
|
||||
try {
|
||||
console.log('2. Перед вызовом updateAnswerVariant', {
|
||||
surveyId: data.surveyId,
|
||||
questionId: data.questionId,
|
||||
id: data.id,
|
||||
text: data.text
|
||||
});
|
||||
const result = await updateAnswerVariant(data.surveyId, data.questionId, data.id, {
|
||||
text: data.text
|
||||
});
|
||||
console.log('3. После вызова updateAnswerVariant', result);
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error('4. Ошибка в handleUpdateAnswer:', error);
|
||||
console.error(error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
|
|
@ -228,86 +208,62 @@ export const SurveyPage: React.FC = () => {
|
|||
}, [surveyId]);
|
||||
|
||||
const handleSave = async () => {
|
||||
console.log('0. Начало handleSave');
|
||||
if (!surveyId || !survey) {
|
||||
console.log('0.1. Прерывание - нет surveyId или survey', { surveyId, survey });
|
||||
return;
|
||||
}
|
||||
if (!surveyId || !survey) return;
|
||||
|
||||
try {
|
||||
setError(null);
|
||||
const id = parseInt(surveyId);
|
||||
const actionQueue = new ActionQueue();
|
||||
|
||||
// 1. Обновление опроса
|
||||
actionQueue.add({
|
||||
type: 'update-survey',
|
||||
data: { id, title, description } as SurveyActionData
|
||||
});
|
||||
|
||||
// 2. Получаем текущие вопросы с сервера
|
||||
const serverQuestions = await getListQuestions(id);
|
||||
|
||||
// 3. Обработка вопросов
|
||||
questions.forEach(question => {
|
||||
const isNewQuestion = !serverQuestions.some(q => q.id === question.id);
|
||||
|
||||
if (isNewQuestion) {
|
||||
const serverQuestion = serverQuestions.find(q => q.id === question.id);
|
||||
if (serverQuestion &&
|
||||
(serverQuestion.title !== question.text ||
|
||||
serverQuestion.questionType !== question.questionType)) {
|
||||
actionQueue.add({
|
||||
type: 'create-question',
|
||||
type: 'update-question',
|
||||
data: {
|
||||
surveyId: id,
|
||||
id: question.id,
|
||||
title: question.text,
|
||||
questionType: question.questionType
|
||||
} as QuestionActionData,
|
||||
tempId: question.id
|
||||
});
|
||||
} else {
|
||||
// Обновляем только если текст изменился
|
||||
const serverQuestion = serverQuestions.find(q => q.id === question.id);
|
||||
if (serverQuestion && (serverQuestion.title !== question.text || serverQuestion.questionType !== question.questionType)) {
|
||||
actionQueue.add({
|
||||
type: 'update-question',
|
||||
data: {
|
||||
surveyId: id,
|
||||
id: question.id,
|
||||
title: question.text,
|
||||
questionType: question.questionType
|
||||
} as QuestionActionData & { id: number }
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Обработка вариантов ответов
|
||||
if (question.answerVariants) {
|
||||
question.answerVariants.forEach(answer => {
|
||||
if (isNewQuestion || !answer.id) {
|
||||
actionQueue.add({
|
||||
type: 'create-answer',
|
||||
data: {
|
||||
surveyId: id,
|
||||
questionId: question.id,
|
||||
text: answer.text
|
||||
} as AnswerActionData,
|
||||
tempId: question.id
|
||||
});
|
||||
} else {
|
||||
// Обновляем только если текст изменился
|
||||
actionQueue.add({
|
||||
type: 'update-answer',
|
||||
data: {
|
||||
surveyId: id,
|
||||
questionId: question.id,
|
||||
id: answer.id,
|
||||
text: answer.text
|
||||
} as AnswerActionData & { id: number }
|
||||
});
|
||||
}
|
||||
} as QuestionActionData & { id: number }
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 5. Удаление удаленных вопросов
|
||||
questions.forEach(question => {
|
||||
question.answerVariants.forEach(answer => {
|
||||
if (!answer.id) {
|
||||
actionQueue.add({
|
||||
type: 'create-answer',
|
||||
data: {
|
||||
surveyId: id,
|
||||
questionId: question.id,
|
||||
text: answer.text
|
||||
} as AnswerActionData
|
||||
});
|
||||
} else {
|
||||
actionQueue.add({
|
||||
type: 'update-answer',
|
||||
data: {
|
||||
surveyId: id,
|
||||
questionId: question.id,
|
||||
id: answer.id,
|
||||
text: answer.text
|
||||
} as AnswerActionData & { id: number }
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
serverQuestions.forEach(serverQuestion => {
|
||||
if (!questions.some(q => q.id === serverQuestion.id)) {
|
||||
actionQueue.add({
|
||||
|
|
@ -320,10 +276,8 @@ export const SurveyPage: React.FC = () => {
|
|||
}
|
||||
});
|
||||
|
||||
// Выполняем все действия
|
||||
await actionQueue.execute();
|
||||
|
||||
// Обновляем данные
|
||||
const updatedQuestions = await getListQuestions(id);
|
||||
const formattedQuestions = await Promise.all(updatedQuestions.map(async q => {
|
||||
const answerVariants = await getAnswerVariants(id, q.id);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue