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 ( - +