2021-07-21 22:04:23 +02:00
import React , { useEffect , useRef , useState } from "react" ;
2021-07-21 02:21:00 +02:00
import "../../App.css" ;
import Footer from "../../Footer" ;
import InputField from "../InputField" ;
import SubmitField from "../SubmitField" ;
2021-07-23 01:00:14 +02:00
import ErrorMessage from "../ErrorMessage" ;
2021-07-21 02:21:00 +02:00
import { Logger } from "@behametrics/logger-web" ;
2021-07-22 03:11:28 +02:00
import { Button } from "../Button" ;
2021-07-21 02:21:00 +02:00
import BehaviorStudyInfo from "../BehaviorStudyInfo" ;
import BehaviorNormal from "../BehaviorNormal" ;
2021-07-21 23:41:10 +02:00
import BehaviorPhone from "../BehaviorPhone" ;
import BehaviorStanding from "../BehaviorStanding" ;
2021-07-22 02:40:17 +02:00
import BehaviorStudyEnd from "../BehaviorStudyEnd" ;
2021-07-22 03:11:28 +02:00
import { authFetch } from "../../auth/AuthProvider" ;
2021-07-21 02:21:00 +02:00
export default function Study ( ) {
2021-07-21 22:04:23 +02:00
const _logger = useRef ( 0 ) ;
2021-07-22 02:41:24 +02:00
const [ serverUsername , setServerUsername ] = useState ( "" ) ;
2021-07-22 19:55:06 +02:00
const [ genPassword , setGenPassword ] = useState ( "" ) ;
const STATES = {
START : "start" ,
NORMAL : "normal" ,
PHONE : "phone" ,
STANDING : "standing" ,
END : "end" ,
} ;
const [ state , setState ] = useState ( STATES . START ) ;
2021-07-23 01:00:14 +02:00
const [ isInputCorrect , setIsInputCorrect ] = useState ( true ) ;
const errorText =
"Das Passwort und der Benutzername stimmen nicht überein. Bitte prüfen Sie, dass Sie das Passwort richtig abgetippt und den Benutzernamen richtig eingegeben haben." ;
2021-07-21 21:53:57 +02:00
useEffect ( ( ) => {
2021-07-21 22:04:23 +02:00
_logger . current = new Logger ( {
2021-07-24 01:14:25 +02:00
inputs : [ "keyboard" , "wheel" , "cursor" , "custom" ] ,
apiUrl : "https://behavior.marcocamenzind.ch" ,
2021-07-21 21:53:57 +02:00
} ) ;
2021-07-21 22:04:23 +02:00
_logger . current . init ( ) ;
2021-07-22 03:11:28 +02:00
authFetch ( "/api/username" , {
2021-07-22 02:41:24 +02:00
method : "get" ,
} ) . then ( ( response ) => {
response . json ( ) . then ( ( resp ) => {
2021-07-22 03:11:28 +02:00
setServerUsername ( resp . username ) ;
2021-07-22 02:41:24 +02:00
} ) ;
} ) ;
2021-07-21 21:53:57 +02:00
} , [ ] ) ;
2021-07-21 02:21:00 +02:00
let username = "" ;
const setUsername = ( tmp _username ) => {
username = tmp _username ;
} ;
let password = "" ;
const setPassword = ( tmp _password ) => {
password = tmp _password ;
} ;
const handleLoggerOff = ( ) => {
2021-07-21 22:04:23 +02:00
_logger . current . stop ( ) ;
2021-07-21 02:21:00 +02:00
console . log ( "Logger ausgeschaltet" ) ;
} ;
const handleLoggerOn = ( ) => {
2021-07-21 22:04:23 +02:00
_logger . current . start ( ) ;
2021-07-21 02:21:00 +02:00
console . log ( "start logging " ) ;
} ;
const handlePasswordChange = ( e ) => {
setPassword ( e . target . value ) ;
} ;
const handleUsernameChange = ( e ) => {
setUsername ( e . target . value ) ;
} ;
const handleOnPasteEvent = ( e ) => {
e . preventDefault ( ) ;
return false ;
} ;
2021-07-23 00:59:39 +02:00
const handleOnCopyEvent = ( e ) => {
e . preventDefault ( ) ;
return false ;
} ;
2021-07-21 02:21:00 +02:00
2021-07-22 03:11:28 +02:00
const checkIfUsernameIsCorrect = ( ) => {
2021-07-22 19:55:06 +02:00
return serverUsername === username ;
2021-07-22 03:11:28 +02:00
} ;
2021-07-22 04:44:31 +02:00
const receiveRandomPassword = ( ) => {
fetch ( "/api/rcv_pw" , {
method : "get" ,
} ) . then ( ( response ) => {
response . json ( ) . then ( ( resp ) => {
2021-07-22 16:53:08 +02:00
console . log ( resp . random _password ) ;
setGenPassword ( resp . random _password ) ;
2021-07-22 04:44:31 +02:00
} ) ;
} ) ;
} ;
const checkIfPasswordIsCorrect = ( ) => {
2021-07-22 19:55:06 +02:00
return genPassword === password ;
2021-07-22 04:44:31 +02:00
} ;
2021-07-22 03:11:28 +02:00
const checkIfValuesAreCorrect = ( ) => {
2021-07-22 19:55:06 +02:00
return checkIfPasswordIsCorrect ( ) && checkIfUsernameIsCorrect ( ) ;
2021-07-22 02:41:24 +02:00
} ;
2021-07-21 23:41:10 +02:00
const handleClickAtStepStart = ( ) => {
2021-07-22 16:53:08 +02:00
receiveRandomPassword ( ) ;
2021-07-22 19:55:06 +02:00
setState ( STATES . NORMAL ) ;
2021-07-21 22:38:31 +02:00
handleLoggerOn ( ) ;
2021-07-24 01:14:25 +02:00
// map username and session with the logs
2021-07-24 02:11:17 +02:00
_logger . current . logCustomEvent ( {
name : serverUsername ,
} ) ;
2021-07-21 04:33:52 +02:00
} ;
2021-07-23 01:00:14 +02:00
const resetInputValues = ( ) => {
2021-07-24 01:10:40 +02:00
let inputElements = document . querySelectorAll (
'div input:not([type="submit"])'
) ;
for ( let i = 0 ; i < inputElements . length ; i ++ ) {
inputElements [ i ] . value = "" ;
2021-07-23 01:00:14 +02:00
}
} ;
2021-07-21 23:41:10 +02:00
const handleClickAtStepNormal = ( ) => {
2021-07-22 05:20:37 +02:00
if ( checkIfValuesAreCorrect ( ) ) {
2021-07-22 04:44:31 +02:00
receiveRandomPassword ( ) ;
2021-07-23 01:00:14 +02:00
setIsInputCorrect ( true ) ;
2021-07-22 19:55:06 +02:00
setState ( STATES . PHONE ) ;
2021-07-23 01:00:14 +02:00
} else {
setIsInputCorrect ( false ) ;
resetInputValues ( ) ;
2021-07-22 04:44:31 +02:00
}
2021-07-21 23:41:10 +02:00
} ;
const handleClickAtStepPhone = ( ) => {
2021-07-22 19:01:40 +02:00
if ( checkIfValuesAreCorrect ( ) ) {
receiveRandomPassword ( ) ;
2021-07-23 01:00:14 +02:00
setIsInputCorrect ( true ) ;
2021-07-22 19:55:06 +02:00
setState ( STATES . STANDING ) ;
2021-07-22 19:01:40 +02:00
} else {
2021-07-23 01:00:14 +02:00
setIsInputCorrect ( false ) ;
resetInputValues ( ) ;
2021-07-22 16:53:08 +02:00
}
2021-07-21 23:41:10 +02:00
} ;
const handleClickAtStepStanding = ( ) => {
2021-07-22 19:01:40 +02:00
if ( checkIfValuesAreCorrect ( ) ) {
2021-07-23 01:00:14 +02:00
setIsInputCorrect ( true ) ;
2021-07-22 19:55:06 +02:00
setState ( STATES . END ) ;
2021-07-22 19:01:40 +02:00
handleLoggerOff ( ) ;
} else {
2021-07-23 01:00:14 +02:00
setIsInputCorrect ( false ) ;
resetInputValues ( ) ;
2021-07-22 16:53:08 +02:00
}
2021-07-21 23:41:10 +02:00
} ;
const study _start = (
< >
< BehaviorStudyInfo / >
< Button
className = "btns"
buttonStyle = "btn--primary"
buttonSize = "btn--full"
onClick = { handleClickAtStepStart }
newTo = "study"
>
2021-07-22 07:29:54 +02:00
Studie starten
2021-07-21 23:41:10 +02:00
< / Button >
< / >
) ;
const study _normal = (
< >
2021-07-23 01:00:14 +02:00
< BehaviorNormal onCopy = { handleOnCopyEvent } genPassword = { genPassword } / >
{ ! isInputCorrect && < ErrorMessage message = { errorText } / > }
< div >
2021-07-21 23:41:10 +02:00
< InputField
LabelName = "Benutzername"
onChange = { handleUsernameChange }
InputType = "text"
InputName = "Benutzername"
InputPlaceHolder = "Benutzername"
onPaste = { handleOnPasteEvent }
/ >
< InputField
LabelName = "Passwort"
onChange = { handlePasswordChange }
InputType = "password"
InputName = "Passwort"
2021-07-23 00:58:34 +02:00
InputPlaceHolder = "Passwort"
2021-07-21 23:41:10 +02:00
onPaste = { handleOnPasteEvent }
/ >
< SubmitField
LabelName = "Weiter zur nächsten Situation"
InputValue = "next-situation"
InputName = "Weiter"
onClick = { handleClickAtStepNormal }
/ >
2021-07-23 01:00:14 +02:00
< / div >
2021-07-21 23:41:10 +02:00
< / >
) ;
const study _phone = (
< >
2021-07-22 16:53:57 +02:00
< BehaviorPhone genPassword = { genPassword } / >
2021-07-23 01:00:14 +02:00
{ ! isInputCorrect && < ErrorMessage message = { errorText } / > }
< div >
2021-07-21 23:41:10 +02:00
< InputField
LabelName = "Benutzername"
onChange = { handleUsernameChange }
InputType = "text"
InputName = "Benutzername"
InputPlaceHolder = "Benutzername"
onPaste = { handleOnPasteEvent }
/ >
< InputField
LabelName = "Passwort"
onChange = { handlePasswordChange }
InputType = "password"
InputName = "Passwort"
2021-07-23 00:58:34 +02:00
InputPlaceHolder = "Passwort"
2021-07-21 23:41:10 +02:00
onPaste = { handleOnPasteEvent }
/ >
< SubmitField
LabelName = "Weiter zur nächsten Situation"
InputValue = "next-situation"
InputName = "Weiter"
onClick = { handleClickAtStepPhone }
/ >
2021-07-23 01:00:14 +02:00
< / div >
2021-07-21 23:41:10 +02:00
< / >
) ;
const study _standing = (
< >
2021-07-22 16:53:57 +02:00
< BehaviorStanding genPassword = { genPassword } / >
2021-07-23 01:00:14 +02:00
{ ! isInputCorrect && < ErrorMessage message = { errorText } / > }
< div >
2021-07-21 23:41:10 +02:00
< InputField
LabelName = "Benutzername"
onChange = { handleUsernameChange }
InputType = "text"
InputName = "Benutzername"
InputPlaceHolder = "Benutzername"
onPaste = { handleOnPasteEvent }
/ >
< InputField
LabelName = "Passwort"
onChange = { handlePasswordChange }
InputType = "password"
InputName = "Passwort"
2021-07-22 19:55:06 +02:00
InputPlaceHolder = "Passwort"
2021-07-21 23:41:10 +02:00
onPaste = { handleOnPasteEvent }
/ >
< SubmitField
LabelName = "Studie beenden"
InputValue = "stopStudy"
InputName = "quit"
onClick = { handleClickAtStepStanding }
/ >
2021-07-23 01:00:14 +02:00
< / div >
2021-07-21 23:41:10 +02:00
< / >
) ;
2021-07-22 02:40:17 +02:00
const study _end = < BehaviorStudyEnd / > ;
2021-07-21 23:41:10 +02:00
2021-07-21 02:21:00 +02:00
return (
< >
< div className = "sitePage" >
< h1 > Studie < / h1 >
2021-07-22 19:55:06 +02:00
{ state === STATES . START ? study _start : null }
{ state === STATES . NORMAL ? study _normal : null }
{ state === STATES . PHONE ? study _phone : null }
{ state === STATES . STANDING ? study _standing : null }
{ state === STATES . END ? study _end : null }
2021-07-21 02:21:00 +02:00
< / div >
< Footer / >
< / >
) ;
}