refactor: improve hooks condition handling
This commit is contained in:
@@ -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 } } });
|
||||
};
|
||||
|
||||
|
||||
@@ -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 } });
|
||||
|
||||
@@ -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: {
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user