Skip to content

Commit c37f4cd

Browse files
committed
issue-1118 Enhance SelectQuestionExercise to support question labels on edit
1 parent 9be2e49 commit c37f4cd

3 files changed

Lines changed: 14 additions & 9 deletions

File tree

bases/rsptx/assignment_server_api/assignment_builder/src/components/routes/AssignmentBuilder/components/exercises/components/CreateExercise/components/SelectQuestionExercise/SelectQuestionExercise.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FC, useMemo, useRef } from "react";
1+
import { FC, useMemo } from "react";
22

33
import { CreateExerciseFormType } from "@/types/exercises";
44
import { QuestionWithLabel } from "@/types/exercises";
@@ -35,6 +35,7 @@ const getDefaultFormData = (): Partial<CreateExerciseFormType> => ({
3535
htmlsrc: "",
3636
question_type: "selectquestion",
3737
questionList: [],
38+
questionLabels: {},
3839
abExperimentName: "",
3940
toggleOptions: [],
4041
dataLimitBasecourse: false
@@ -52,13 +53,12 @@ export const SelectQuestionExercise: FC<ExerciseComponentProps> = ({
5253
onFormReset,
5354
isEdit = false
5455
}) => {
55-
const questionLabelsRef = useRef<Map<string, string>>(new Map());
56-
5756
const generatePreviewWithLabels = (data: Partial<CreateExerciseFormType>): string => {
5857
const stringList = data.questionList || [];
58+
const labels = data.questionLabels || {};
5959
const questionListWithLabels = stringList.map((questionId) => ({
6060
questionId,
61-
label: questionLabelsRef.current.get(questionId)
61+
label: labels[questionId]
6262
}));
6363

6464
return generateSelectQuestionPreview({
@@ -116,24 +116,26 @@ export const SelectQuestionExercise: FC<ExerciseComponentProps> = ({
116116

117117
const safeQuestionList = useMemo(() => {
118118
const stringList = formData.questionList || [];
119+
const labels = formData.questionLabels || {};
119120

120121
return stringList.map((questionId) => ({
121122
questionId,
122-
label: questionLabelsRef.current.get(questionId)
123+
label: labels[questionId]
123124
}));
124-
}, [formData.questionList]);
125+
}, [formData.questionList, formData.questionLabels]);
125126

126127
const handleQuestionListChange = (questionList: QuestionWithLabel[]) => {
127-
questionLabelsRef.current.clear();
128+
const labelsRecord: Record<string, string> = {};
128129
questionList.forEach((q) => {
129130
if (q.label) {
130-
questionLabelsRef.current.set(q.questionId, q.label);
131+
labelsRecord[q.questionId] = q.label;
131132
}
132133
});
133134

134135
const stringList = convertToStringArray(questionList);
135136

136137
updateFormData("questionList", stringList);
138+
updateFormData("questionLabels", labelsRecord);
137139
};
138140

139141
const handleABExperimentChange = (experimentName: string) => {

bases/rsptx/assignment_server_api/assignment_builder/src/types/exercises.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export type QuestionJSON = Partial<{
9797
scale_max: number;
9898
forceCheckboxes: boolean;
9999
questionList: string[];
100+
questionLabels: Record<string, string>;
100101
abExperimentName: string;
101102
toggleOptions: string[];
102103
dataLimitBasecourse: boolean;

bases/rsptx/assignment_server_api/assignment_builder/src/utils/questionJson.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export const buildQuestionJson = (data: CreateExerciseFormType) => {
5454
}),
5555
...(data.question_type === "selectquestion" && {
5656
questionList: data.questionList,
57+
questionLabels: data.questionLabels,
5758
abExperimentName: data.abExperimentName,
5859
toggleOptions: data.toggleOptions,
5960
dataLimitBasecourse: data.dataLimitBasecourse
@@ -117,6 +118,7 @@ export const mergeQuestionJsonWithDefaults = (
117118
parsonspersonalize:
118119
questionJson?.parsonspersonalize ??
119120
(defaultQuestionJson.parsonspersonalize as "" | "solution-level" | "block-and-solution"),
120-
parsonsexample: questionJson?.parsonsexample ?? defaultQuestionJson.parsonsexample
121+
parsonsexample: questionJson?.parsonsexample ?? defaultQuestionJson.parsonsexample,
122+
questionLabels: questionJson?.questionLabels ?? {}
121123
};
122124
};

0 commit comments

Comments
 (0)