From 091e120224061968a721a1a20484a35716dab30d Mon Sep 17 00:00:00 2001 From: Muhammad Farhan Date: Mon, 10 Mar 2025 16:19:09 +0500 Subject: [PATCH] fix: Use defaultValue when item is null or empty --- .../components/EditProblemView/hooks.test.js | 4 ++-- .../containers/ProblemEditor/data/SettingsParser.js | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/editors/containers/ProblemEditor/components/EditProblemView/hooks.test.js b/src/editors/containers/ProblemEditor/components/EditProblemView/hooks.test.js index bf75e3cfa..e45206e1c 100644 --- a/src/editors/containers/ProblemEditor/components/EditProblemView/hooks.test.js +++ b/src/editors/containers/ProblemEditor/components/EditProblemView/hooks.test.js @@ -280,7 +280,7 @@ describe('EditProblemView hooks parseState', () => { const lmsEndpointUrl = 'someUrl'; const editorRef = refMock; const expectedSettings = { - max_attempts: '', + max_attempts: null, weight: 1, rerandomize: null, showanswer: ShowAnswerTypesKeys.AFTER_SOME_NUMBER_OF_ATTEMPTS, @@ -328,7 +328,7 @@ describe('EditProblemView hooks parseState', () => { }); expect(settings).toEqual({ max_attempts: '', - rerandomize: null, + rerandomize: 'never', show_reset_button: false, showanswer: 'after_attempts', attempts_before_showanswer_button: 0, diff --git a/src/editors/containers/ProblemEditor/data/SettingsParser.js b/src/editors/containers/ProblemEditor/data/SettingsParser.js index 1c9047067..d2b886358 100644 --- a/src/editors/containers/ProblemEditor/data/SettingsParser.js +++ b/src/editors/containers/ProblemEditor/data/SettingsParser.js @@ -5,11 +5,12 @@ import { ShowAnswerTypes, RandomizationTypesKeys } from '../../../data/constants export const popuplateItem = (parentObject, itemName, statekey, metadata, defaultValue = null, allowNull = false) => { let parent = parentObject; const item = _.get(metadata, itemName, null); - const equalsDefault = item === defaultValue; - if (allowNull) { - parent = { ...parentObject, [statekey]: item }; - } else if (!_.isNil(item) && !equalsDefault) { - parent = { ...parentObject, [statekey]: item }; + + // if item is null, undefined, or empty string, use defaultValue + const finalValue = (!_.isNil(item) && item !== '') ? item : defaultValue; + + if (allowNull || (!_.isNil(finalValue) && finalValue !== defaultValue)) { + parent = { ...parentObject, [statekey]: finalValue }; } return parent; };