mirror of
https://github.com/genemecija/learn-morse-code.git
synced 2026-04-21 06:03:56 +00:00
Challenge mode working; more bugs squashed
This commit is contained in:
parent
43255cf792
commit
69b910bf5e
17 changed files with 395 additions and 148 deletions
46
src/contexts/wordFeederContext.js
Normal file
46
src/contexts/wordFeederContext.js
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import React, {useState, useContext} from "react"
|
||||
import { WordListPickerContext } from "./wordListPickerContext"
|
||||
const WordFeederContext = React.createContext()
|
||||
|
||||
function WordFeederContextProvider(props) {
|
||||
|
||||
// let wordList = ['hi', 'morse', 'code', 'hello', 'gene']
|
||||
const {wordList} = useContext(WordListPickerContext)
|
||||
|
||||
const [wordIndex, setWordIndex] = useState(0)
|
||||
const [order, setOrder] = useState('sequential')
|
||||
let indexCache = []
|
||||
|
||||
let word = wordList[wordIndex]
|
||||
|
||||
function resetFeeder() {
|
||||
setWordIndex(0)
|
||||
indexCache = []
|
||||
}
|
||||
|
||||
function getNextWord() {
|
||||
if (order === 'sequential') {
|
||||
setWordIndex(prev => prev + 1)
|
||||
word = wordList[wordIndex] || null
|
||||
} else if (order === 'random') {
|
||||
let index = Math.floor(Math.random*wordList.length)
|
||||
indexCache.push(index)
|
||||
// Need to account for what to do when all words in wordList have been exhausted
|
||||
while (indexCache.includes(index) && wordList.length !== indexCache.length) {
|
||||
index = Math.floor(Math.random*wordList.length)
|
||||
indexCache.push(index)
|
||||
}
|
||||
setWordIndex(index)
|
||||
|
||||
word = wordList[wordIndex]
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<WordFeederContext.Provider value={{word: word, getNextWord: getNextWord, resetFeeder: resetFeeder}}>
|
||||
{props.children}
|
||||
</WordFeederContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export {WordFeederContextProvider, WordFeederContext}
|
||||
30
src/contexts/wordListPickerContext.js
Normal file
30
src/contexts/wordListPickerContext.js
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import React, {useState} from "react"
|
||||
import alphabet from '../data/alphabet.json'
|
||||
import common100 from '../data/common100.json'
|
||||
|
||||
|
||||
const WordListPickerContext = React.createContext()
|
||||
|
||||
function WordListPickerContextProvider(props) {
|
||||
|
||||
const [wordListCategory, setWordListCategory] = useState('alphabet')
|
||||
let wordList = []
|
||||
const testList = ['gene', 'anya', 'ali', 'liam', 'last']
|
||||
wordList = testList
|
||||
|
||||
if (wordListCategory === 'alphabet') {
|
||||
wordList = alphabet.words
|
||||
} else if (wordListCategory === 'common100') {
|
||||
wordList = common100.words
|
||||
} else if (wordListCategory === 'test') {
|
||||
wordList = testList
|
||||
}
|
||||
|
||||
return (
|
||||
<WordListPickerContext.Provider value={{wordList: wordList, setWordListCategory: setWordListCategory}}>
|
||||
{props.children}
|
||||
</WordListPickerContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export {WordListPickerContextProvider, WordListPickerContext}
|
||||
Loading…
Add table
Add a link
Reference in a new issue