Preserve dynamic question slot kinds when rebuilding practice questions on edit.
Carry the original API payload through the form so slot kinds and timer values are resolved correctly when saving edited dynamic questions. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
4eac111a7b
commit
827e20b4f1
|
|
@ -1,5 +1,5 @@
|
||||||
import type { CreateQuestionRequest, QuestionOption } from "../types/course.types"
|
import type { CreateQuestionRequest, QuestionOption } from "../types/course.types"
|
||||||
import type { QuestionTypeDefinition } from "../types/questionTypeDefinition.types"
|
import type { QuestionTypeDefinition, DynamicQuestionPayload } from "../types/questionTypeDefinition.types"
|
||||||
import { createEmptyTable, serializeTableSlotValue } from "./dynamicTableValue"
|
import { createEmptyTable, serializeTableSlotValue } from "./dynamicTableValue"
|
||||||
import { buildDynamicQuestionPayload } from "./practiceDynamicQuestionPayload"
|
import { buildDynamicQuestionPayload } from "./practiceDynamicQuestionPayload"
|
||||||
import {
|
import {
|
||||||
|
|
@ -188,6 +188,8 @@ export interface LearnEnglishDefinitionQuestionInput {
|
||||||
questionText: string
|
questionText: string
|
||||||
questionTypeDefinitionId: number
|
questionTypeDefinitionId: number
|
||||||
dynamicFieldValues: Record<string, string>
|
dynamicFieldValues: Record<string, string>
|
||||||
|
/** Original payload from API — used to preserve slot kinds when rebuilding on edit. */
|
||||||
|
sourceDynamicPayload?: DynamicQuestionPayload | null
|
||||||
difficultyLevel?: QuestionDifficultyLevel
|
difficultyLevel?: QuestionDifficultyLevel
|
||||||
points?: number
|
points?: number
|
||||||
associatedQuestionId?: number | null
|
associatedQuestionId?: number | null
|
||||||
|
|
@ -346,6 +348,7 @@ export function buildCreateQuestionFromDefinition(
|
||||||
responseRows: def.response_schema.map((r) => ({ id: r.id, kind: r.kind })),
|
responseRows: def.response_schema.map((r) => ({ id: r.id, kind: r.kind })),
|
||||||
fieldValues,
|
fieldValues,
|
||||||
mcqOptions: q.mcqOptions,
|
mcqOptions: q.mcqOptions,
|
||||||
|
existingPayload: q.sourceDynamicPayload ?? null,
|
||||||
})
|
})
|
||||||
return {
|
return {
|
||||||
question_type: "DYNAMIC",
|
question_type: "DYNAMIC",
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import type { DynamicQuestionPayload } from "../types/questionTypeDefinition.types"
|
import type { DynamicQuestionPayload, DynamicElementInstance } from "../types/questionTypeDefinition.types"
|
||||||
import { parseTableSlotValue } from "./dynamicTableValue"
|
import { parseTableSlotValue } from "./dynamicTableValue"
|
||||||
import {
|
import {
|
||||||
finalizeMatchingAnswerPayload,
|
finalizeMatchingAnswerPayload,
|
||||||
|
|
@ -71,6 +71,51 @@ function isSecondsKind(kind: string): boolean {
|
||||||
return upper === "PREP_TIME" || upper === "ANSWER_TIMER"
|
return upper === "PREP_TIME" || upper === "ANSWER_TIMER"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isTimerValueShape(value: unknown): boolean {
|
||||||
|
if (value == null) return false
|
||||||
|
if (typeof value === "number" && Number.isFinite(value) && value >= 0) return true
|
||||||
|
if (typeof value === "string") {
|
||||||
|
const t = value.trim()
|
||||||
|
if (/^\d+$/.test(t)) return true
|
||||||
|
if (t.startsWith("{") && t.includes("seconds")) return true
|
||||||
|
}
|
||||||
|
if (typeof value === "object" && !Array.isArray(value) && "seconds" in value) return true
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveRowKind(
|
||||||
|
row: { id: string; kind: string },
|
||||||
|
side: "stimulus" | "response",
|
||||||
|
existingPayload?: DynamicQuestionPayload | null,
|
||||||
|
): string {
|
||||||
|
const fromRow = row.kind?.trim()
|
||||||
|
if (fromRow) return fromRow
|
||||||
|
const slots = side === "stimulus" ? existingPayload?.stimulus : existingPayload?.response
|
||||||
|
const fromPayload = slots?.find((slot) => slot.id === row.id)?.kind?.trim()
|
||||||
|
return fromPayload ?? fromRow ?? ""
|
||||||
|
}
|
||||||
|
|
||||||
|
function mergeSchemaRowsWithPayload(
|
||||||
|
schemaRows: { id: string; kind: string }[],
|
||||||
|
payloadSlots: DynamicElementInstance[] | undefined,
|
||||||
|
): { id: string; kind: string }[] {
|
||||||
|
const merged = schemaRows.map((row) => ({ ...row }))
|
||||||
|
const byId = new Map(merged.map((row) => [row.id, row]))
|
||||||
|
for (const slot of payloadSlots ?? []) {
|
||||||
|
const id = slot.id?.trim()
|
||||||
|
if (!id) continue
|
||||||
|
const existing = byId.get(id)
|
||||||
|
if (existing) {
|
||||||
|
if (!existing.kind?.trim() && slot.kind?.trim()) {
|
||||||
|
existing.kind = slot.kind.trim()
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
merged.push({ id, kind: slot.kind?.trim() ?? "" })
|
||||||
|
}
|
||||||
|
return merged
|
||||||
|
}
|
||||||
|
|
||||||
function isTableKind(kind: string): boolean {
|
function isTableKind(kind: string): boolean {
|
||||||
return kind.trim().toUpperCase() === "TABLE"
|
return kind.trim().toUpperCase() === "TABLE"
|
||||||
}
|
}
|
||||||
|
|
@ -147,23 +192,25 @@ function slotValueForRow(
|
||||||
mcqOptionsConsumed: { current: boolean },
|
mcqOptionsConsumed: { current: boolean },
|
||||||
stimulusRows: { id: string; kind: string }[],
|
stimulusRows: { id: string; kind: string }[],
|
||||||
responseRows: { id: string; kind: string }[],
|
responseRows: { id: string; kind: string }[],
|
||||||
|
existingPayload?: DynamicQuestionPayload | null,
|
||||||
): unknown {
|
): unknown {
|
||||||
const fieldKey = `${side}:${row.id}`
|
const fieldKey = `${side}:${row.id}`
|
||||||
const rawField = fieldValues[fieldKey]
|
const rawField = fieldValues[fieldKey]
|
||||||
const upperKind = row.kind.trim().toUpperCase()
|
const effectiveKind = resolveRowKind(row, side, existingPayload)
|
||||||
|
const upperKind = effectiveKind.trim().toUpperCase()
|
||||||
|
|
||||||
if (isSecondsKind(row.kind)) {
|
if (isSecondsKind(effectiveKind)) {
|
||||||
return finalizeSecondsPayload(rawField)
|
return finalizeSecondsPayload(rawField)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isTableKind(row.kind)) {
|
if (isTableKind(effectiveKind)) {
|
||||||
const t = (rawField ?? "").trim()
|
const t = (rawField ?? "").trim()
|
||||||
if (!t) return { columns: [], rows: [] }
|
if (!t) return { columns: [], rows: [] }
|
||||||
const table = parseTableSlotValue(rawField)
|
const table = parseTableSlotValue(rawField)
|
||||||
return { columns: table.columns, rows: table.rows }
|
return { columns: table.columns, rows: table.rows }
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isMultipleChoiceKind(row.kind)) {
|
if (isMultipleChoiceKind(effectiveKind)) {
|
||||||
const fromField = parseMultipleChoiceSlotValue(rawField)
|
const fromField = parseMultipleChoiceSlotValue(rawField)
|
||||||
if (multipleChoiceSlotHasContent(fromField)) {
|
if (multipleChoiceSlotHasContent(fromField)) {
|
||||||
return {
|
return {
|
||||||
|
|
@ -183,7 +230,7 @@ function slotValueForRow(
|
||||||
return { options: [] }
|
return { options: [] }
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isMatchingInputsKind(row.kind)) {
|
if (isMatchingInputsKind(effectiveKind)) {
|
||||||
const fromField = parseMatchingInputsSlotValue(rawField)
|
const fromField = parseMatchingInputsSlotValue(rawField)
|
||||||
if (matchingInputsSlotHasContent(fromField)) {
|
if (matchingInputsSlotHasContent(fromField)) {
|
||||||
return finalizeMatchingInputsPayload(fromField)
|
return finalizeMatchingInputsPayload(fromField)
|
||||||
|
|
@ -191,7 +238,7 @@ function slotValueForRow(
|
||||||
return { left: [], right: [] }
|
return { left: [], right: [] }
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isMatchingAnswerKind(row.kind)) {
|
if (isMatchingAnswerKind(effectiveKind)) {
|
||||||
const matchingInputs = findMatchingInputsForAnswerRow(
|
const matchingInputs = findMatchingInputsForAnswerRow(
|
||||||
fieldValues,
|
fieldValues,
|
||||||
stimulusRows,
|
stimulusRows,
|
||||||
|
|
@ -206,7 +253,7 @@ function slotValueForRow(
|
||||||
return { pairs: [] }
|
return { pairs: [] }
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isSelectMissingWordsKind(row.kind)) {
|
if (isSelectMissingWordsKind(effectiveKind)) {
|
||||||
if (side === "stimulus") {
|
if (side === "stimulus") {
|
||||||
const fromField = parseSelectMissingWordsStimulusSlotValue(rawField)
|
const fromField = parseSelectMissingWordsStimulusSlotValue(rawField)
|
||||||
if (selectMissingWordsStimulusHasContent(fromField)) {
|
if (selectMissingWordsStimulusHasContent(fromField)) {
|
||||||
|
|
@ -231,7 +278,7 @@ function slotValueForRow(
|
||||||
return { blanks: [] }
|
return { blanks: [] }
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isSequenceOrderKind(row.kind)) {
|
if (isSequenceOrderKind(effectiveKind)) {
|
||||||
const fromField = parseSequenceOrderSlotValue(rawField)
|
const fromField = parseSequenceOrderSlotValue(rawField)
|
||||||
if (sequenceOrderSlotHasContent(fromField)) {
|
if (sequenceOrderSlotHasContent(fromField)) {
|
||||||
return finalizeSequenceOrderPayload(fromField)
|
return finalizeSequenceOrderPayload(fromField)
|
||||||
|
|
@ -248,10 +295,9 @@ function slotValueForRow(
|
||||||
}
|
}
|
||||||
|
|
||||||
export function slotApiValueToFieldString(value: unknown, kind: string): string {
|
export function slotApiValueToFieldString(value: unknown, kind: string): string {
|
||||||
const upperKind = kind.trim().toUpperCase()
|
|
||||||
if (value == null) return ""
|
if (value == null) return ""
|
||||||
|
|
||||||
if (isSecondsKind(kind)) {
|
if (isSecondsKind(kind) || (!kind.trim() && isTimerValueShape(value))) {
|
||||||
const seconds = extractSecondsForField(value)
|
const seconds = extractSecondsForField(value)
|
||||||
// Plain digits in the form; buildDynamicQuestionPayload rewraps to { seconds }.
|
// Plain digits in the form; buildDynamicQuestionPayload rewraps to { seconds }.
|
||||||
return seconds == null ? "" : String(seconds)
|
return seconds == null ? "" : String(seconds)
|
||||||
|
|
@ -285,38 +331,49 @@ export function buildDynamicQuestionPayload(input: {
|
||||||
responseRows: { id: string; kind: string }[]
|
responseRows: { id: string; kind: string }[]
|
||||||
fieldValues: Record<string, string>
|
fieldValues: Record<string, string>
|
||||||
mcqOptions?: { option_text: string; is_correct: boolean }[]
|
mcqOptions?: { option_text: string; is_correct: boolean }[]
|
||||||
|
existingPayload?: DynamicQuestionPayload | null
|
||||||
}): DynamicQuestionPayload {
|
}): DynamicQuestionPayload {
|
||||||
const mcqOptionsConsumed = { current: false }
|
const mcqOptionsConsumed = { current: false }
|
||||||
|
const stimulusRows = mergeSchemaRowsWithPayload(
|
||||||
|
input.stimulusRows,
|
||||||
|
input.existingPayload?.stimulus,
|
||||||
|
)
|
||||||
|
const responseRows = mergeSchemaRowsWithPayload(
|
||||||
|
input.responseRows,
|
||||||
|
input.existingPayload?.response,
|
||||||
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
stimulus: input.stimulusRows
|
stimulus: stimulusRows
|
||||||
.filter((row) => !isNoInputComponentKind(row.kind))
|
.filter((row) => !isNoInputComponentKind(resolveRowKind(row, "stimulus", input.existingPayload)))
|
||||||
.map((row) => ({
|
.map((row) => ({
|
||||||
id: row.id,
|
id: row.id,
|
||||||
kind: row.kind,
|
kind: resolveRowKind(row, "stimulus", input.existingPayload),
|
||||||
value: slotValueForRow(
|
value: slotValueForRow(
|
||||||
row,
|
row,
|
||||||
"stimulus",
|
"stimulus",
|
||||||
input.fieldValues,
|
input.fieldValues,
|
||||||
input.mcqOptions,
|
input.mcqOptions,
|
||||||
mcqOptionsConsumed,
|
mcqOptionsConsumed,
|
||||||
input.stimulusRows,
|
stimulusRows,
|
||||||
input.responseRows,
|
responseRows,
|
||||||
|
input.existingPayload,
|
||||||
),
|
),
|
||||||
})),
|
})),
|
||||||
response: input.responseRows
|
response: responseRows
|
||||||
.filter((row) => !isNoInputComponentKind(row.kind))
|
.filter((row) => !isNoInputComponentKind(resolveRowKind(row, "response", input.existingPayload)))
|
||||||
.map((row) => ({
|
.map((row) => ({
|
||||||
id: row.id,
|
id: row.id,
|
||||||
kind: row.kind,
|
kind: resolveRowKind(row, "response", input.existingPayload),
|
||||||
value: slotValueForRow(
|
value: slotValueForRow(
|
||||||
row,
|
row,
|
||||||
"response",
|
"response",
|
||||||
input.fieldValues,
|
input.fieldValues,
|
||||||
input.mcqOptions,
|
input.mcqOptions,
|
||||||
mcqOptionsConsumed,
|
mcqOptionsConsumed,
|
||||||
input.stimulusRows,
|
stimulusRows,
|
||||||
input.responseRows,
|
responseRows,
|
||||||
|
input.existingPayload,
|
||||||
),
|
),
|
||||||
})),
|
})),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -60,6 +60,7 @@ export interface PracticeFormQuestionRow {
|
||||||
difficultyLevel: "EASY" | "MEDIUM" | "HARD"
|
difficultyLevel: "EASY" | "MEDIUM" | "HARD"
|
||||||
points: number
|
points: number
|
||||||
dynamicFieldValues: Record<string, string>
|
dynamicFieldValues: Record<string, string>
|
||||||
|
sourceDynamicPayload?: DynamicQuestionPayload | null
|
||||||
mcqOptions: { text: string; isCorrect: boolean }[]
|
mcqOptions: { text: string; isCorrect: boolean }[]
|
||||||
trueFalseCorrect: boolean
|
trueFalseCorrect: boolean
|
||||||
shortAnswers: string[]
|
shortAnswers: string[]
|
||||||
|
|
@ -560,6 +561,7 @@ function mapFullQuestionToFormRow(
|
||||||
? Number(q.points)
|
? Number(q.points)
|
||||||
: 1,
|
: 1,
|
||||||
dynamicFieldValues,
|
dynamicFieldValues,
|
||||||
|
sourceDynamicPayload: q.dynamic_payload ?? null,
|
||||||
mcqOptions,
|
mcqOptions,
|
||||||
trueFalseCorrect,
|
trueFalseCorrect,
|
||||||
shortAnswers,
|
shortAnswers,
|
||||||
|
|
|
||||||
|
|
@ -397,6 +397,7 @@ export function AddPracticeFlow() {
|
||||||
associatedAnchorRowId: q.associatedAnchorRowId ?? null,
|
associatedAnchorRowId: q.associatedAnchorRowId ?? null,
|
||||||
stimulusBlockKey: q.stimulusBlockKey ?? null,
|
stimulusBlockKey: q.stimulusBlockKey ?? null,
|
||||||
dynamicFieldValues: { ...(q.dynamicFieldValues ?? {}) },
|
dynamicFieldValues: { ...(q.dynamicFieldValues ?? {}) },
|
||||||
|
sourceDynamicPayload: q.sourceDynamicPayload ?? null,
|
||||||
mcqOptions: (q.mcqOptions ?? []).map(
|
mcqOptions: (q.mcqOptions ?? []).map(
|
||||||
(o: { text?: string; isCorrect?: boolean }) => ({
|
(o: { text?: string; isCorrect?: boolean }) => ({
|
||||||
option_text: String(o.text ?? ""),
|
option_text: String(o.text ?? ""),
|
||||||
|
|
|
||||||
|
|
@ -429,6 +429,7 @@ export function EditPracticeFlow() {
|
||||||
associatedAnchorRowId: q.associatedAnchorRowId ?? null,
|
associatedAnchorRowId: q.associatedAnchorRowId ?? null,
|
||||||
stimulusBlockKey: q.stimulusBlockKey ?? null,
|
stimulusBlockKey: q.stimulusBlockKey ?? null,
|
||||||
dynamicFieldValues: { ...(q.dynamicFieldValues ?? {}) },
|
dynamicFieldValues: { ...(q.dynamicFieldValues ?? {}) },
|
||||||
|
sourceDynamicPayload: q.sourceDynamicPayload ?? null,
|
||||||
mcqOptions: (q.mcqOptions ?? []).map(
|
mcqOptions: (q.mcqOptions ?? []).map(
|
||||||
(o: { text?: string; isCorrect?: boolean }) => ({
|
(o: { text?: string; isCorrect?: boolean }) => ({
|
||||||
option_text: String(o.text ?? ""),
|
option_text: String(o.text ?? ""),
|
||||||
|
|
|
||||||
|
|
@ -1031,6 +1031,7 @@ export function QuestionsStep({
|
||||||
questionText: String(row.text ?? "").trim(),
|
questionText: String(row.text ?? "").trim(),
|
||||||
questionTypeDefinitionId: Number(row.questionTypeDefinitionId),
|
questionTypeDefinitionId: Number(row.questionTypeDefinitionId),
|
||||||
dynamicFieldValues: { ...(row.dynamicFieldValues ?? {}) },
|
dynamicFieldValues: { ...(row.dynamicFieldValues ?? {}) },
|
||||||
|
sourceDynamicPayload: row.sourceDynamicPayload ?? null,
|
||||||
displayOrder: row.displayOrder,
|
displayOrder: row.displayOrder,
|
||||||
associatedQuestionId: row.associatedQuestionId ?? null,
|
associatedQuestionId: row.associatedQuestionId ?? null,
|
||||||
associatedAnchorRowId: row.associatedAnchorRowId ?? null,
|
associatedAnchorRowId: row.associatedAnchorRowId ?? null,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user