|
167 | 167 | " |
168 | 168 | @update:options="updateQuestionOptions(index, $event)" |
169 | 169 | @clone="cloneQuestion(question, index)" |
170 | | - @delete="deleteQuestion(question.id)" |
| 170 | + @delete="onDelete(question.id)" |
171 | 171 | @moveDown="onMoveDown(index)" |
172 | 172 | @moveUp="onMoveUp(index)"> |
173 | 173 | <template |
@@ -1155,56 +1155,108 @@ export default defineComponent({ |
1155 | 1155 | } |
1156 | 1156 |
|
1157 | 1157 | /** |
1158 | | - * Delete a question |
| 1158 | + * Delete questions one after the other, one request each, and warn once about the jumps this changes; a failed request keeps the remaining questions |
1159 | 1159 | * |
1160 | | - * @param questionId the question id to delete |
| 1160 | + * @param questionIds the ids of the questions to delete |
1161 | 1161 | */ |
1162 | | - const deleteQuestion = async (questionId: number): Promise<void> => { |
1163 | | - if (deletingQuestionIds.has(questionId)) { |
| 1162 | + const deleteQuestions = async (questionIds: number[]): Promise<void> => { |
| 1163 | + const ids = questionIds.filter((id) => !deletingQuestionIds.has(id)) |
| 1164 | + if (ids.length === 0) { |
1164 | 1165 | return |
1165 | 1166 | } |
1166 | | - deletingQuestionIds.add(questionId) |
| 1167 | + ids.forEach((id) => deletingQuestionIds.add(id)) |
1167 | 1168 | const formId = props.form.id |
| 1169 | + const oldQuestions = props.form.questions |
1168 | 1170 | isLoadingQuestions.value = true |
1169 | 1171 | startChangingQuestions(formId) |
1170 | 1172 |
|
| 1173 | + const deleted = new Set<number>() |
1171 | 1174 | try { |
1172 | | - await queueQuestionRequest( |
1173 | | - props.form.questions.map(({ id }) => id), |
1174 | | - () => |
1175 | | - axios.delete( |
1176 | | - generateOcsUrl( |
1177 | | - 'apps/forms/api/v3/forms/{id}/questions/{questionId}', |
1178 | | - { |
1179 | | - id: props.form.id, |
1180 | | - questionId, |
1181 | | - }, |
| 1175 | + for (const questionId of ids) { |
| 1176 | + await queueQuestionRequest( |
| 1177 | + props.form.questions.map(({ id }) => id), |
| 1178 | + () => |
| 1179 | + axios.delete( |
| 1180 | + generateOcsUrl( |
| 1181 | + 'apps/forms/api/v3/forms/{id}/questions/{questionId}', |
| 1182 | + { |
| 1183 | + id: props.form.id, |
| 1184 | + questionId, |
| 1185 | + }, |
| 1186 | + ), |
1182 | 1187 | ), |
1183 | | - ), |
1184 | | - ) |
1185 | | - if (props.form.id !== formId) { |
1186 | | - return |
| 1188 | + ) |
| 1189 | + deleted.add(questionId) |
1187 | 1190 | } |
1188 | | - const questions = props.form.questions.filter( |
1189 | | - (search: FormsQuestion) => search.id !== questionId, |
1190 | | - ) |
1191 | | - warnAboutJumpChanges(props.form.questions, questions) |
1192 | | - emit('update:form', { ...props.form, questions }) |
1193 | | - emitEvent('forms:last-updated:set', props.form.id) |
1194 | 1191 | } catch (error) { |
1195 | | - logger.error(`Error while removing question ${questionId}`, { |
1196 | | - error, |
1197 | | - }) |
| 1192 | + logger.error('Error while removing questions', { error, ids }) |
1198 | 1193 | showError( |
1199 | 1194 | t('forms', 'There was an error while removing the question'), |
1200 | 1195 | ) |
1201 | 1196 | } finally { |
| 1197 | + if (props.form.id === formId && deleted.size > 0) { |
| 1198 | + const questions = props.form.questions.filter( |
| 1199 | + (search: FormsQuestion) => !deleted.has(search.id), |
| 1200 | + ) |
| 1201 | + warnAboutJumpChanges(oldQuestions, questions) |
| 1202 | + emit('update:form', { ...props.form, questions }) |
| 1203 | + emitEvent('forms:last-updated:set', props.form.id) |
| 1204 | + } |
1202 | 1205 | isLoadingQuestions.value = false |
1203 | 1206 | endChangingQuestions(formId) |
1204 | | - deletingQuestionIds.delete(questionId) |
| 1207 | + ids.forEach((id) => deletingQuestionIds.delete(id)) |
1205 | 1208 | } |
1206 | 1209 | } |
1207 | 1210 |
|
| 1211 | + /** |
| 1212 | + * Delete a question; for a section with questions, ask whether to delete only the section, so its questions join the part before it, or the section with its questions, deleting the section last |
| 1213 | + * |
| 1214 | + * @param questionId the id of the question or section |
| 1215 | + */ |
| 1216 | + const onDelete = async (questionId: number): Promise<void> => { |
| 1217 | + const position = props.form.questions.findIndex( |
| 1218 | + ({ id }) => id === questionId, |
| 1219 | + ) |
| 1220 | + if (props.form.questions[position]?.type !== QUESTION_TYPE_SECTION) { |
| 1221 | + await deleteQuestions([questionId]) |
| 1222 | + return |
| 1223 | + } |
| 1224 | + const end = props.form.questions.findIndex( |
| 1225 | + (question, index) => |
| 1226 | + index > position && question.type === QUESTION_TYPE_SECTION, |
| 1227 | + ) |
| 1228 | + const sectionQuestions = props.form.questions.slice( |
| 1229 | + position + 1, |
| 1230 | + end === -1 ? undefined : end, |
| 1231 | + ) |
| 1232 | + if (sectionQuestions.length === 0) { |
| 1233 | + await deleteQuestions([questionId]) |
| 1234 | + return |
| 1235 | + } |
| 1236 | + let withQuestions: boolean |
| 1237 | + try { |
| 1238 | + withQuestions = await showConfirmation({ |
| 1239 | + name: t('forms', 'Delete section'), |
| 1240 | + text: n( |
| 1241 | + 'forms', |
| 1242 | + 'Delete only the section, so its question joins the part before it, or delete the section with its question?', |
| 1243 | + 'Delete only the section, so its %n questions join the part before it, or delete the section with its %n questions?', |
| 1244 | + sectionQuestions.length, |
| 1245 | + ), |
| 1246 | + labelConfirm: t('forms', 'Delete questions'), |
| 1247 | + labelReject: t('forms', 'Keep questions'), |
| 1248 | + severity: 'warning', |
| 1249 | + }) |
| 1250 | + } catch { |
| 1251 | + return |
| 1252 | + } |
| 1253 | + await deleteQuestions( |
| 1254 | + withQuestions |
| 1255 | + ? [...sectionQuestions.map(({ id }) => id), questionId] |
| 1256 | + : [questionId], |
| 1257 | + ) |
| 1258 | + } |
| 1259 | +
|
1208 | 1260 | /** |
1209 | 1261 | * Clone a question |
1210 | 1262 | * |
@@ -1339,7 +1391,7 @@ export default defineComponent({ |
1339 | 1391 | resizeTitle, |
1340 | 1392 | resizeDescription, |
1341 | 1393 | addQuestion, |
1342 | | - deleteQuestion, |
| 1394 | + onDelete, |
1343 | 1395 | insertQuestion, |
1344 | 1396 | cloneQuestion, |
1345 | 1397 | questionNumbers, |
|
0 commit comments