diff --git a/client/src/templates/Challenges/classic/lower-jaw.tsx b/client/src/templates/Challenges/classic/lower-jaw.tsx index c66c69ff19b..796a24368b1 100644 --- a/client/src/templates/Challenges/classic/lower-jaw.tsx +++ b/client/src/templates/Challenges/classic/lower-jaw.tsx @@ -39,6 +39,8 @@ const LowerJaw = ({ const { t } = useTranslation(); const submitButtonRef = React.createRef(); const testFeedbackRef = React.createRef(); + const [challengeHasBeenCompleted, setChallengeHasBeenCompleted] = + useState(false); useEffect(() => { if (attemptsNumber && attemptsNumber > 0) { @@ -69,13 +71,21 @@ const LowerJaw = ({ }, [challengeHasErrors, hint]); useEffect(() => { - if (challengeIsCompleted && submitButtonRef?.current) { + if (challengeHasBeenCompleted && submitButtonRef?.current) { submitButtonRef.current.focus(); setTimeout(() => { setTestBtnariaHidden(true); }, 500); } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [challengeHasBeenCompleted]); + + useEffect(() => { + if (!challengeHasBeenCompleted && challengeIsCompleted) { + setChallengeHasBeenCompleted(challengeIsCompleted); + } + // eslint-disable-next-line react-hooks/exhaustive-deps }, [challengeIsCompleted]); @@ -86,12 +96,22 @@ const LowerJaw = ({ } }); + /* + Retun early in lifecycle based on the earliest available conditions to help the editor + calcuate the correct editor gap for the lower jaw. + + For consistency, use the persisted version if the conditions has been met before. + */ + const earliestAvailableCompletion = + challengeHasBeenCompleted || challengeIsCompleted; + const earliestAvailableHint = hint || previousHint; + const renderTestFeedbackContainer = () => { if (attemptsNumber === 0) { return ''; } else if (runningTests) { return {t('aria.running-tests')}; - } else if (challengeIsCompleted) { + } else if (earliestAvailableCompletion) { const submitKeyboardInstructions = isEditorInFocus ? ( {t('aria.submit')} ) : ( @@ -113,12 +133,10 @@ const LowerJaw = ({ ); - - // show the hint if the previousHint is not set - } else if (hint || previousHint) { - const hintDiscription = `

${t('learn.hint')}

${ - hint || previousHint - }`; + } else if (earliestAvailableHint) { + const hintDiscription = `

${t( + 'learn.hint' + )}

${earliestAvailableHint}`; return ( <>
@@ -165,7 +183,7 @@ const LowerJaw = ({ const isAtteptsLargerThanTest = attemptsNumber && testsLength && attemptsNumber >= testsLength; - if (isAtteptsLargerThanTest && !challengeIsCompleted) + if (isAtteptsLargerThanTest && !earliestAvailableCompletion) return (

@@ -188,7 +206,9 @@ const LowerJaw = ({ <>