fix: Use defaultValue when item is null or empty

This commit is contained in:
Muhammad Farhan
2025-03-10 16:19:09 +05:00
committed by Muhammad Farhan
parent 1174b09ac4
commit 091e120224
2 changed files with 8 additions and 7 deletions

View File

@@ -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,

View File

@@ -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;
};