@@ -21,13 +21,42 @@ import Answers from './fillInTheBlanks/Answers';
2121import BlankedText from './fillInTheBlanks/BlankedText' ;
2222import Correction from './fillInTheBlanks/Correction' ;
2323
24+ type State = { answers : Word [ ] ; words : Word [ ] } ;
25+
26+ const isWordCorrect = ( w : Word ) => w . displayed === w . text ;
27+
28+ const resetWrongAnswers = ( state : State ) : State => {
29+ const newWords = state . words . map ( ( w ) => {
30+ const isCorrect = isWordCorrect ( w ) ;
31+
32+ // Type word cannot be placed
33+ if ( w . type === 'word' ) {
34+ return w ;
35+ }
36+
37+ return {
38+ ...w ,
39+ // set displayed to empty string to reset the answer
40+ displayed : isCorrect ? w . displayed : '' ,
41+ placed : isCorrect ,
42+ } ;
43+ } ) ;
44+ const newAnswers = state . answers . map ( ( a ) => ( {
45+ ...a ,
46+ placed : Boolean ( newWords . find ( ( w ) => w . id === a . id ) ?. displayed ) ,
47+ } ) ) ;
48+
49+ return { answers : newAnswers , words : newWords } ;
50+ } ;
51+
2452type Props = {
2553 showCorrection : boolean ;
2654 showCorrectness : boolean ;
2755 lastUserAnswer ?: FillTheBlanksAppDataData ;
2856 isReadonly : boolean ;
2957 values : FillTheBlanksAppSettingData ;
3058 response : FillTheBlanksAppDataData ;
59+ numberOfRetry : number ;
3160 setResponse : ( text : string ) => void ;
3261} ;
3362
@@ -38,6 +67,7 @@ const PlayFillInTheBlanks = ({
3867 isReadonly,
3968 values,
4069 response,
70+ numberOfRetry,
4171 setResponse,
4272} : Props ) => {
4373 const [ state , setState ] = useState < { answers : Word [ ] ; words : Word [ ] } > ( {
@@ -48,6 +78,20 @@ const PlayFillInTheBlanks = ({
4878 const { t } = useTranslation ( ) ;
4979 const [ prevWords , setPrevWords ] = useState < string [ ] > ( ) ;
5080
81+ const userCannotPlay = isReadonly || showCorrection || showCorrectness ;
82+
83+ useEffect ( ( ) => {
84+ console . log ( 'words' , state . words ) ;
85+ } , [ state . words ] ) ;
86+
87+ // reset wrong answers on retry
88+ useEffect ( ( ) => {
89+ const newState = resetWrongAnswers ( state ) ;
90+ setState ( newState ) ;
91+ saveResponse ( newState . words ) ;
92+ // eslint-disable-next-line react-hooks/exhaustive-deps
93+ } , [ numberOfRetry ] ) ;
94+
5195 useEffect ( ( ) => {
5296 if ( lastUserAnswer ) {
5397 const regExp = RegExp ( ANSWER_REGEXP ) ;
@@ -75,7 +119,7 @@ const PlayFillInTheBlanks = ({
75119 } ;
76120
77121 const onDelete = ( e : React . MouseEvent < HTMLSpanElement > ) => {
78- if ( isReadonly ) {
122+ if ( userCannotPlay ) {
79123 return ;
80124 }
81125
@@ -95,7 +139,7 @@ const PlayFillInTheBlanks = ({
95139 } ;
96140
97141 const onDrop = ( e : React . DragEvent < HTMLSpanElement > , dropId : number ) => {
98- if ( isReadonly ) {
142+ if ( userCannotPlay ) {
99143 return ;
100144 }
101145
@@ -132,17 +176,17 @@ const PlayFillInTheBlanks = ({
132176
133177 return (
134178 < Box width = "100%" >
135- < Answers answers = { state . answers } isReadonly = { isReadonly } />
179+ < Answers answers = { state . answers } isReadonly = { userCannotPlay } />
136180 < BlankedText
137181 showCorrection = { showCorrection }
138182 showCorrectness = { showCorrectness }
139- isReadonly = { isReadonly }
183+ isReadonly = { userCannotPlay }
140184 words = { state . words }
141185 prevWords = { prevWords }
142186 onDrop = { onDrop }
143187 onDelete = { onDelete }
144188 />
145- { ! showCorrection && prevWords && (
189+ { showCorrectness && ! isReadonly && (
146190 < Typography variant = "body1" color = "error" mt = { 2 } >
147191 { t ( QUIZ_TRANSLATIONS . RESPONSE_NOT_CORRECT ) }
148192 </ Typography >
0 commit comments