Bachelorthesis_Code/frontend/src/components/pages/Study.js

275 lines
6.9 KiB
JavaScript

import React, { useEffect, useRef, useState } from "react";
import "../../App.css";
import Footer from "../../Footer";
import InputField from "../InputField";
import SubmitField from "../SubmitField";
import { Logger } from "@behametrics/logger-web";
import { Button } from "../Button";
import BehaviorStudyInfo from "../BehaviorStudyInfo";
import BehaviorNormal from "../BehaviorNormal";
import BehaviorPhone from "../BehaviorPhone";
import BehaviorStanding from "../BehaviorStanding";
import BehaviorStudyEnd from "../BehaviorStudyEnd";
import { authFetch } from "../../auth/AuthProvider";
export default function Study() {
const _logger = useRef(0);
const [serverUsername, setServerUsername] = useState("");
useEffect(() => {
_logger.current = new Logger({
//inputs: ["cursor", "wheel", "keyboard", "touch"],
inputs: ["keyboard"],
// apiUrl: "https://behavior.marcocamenzind.ch",
apiUrl: "http://localhost:5000",
logToConsole: true,
});
_logger.current.init();
authFetch("/api/username", {
method: "get",
}).then((response) => {
response.json().then((resp) => {
setServerUsername(resp.username);
});
});
}, []);
let username = "";
const setUsername = (tmp_username) => {
username = tmp_username;
};
let password = "";
const setPassword = (tmp_password) => {
password = tmp_password;
};
const [isStepStart, setIsStepStart] = useState(true);
const [isStepNormal, setIsStepNormal] = useState(false);
const [isStepPhone, setIsStepPhone] = useState(false);
const [isStepStanding, setIsStepStanding] = useState(false);
const [isStepEnd, setIsStepEnd] = useState(false);
const handleLoggerOff = () => {
_logger.current.stop();
console.log("Logger ausgeschaltet");
};
const handleLoggerOn = () => {
_logger.current.start();
console.log("start logging ");
};
const handlePasswordChange = (e) => {
setPassword(e.target.value);
};
const handleUsernameChange = (e) => {
setUsername(e.target.value);
};
const handleOnPasteEvent = (e) => {
e.preventDefault();
console.log("paste not allowed");
return false;
};
const checkIfUsernameIsCorrect = () => {
if (serverUsername === username) {
return true;
} else {
return false;
}
};
const [genPassword, setGenPassword] = useState("");
const receiveRandomPassword = () => {
fetch("/api/rcv_pw", {
method: "get",
}).then((response) => {
response.json().then((resp) => {
console.log(resp.random_password);
setGenPassword(resp.random_password);
console.log("rcv pw; print genPassword", genPassword);
});
});
};
const checkIfPasswordIsCorrect = () => {
if (genPassword === password) {
return true;
} else {
return false;
}
};
const checkIfValuesAreCorrect = () => {
console.log(checkIfPasswordIsCorrect());
if (checkIfPasswordIsCorrect() && checkIfUsernameIsCorrect()) {
return true;
} else {
alert("Passt nicht");
return false;
}
};
const handleClickAtStepStart = () => {
receiveRandomPassword();
setIsStepStart(false);
setIsStepNormal(true);
handleLoggerOn();
};
const handleClickAtStepNormal = () => {
if (checkIfValuesAreCorrect()) {
receiveRandomPassword();
setIsStepNormal(false);
setIsStepPhone(true);
}
};
const handleClickAtStepPhone = () => {
if (checkIfValuesAreCorrect()){
receiveRandomPassword();
setIsStepPhone(false);
setIsStepStanding(true);
}
else {
console.log("Passwort und Benutzername stimmen nicht.")
}
};
const handleClickAtStepStanding = () => {
if (checkIfValuesAreCorrect()){
setIsStepStanding(false);
setIsStepEnd(true);
handleLoggerOff();
}
else {
console.log("Passwort und Benutzername stimmen nicht.")
}
};
const study_start = (
<>
<BehaviorStudyInfo />
<Button
className="btns"
buttonStyle="btn--primary"
buttonSize="btn--full"
onClick={handleClickAtStepStart}
newTo="study"
>
Studie starten
</Button>
</>
);
const study_normal = (
<>
<BehaviorNormal genPassword={genPassword} />
<form id="behaviorNormal">
<InputField
LabelName="Benutzername"
onChange={handleUsernameChange}
InputType="text"
InputName="Benutzername"
InputPlaceHolder="Benutzername"
onPaste={handleOnPasteEvent}
/>
<InputField
LabelName="Passwort"
onChange={handlePasswordChange}
InputType="password"
InputName="Passwort"
InputPlaceHolder="Benutzername"
onPaste={handleOnPasteEvent}
/>
<SubmitField
LabelName="Weiter zur nächsten Situation"
InputValue="next-situation"
InputName="Weiter"
onClick={handleClickAtStepNormal}
/>
</form>
</>
);
const study_phone = (
<>
<BehaviorPhone genPassword={genPassword} />
<form id="behaviorPhone">
<InputField
LabelName="Benutzername"
onChange={handleUsernameChange}
InputType="text"
InputName="Benutzername"
InputPlaceHolder="Benutzername"
onPaste={handleOnPasteEvent}
/>
<InputField
LabelName="Passwort"
onChange={handlePasswordChange}
InputType="password"
InputName="Passwort"
InputPlaceHolder="Benutzername"
onPaste={handleOnPasteEvent}
/>
<SubmitField
LabelName="Weiter zur nächsten Situation"
InputValue="next-situation"
InputName="Weiter"
onClick={handleClickAtStepPhone}
/>
</form>
</>
);
const study_standing = (
<>
<BehaviorStanding genPassword={genPassword} />
<form id="behaviorStanding">
<InputField
LabelName="Benutzername"
onChange={handleUsernameChange}
InputType="text"
InputName="Benutzername"
InputPlaceHolder="Benutzername"
onPaste={handleOnPasteEvent}
/>
<InputField
LabelName="Passwort"
onChange={handlePasswordChange}
InputType="password"
InputName="Passwort"
InputPlaceHolder="Benutzername"
onPaste={handleOnPasteEvent}
/>
<SubmitField
LabelName="Studie beenden"
InputValue="stopStudy"
InputName="quit"
onClick={handleClickAtStepStanding}
/>
</form>
</>
);
const study_end = <BehaviorStudyEnd />;
return (
<>
<div className="sitePage">
<h1>Studie</h1>
{isStepStart ? study_start : null}
{isStepNormal ? study_normal : null}
{isStepPhone ? study_phone : null}
{isStepStanding ? study_standing : null}
{isStepEnd ? study_end : null}
</div>
<Footer />
</>
);
}