diff --git a/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.js b/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.js index 4183d5bf4..5cdc2c34a 100644 --- a/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.js +++ b/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.js @@ -109,7 +109,11 @@ export const resetCardHooks = (updateSettings) => { }; export const scoringCardHooks = (scoring, updateSettings, defaultValue) => { - const loadedAttemptsNumber = scoring.attempts.number === defaultValue ? `${scoring.attempts.number} (Default)` : scoring.attempts.number; + let loadedAttemptsNumber = scoring.attempts.number; + if (scoring.attempts.number === defaultValue) { + loadedAttemptsNumber = `${scoring.attempts.number} (Default)`; + updateSettings({ scoring: { ...scoring, attempts: { number: null, unlimited: false } } }); + } const [attemptDisplayValue, setAttemptDisplayValue] = module.state.attemptDisplayValue(loadedAttemptsNumber); const handleUnlimitedChange = (event) => { const isUnlimited = event.target.checked; @@ -117,27 +121,26 @@ export const scoringCardHooks = (scoring, updateSettings, defaultValue) => { setAttemptDisplayValue(''); updateSettings({ scoring: { ...scoring, attempts: { number: null, unlimited: true } } }); } else { - setAttemptDisplayValue(`${defaultValue} (Default)`); updateSettings({ scoring: { ...scoring, attempts: { number: null, unlimited: false } } }); } }; + const handleMaxAttemptChange = (event) => { let unlimitedAttempts = false; let attemptNumber = parseInt(event.target.value); - const { value } = event.target; - // TODO: impove below condition handling - if (_.isNaN(attemptNumber) || _.isNil(attemptNumber)) { + + if (!_.isFinite(attemptNumber) || attemptNumber === defaultValue) { attemptNumber = null; - if (value === '' && !_.isNil(defaultValue)) { + if (_.isFinite(defaultValue)) { setAttemptDisplayValue(`${defaultValue} (Default)`); - } else if (_.isNil(defaultValue)) { + } else { + setAttemptDisplayValue(''); unlimitedAttempts = true; } } else if (attemptNumber <= 0) { attemptNumber = 0; - } else if (attemptNumber === defaultValue) { - attemptNumber = null; } + updateSettings({ scoring: { ...scoring, attempts: { number: attemptNumber, unlimited: unlimitedAttempts } } }); }; diff --git a/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.test.js b/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.test.js index 49dc36170..1fcc05a1b 100644 --- a/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.test.js +++ b/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.test.js @@ -159,7 +159,6 @@ describe('Problem settings hooks', () => { }); test('test handleUnlimitedChange sets attempts.unlimited to false when unchecked', () => { output.handleUnlimitedChange({ target: { checked: false } }); - expect(state.setState[state.keys.attemptDisplayValue]).toHaveBeenCalledWith(`${defaultValue} (Default)`); expect(updateSettings) .toHaveBeenCalledWith({ scoring: { ...scoring, attempts: { number: null, unlimited: false } } }); }); @@ -206,6 +205,14 @@ describe('Problem settings hooks', () => { expect(updateSettings) .toHaveBeenCalledWith({ scoring: { ...scoring, attempts: { number: 0, unlimited: false } } }); }); + test('test handleMaxAttemptChange set attempts to empty value with no default', () => { + const value = ''; + output = hooks.scoringCardHooks(scoring, updateSettings, null); + output.handleMaxAttemptChange({ target: { value } }); + expect(state.setState[state.keys.attemptDisplayValue]).toHaveBeenCalledWith(''); + expect(updateSettings) + .toHaveBeenCalledWith({ scoring: { ...scoring, attempts: { number: null, unlimited: true } } }); + }); test('test handleOnChange', () => { const value = 6; output.handleOnChange({ target: { value } }); diff --git a/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/messages.js b/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/messages.js index 77b771f6c..26c5414a1 100644 --- a/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/messages.js +++ b/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/messages.js @@ -109,7 +109,7 @@ const messages = defineMessages({ }, attemptsHint: { id: 'authoring.problemeditor.settings.scoring.attempts.hint', - defaultMessage: 'If a value is not set, unlimited attempts are allowed', + defaultMessage: 'If a default value is not set in advanced settings, unlimited attempts are allowed', description: 'Summary text for scoring weight', }, weightHint: { diff --git a/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/settingsComponents/ScoringCard.jsx b/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/settingsComponents/ScoringCard.jsx index 17c2b2a4a..03a0ffb6b 100644 --- a/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/settingsComponents/ScoringCard.jsx +++ b/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/settingsComponents/ScoringCard.jsx @@ -33,7 +33,7 @@ export const ScoringCard = ({ summary += ` ${String.fromCharCode(183)} `; summary += unlimited ? intl.formatMessage(messages.unlimitedAttemptsSummary) - : intl.formatMessage(messages.attemptsSummary, { attempts }); + : intl.formatMessage(messages.attemptsSummary, { attempts: attempts ? attempts : defaultValue }); return summary; };