From 53d4ab527a8ab46626a5cede50ebfdfb3a5678de Mon Sep 17 00:00:00 2001 From: aronmal Date: Sat, 8 Apr 2023 16:54:15 +0200 Subject: [PATCH] Reworked login page --- leaky-ships/pages/dev/login.tsx | 153 +++++++++++++++++++++++--------- 1 file changed, 113 insertions(+), 40 deletions(-) diff --git a/leaky-ships/pages/dev/login.tsx b/leaky-ships/pages/dev/login.tsx index e5dd6ca..c5283ed 100644 --- a/leaky-ships/pages/dev/login.tsx +++ b/leaky-ships/pages/dev/login.tsx @@ -1,9 +1,117 @@ -import { faSpinnerThird } from "@fortawesome/pro-solid-svg-icons" +import { + faArrowLeft, + faCheck, + faSpinnerThird, + faXmark, +} from "@fortawesome/pro-solid-svg-icons" +import { faWifiExclamation } from "@fortawesome/pro-duotone-svg-icons" import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" -import { useState } from "react" +import { FormEvent, useState } from "react" +import classNames from "classnames" function Login() { - const [loading, setLoading] = useState(false) + const [username, setUsername] = useState("") + const [password, setPassword] = useState("") + type ProcessStates = "waiting" | "loading" | "success" | "wrong" | "error" + const [state, setState] = useState("waiting") + const [error, setError] = useState("") + + const messages: { [key in ProcessStates]: string } = { + waiting: "Enter Login Details", + loading: "Logging in...", + success: "Done!", + wrong: "Wrong username or password", + error: "An error occured!", + } + const elem = () => { + if (state === "waiting") + return ( +
+
+ setUsername(e.target.value)} + /> +
+ +
+ setPassword(e.target.value)} + /> +
+
+ +
+
+ ) + + const icons = { + loading: faSpinnerThird, + success: faCheck, + wrong: faXmark, + error: faWifiExclamation, + } + const iconClasses = { + loading: "animate-spin", + success: "text-green-500", + wrong: "text-red-500", + error: "animate-pulse text-amber-500 !text-8xl", + } + return ( + <> +
+ +
+
+ +
+ + ) + } + + async function login(e: FormEvent) { + e.preventDefault() + setState("loading") + await fetch("/api/login", { + method: "POST", + body: JSON.stringify({ username, password }), + }) + .then((res) => { + if (res.status === 200) setState("success") + if (res.status === 401) setState("wrong") + }) + .catch((err: Error) => { + setState("error") + setError(err.message) + }) + } return (
@@ -18,45 +126,10 @@ function Login() { />

Leaky Ships

- {loading ? "Logging in..." : "Enter Login Details"} + {error ? error : messages[state]}
- {loading ? ( - setLoading(false)} - /> - ) : ( -
-
- -
- -
- -
-
- -
-
- )} + {elem()}