fix: Use defaultValue when item is null or empty
This commit is contained in:
committed by
Muhammad Farhan
parent
1174b09ac4
commit
091e120224
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user