From 9f3685caede89a888e93853557e3a6ec36fd4cef Mon Sep 17 00:00:00 2001 From: Imbus Date: Wed, 15 Nov 2023 14:21:14 +0100 Subject: [PATCH] Breaking apart some large components --- client-solid/package.json | 2 +- client-solid/src/GlobalState.tsx | 16 ++++-- client-solid/src/LoginButton.tsx | 23 +++++++++ client-solid/src/LoginModal.tsx | 6 +-- client-solid/src/Menu.tsx | 36 ++++++++++++++ client-solid/src/Navbar.tsx | 56 ++------------------- client-solid/src/RegLogin/Login.tsx | 66 +++++++++--------------- client-solid/src/RegLogin/Register.tsx | 69 ++++++++++---------------- client-solid/src/api.ts | 36 ++++++++++++++ 9 files changed, 165 insertions(+), 145 deletions(-) create mode 100644 client-solid/src/LoginButton.tsx create mode 100644 client-solid/src/Menu.tsx diff --git a/client-solid/package.json b/client-solid/package.json index fd79256..d82d4c0 100644 --- a/client-solid/package.json +++ b/client-solid/package.json @@ -34,4 +34,4 @@ "vite-plugin-qrcode": "^0.2.2", "vite-plugin-solid": "^2.7.2" } -} +} \ No newline at end of file diff --git a/client-solid/src/GlobalState.tsx b/client-solid/src/GlobalState.tsx index 4cb3476..b70596b 100644 --- a/client-solid/src/GlobalState.tsx +++ b/client-solid/src/GlobalState.tsx @@ -10,8 +10,8 @@ import { // So far we only have one modal, but we can add more later // by adding more fields to this interface, or maybe an enum interface ModalContextType { - loginModalOpen: Accessor; - setLoginModalOpen: (value: boolean) => void; + isOpen: Accessor; + setOpen: (value: boolean) => void; } interface LoginContextType { @@ -21,6 +21,7 @@ interface LoginContextType { setUsername: (value: string) => void; loggedIn: () => boolean; logOut: () => void; + logIn: (username: string, token: string) => void; } // It is unclear to me if this is the idiomatic way to do this in Solid @@ -46,6 +47,13 @@ export function GlobalStateProvider(props: { return token() != "" && username() != ""; } + function logIn (username: string, token: string): void { + setUsername(username); + setToken(token); + localStorage.setItem("token", token); + localStorage.setItem("username", username); + } + function logOut(): void { localStorage.removeItem("token"); localStorage.removeItem("username"); @@ -55,9 +63,9 @@ export function GlobalStateProvider(props: { return ( <> - + {props.children} diff --git a/client-solid/src/LoginButton.tsx b/client-solid/src/LoginButton.tsx new file mode 100644 index 0000000..d4867bf --- /dev/null +++ b/client-solid/src/LoginButton.tsx @@ -0,0 +1,23 @@ +import { JSXElement, Show, useContext } from "solid-js"; + +import { LoginContext, ModalContext } from "./GlobalState"; +import { UserCircle } from "./Icons"; + +export function LoginButton(): JSXElement { + const modal_ctx = useContext(ModalContext)!; + const login_ctx = useContext(LoginContext)!; + + const clickHandler = (): void => { + if (login_ctx.loggedIn()) login_ctx.logOut(); + else modal_ctx.setOpen(true); + }; + + return ( +
+ + {login_ctx.username()} + + +
+ ); +} diff --git a/client-solid/src/LoginModal.tsx b/client-solid/src/LoginModal.tsx index 4266145..f7d5541 100644 --- a/client-solid/src/LoginModal.tsx +++ b/client-solid/src/LoginModal.tsx @@ -8,16 +8,16 @@ export function LoginModal(): JSXElement { const modal_ctx = useContext(ModalContext)!; const closeModal = (): void => { - modal_ctx.setLoginModalOpen(false); + modal_ctx.setOpen(false); }; // Close the modal when the component is unmounted onCleanup(() => { - modal_ctx.setLoginModalOpen(false); + modal_ctx.setOpen(false); }); return ( - +