before ai
This commit is contained in:
1 parent
85d0d0be57
commit
be50463157
11 files changed
+205
-76
No files matched your search
@@ -1,10 +1,88 @@
|
||||
"use client";
|
||||
|
||||
import { ChangeEvent, SyntheticEvent, useEffect, useRef, useState } from "react";
|
||||
import styles from "../../css/WelcomeScreen.module.css";
|
||||
import ChargedButton from "../ChargedButton";
|
||||
|
||||
const CODE_LENGTH = 9;
|
||||
const COOLDOWN_MS = 3000;
|
||||
|
||||
export default function WelcomeScreen() {
|
||||
console.log(styles);
|
||||
return(
|
||||
<div className="screen-container">
|
||||
|
||||
</div>
|
||||
)
|
||||
}
|
||||
const [tourCode, setTourCode] = useState("");
|
||||
const [lastSubmitTs, setLastSubmitTs] = useState(0);
|
||||
const [isCooldown, setIsCooldown] = useState(false);
|
||||
const tourCodeRef = useRef("");
|
||||
const cooldownRef = useRef(-1);
|
||||
|
||||
const checkCooldown = () => {
|
||||
if (cooldownRef.current === -1) return false;
|
||||
return Date.now() - cooldownRef.current <= COOLDOWN_MS
|
||||
};
|
||||
|
||||
const isTourCodeValid = () => {
|
||||
return tourCode.length === CODE_LENGTH;
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (checkCooldown()) {
|
||||
setIsCooldown(true);
|
||||
const inputTimeout = setTimeout(() => {
|
||||
console.log("Cooldown over");
|
||||
setIsCooldown(false);
|
||||
}, 3000);
|
||||
return () => clearTimeout(inputTimeout);
|
||||
} else {
|
||||
setIsCooldown(false);
|
||||
}
|
||||
}, [lastSubmitTs]);
|
||||
|
||||
const submit = () => {
|
||||
const value = tourCodeRef.current;
|
||||
|
||||
if (!isTourCodeValid() && checkCooldown()) {
|
||||
return;
|
||||
}
|
||||
|
||||
cooldownRef.current = Date.now();
|
||||
setLastSubmitTs(cooldownRef.current);
|
||||
|
||||
console.log("submitted", value);
|
||||
};
|
||||
|
||||
const handleChange = (event: ChangeEvent<HTMLInputElement>): boolean => {
|
||||
const value = event.target.value;
|
||||
|
||||
if (value.length > CODE_LENGTH) {
|
||||
event.preventDefault();
|
||||
return false;
|
||||
}
|
||||
tourCodeRef.current = value;
|
||||
setTourCode(value.toUpperCase().replaceAll(/\W/g, ""));
|
||||
return true;
|
||||
};
|
||||
|
||||
const buttonDisabled = isCooldown || !isTourCodeValid();
|
||||
|
||||
return (
|
||||
<div className="screen-container">
|
||||
<label className="screen__label">
|
||||
<input
|
||||
type="text"
|
||||
className="screen-input"
|
||||
value={tourCode}
|
||||
onChange={handleChange}
|
||||
aria-label="Tour Code"
|
||||
placeholder={`Enter a tour code (${CODE_LENGTH} characters max)`}
|
||||
/>
|
||||
</label>
|
||||
<ChargedButton
|
||||
holdDuration={1000}
|
||||
onAction={submit}
|
||||
className="screen-submit"
|
||||
disabled={buttonDisabled}
|
||||
>
|
||||
{(!buttonDisabled) ? "Halten zum Spielen!" : "Halten zum Spielen."}
|
||||
</ChargedButton>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user