11import React , { useEffect , useState } from 'react' ;
22import { shuffleArray , getRandomElement , GridItem , GridStatement ,
3- GridTemplate } from './utils' ;
3+ GridTemplate , Score } from './utils' ;
44import { getTemplatesByDifficulty } from './statementGenerator' ;
55import { Grid } from './grid' ;
66import { Options } from './options' ;
77import { StatementInput } from './statementInput' ;
8+ import { Progress } from './progress' ;
89
910export const STATIC_URL = LogicLearner . staticUrl ;
1011
@@ -21,6 +22,11 @@ export const FirstOrderLogic: React.FC = () => {
2122 const [ isCorrect , setIsCorrect ] = useState < boolean > ( false ) ;
2223 const [ selected , setSelected ] = useState < number | null > ( )
2324 const [ mode , setMode ] = useState < number > ( 0 ) ;
25+ const [ isDone , setIsDone ] = useState < boolean > ( false ) ;
26+ const [ attempt , setAttempt ] = useState < number > ( 4 ) ;
27+
28+ const scoreDefault = { easy :[ ] , medium : [ ] , hard : [ ] }
29+ const [ score , setScore ] = useState < Score > ( scoreDefault ) ;
2430
2531 const [ correctTemplate , setCorrectTemplate ] =
2632 useState < GridTemplate > ( getRandomElement ( templateBank ) ) ;
@@ -74,14 +80,31 @@ export const FirstOrderLogic: React.FC = () => {
7480 setMode ( Number ( e . target . value ) ) ;
7581 }
7682
83+ const handleAttempt = ( result :boolean ) => {
84+ if ( ! isDone ) {
85+ if ( result ) {
86+ setIsDone ( true ) ;
87+ setScore ( { ...score , [ difficulty ] : [ ...( score [ difficulty ] ) , attempt ] } ) ;
88+ } else {
89+ setAttempt ( Math . max ( attempt - 1 , 1 ) ) ;
90+ }
91+ }
92+ } ;
93+
7794 const handleNewGrid = ( ) => {
7895 let newArr = getRandomElement ( templateBank )
7996 while ( newArr === correctTemplate ) {
8097 newArr = getRandomElement ( templateBank )
8198 }
99+ if ( ! isDone ) {
100+ setScore ( { ...score ,
101+ [ difficulty ] : [ ...( score [ difficulty ] ) , - attempt ] } ) ;
102+ }
82103 setCorrectTemplate ( newArr ) ;
83104 setSelected ( null ) ;
84105 setText ( '' ) ;
106+ setAttempt ( 4 ) ;
107+ setIsDone ( false ) ;
85108 }
86109
87110 const mkSelect = ( options , action ) => < select className = 'form-select mt-2'
@@ -138,8 +161,19 @@ export const FirstOrderLogic: React.FC = () => {
138161 }
139162 } , [ correctStatement ] ) ;
140163
164+ useEffect ( ( ) => {
165+ if ( score != scoreDefault ) {
166+ localStorage . setItem ( 'fol' , JSON . stringify ( { score, attempt} ) ) ;
167+ }
168+ } , [ score , attempt ] ) ;
169+
141170 useEffect ( ( ) => {
142171 setSelected ( null ) ;
172+ const store = JSON . parse ( localStorage . getItem ( 'fol' ) ) ;
173+ if ( store ) {
174+ setAttempt ( store . attempt ?? 4 ) ;
175+ setScore ( store . score ?? scoreDefault ) ;
176+ }
143177 } , [ ] ) ;
144178
145179 useEffect ( ( ) => {
@@ -158,10 +192,13 @@ export const FirstOrderLogic: React.FC = () => {
158192 { setting }
159193 </ li > ) }
160194 </ ul >
195+ { mode === 0 &&
196+ < Progress difficulty = { difficulty } score = { score } /> }
161197 </ div >
162- { mode === 0 && < Options options = { options } correctIndex = { correctIndex }
163- isCorrect = { isCorrect } setIsCorrect = { setIsCorrect }
164- selected = { selected } setSelected = { setSelected } /> }
198+ { mode === 0 && < Options options = { options }
199+ correctIndex = { correctIndex } isCorrect = { isCorrect }
200+ setIsCorrect = { setIsCorrect } selected = { selected }
201+ setSelected = { setSelected } handleAttempt = { handleAttempt } /> }
165202 { mode === 1 && < StatementInput isCorrect = { isCorrect }
166203 correctStatement = { correctStatement } difficulty = { difficulty }
167204 setIsCorrect = { setIsCorrect } text = { text } setText = { setText } /> }
0 commit comments