-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from Ra3d0r/develop
v. 0.3.1 - Fix keyboard
- Loading branch information
Showing
10 changed files
with
145 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,11 @@ | ||
import {t} from 'i18next'; | ||
|
||
import {scoreActions} from '@feature/score/scoreSlice'; | ||
import {toastActions} from '@feature/toast/toastSlice'; | ||
import {typingActions} from '@feature/typing/typingSlice'; | ||
import postScore from '@feature/userScore/postScore'; | ||
|
||
import {textEng, textRu} from '../config'; | ||
import keyIdButtons from './keyIdButtons'; | ||
import randomTextFromArray from './randomTextFromArray'; | ||
import {typeHandleKeyUp} from './types/typeHandleKeyUp'; | ||
import checkKeyboardLayout from './utils/checkKeyboardLayout'; | ||
import deleteWords from './utils/deleteWords'; | ||
|
||
const { | ||
addCurrentText, | ||
addErrorIndex, | ||
changeStatusCustomMode, | ||
nextLetter, | ||
resetCustomModeText, | ||
previousLetter, | ||
} = typingActions; | ||
|
||
const {openToast} = toastActions; | ||
|
||
const {increaseTypos, updateCorrectness} = scoreActions; | ||
|
||
const handleKeyUp: typeHandleKeyUp = ({ | ||
event, | ||
setIsShiftPressed, | ||
setEventKeyCode, | ||
target, | ||
dispatch, | ||
currentText, | ||
currentTextIndex, | ||
mode, | ||
allText, | ||
isAuth, | ||
errorsIndex, | ||
lang, | ||
}) => { | ||
const handleKeyUp: typeHandleKeyUp = ({event, setIsShiftPressed, setEventKeyCode}) => { | ||
if (event.key === 'Shift') { | ||
setIsShiftPressed(false); | ||
} | ||
|
||
setEventKeyCode(''); | ||
|
||
if (!checkKeyboardLayout(lang, event.key)) { | ||
dispatch(openToast({message: t('changeKeyboard'), type: 'warning'})); | ||
return; | ||
} | ||
|
||
if (!target) { | ||
return; | ||
} | ||
|
||
const TargetKeyId = keyIdButtons(event.key); | ||
if (!TargetKeyId) { | ||
return; | ||
} | ||
|
||
if (!currentText.length) { | ||
return; | ||
} | ||
|
||
if (event.key === 'Backspace') { | ||
dispatch(previousLetter({mode, currentTextIndex})); | ||
dispatch(updateCorrectness({mode, errorsIndex, currentTextIndex})); | ||
return; | ||
} | ||
|
||
if (target !== event.key) { | ||
dispatch(addErrorIndex({currentTextIndex, mode})); | ||
dispatch(increaseTypos({mode})); | ||
dispatch(updateCorrectness({mode, errorsIndex, currentTextIndex, isIncreaseTypos: true})); | ||
} | ||
|
||
if (mode === 'custom' && currentTextIndex === currentText.length - 1) { | ||
isAuth && dispatch(postScore({mode, lang})); | ||
dispatch(changeStatusCustomMode('idle')); | ||
dispatch(resetCustomModeText()); | ||
return; | ||
} | ||
|
||
if (currentTextIndex < currentText.length - 1) { | ||
dispatch(nextLetter({mode})); | ||
return; | ||
} | ||
|
||
if (mode !== 'custom') { | ||
const text = randomTextFromArray( | ||
allText, | ||
mode, | ||
lang === 'en' ? textEng.textKey : textRu.textKey, | ||
); | ||
isAuth && dispatch(postScore({mode, lang})); | ||
const changedText = lang === 'ru' ? deleteWords(text, '\r') : text; | ||
dispatch(addCurrentText({text: changedText, mode})); | ||
return; | ||
} | ||
|
||
throw new Error('Incorrect next text or letter'); | ||
}; | ||
|
||
export default handleKeyUp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,25 @@ | ||
import {TFunction} from 'i18next'; | ||
|
||
import {typeStatus} from '@types'; | ||
import {typeLang, typeModeUnion} from '@types'; | ||
|
||
import {AppDispatch} from '@store/store'; | ||
|
||
export type typeHandleKeyDown = (arg: { | ||
event: KeyboardEvent; | ||
setIsShiftPressed: React.Dispatch<React.SetStateAction<boolean>>; | ||
setEventKeyCode: React.Dispatch<React.SetStateAction<string>>; | ||
status: typeStatus; | ||
target: string | undefined; | ||
dispatch: AppDispatch; | ||
currentText: string[]; | ||
currentTextIndex: number; | ||
mode: typeModeUnion; | ||
allText: string[] | Record<string, unknown>[]; | ||
isAuth: boolean; | ||
errorsIndex: Record<number, 'error'>; | ||
lang: typeLang; | ||
t: TFunction<'translation'>; | ||
}) => void; | ||
|
||
export default typeHandleKeyDown; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,7 @@ | ||
import {TFunction} from 'i18next'; | ||
|
||
import {typeLang, typeModeUnion} from '@types'; | ||
|
||
import {AppDispatch} from '@store/store'; | ||
|
||
interface params { | ||
event: KeyboardEvent; | ||
setIsShiftPressed: React.Dispatch<React.SetStateAction<boolean>>; | ||
setEventKeyCode: React.Dispatch<React.SetStateAction<string>>; | ||
target: string | undefined; | ||
dispatch: AppDispatch; | ||
currentText: string[]; | ||
currentTextIndex: number; | ||
mode: typeModeUnion; | ||
allText: string[] | Record<string, unknown>[]; | ||
isAuth: boolean; | ||
errorsIndex: Record<number, 'error'>; | ||
lang: typeLang; | ||
t: TFunction<'translation'>; | ||
} | ||
|
||
export type typeHandleKeyUp = (arg: params) => void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters