Somewhat cleaner state management
This commit is contained in:
parent
0af2764b25
commit
811f345960
7 changed files with 81 additions and 51 deletions
59
client-solid/src/GlobalState.tsx
Normal file
59
client-solid/src/GlobalState.tsx
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
import {
|
||||||
|
Accessor,
|
||||||
|
JSXElement,
|
||||||
|
createContext,
|
||||||
|
createSignal,
|
||||||
|
onMount,
|
||||||
|
} from "solid-js";
|
||||||
|
|
||||||
|
// Representing the state of varoious modals.
|
||||||
|
// 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<boolean>;
|
||||||
|
setLoginModalOpen: (value: boolean) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface LoginContextType {
|
||||||
|
token: Accessor<string>;
|
||||||
|
setToken: (value: string) => void;
|
||||||
|
username: Accessor<string>;
|
||||||
|
setUsername: (value: string) => void;
|
||||||
|
loggedIn: () => boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
// It is unclear to me if this is the idiomatic way to do this in Solid
|
||||||
|
export const ModalContext = createContext<ModalContextType>();
|
||||||
|
export const LoginContext = createContext<LoginContextType>();
|
||||||
|
|
||||||
|
export function GlobalStateProvider(props: {
|
||||||
|
children: JSXElement;
|
||||||
|
}): JSXElement {
|
||||||
|
// All of these are passed into context providers
|
||||||
|
const [loginModalOpen, setLoginModalOpen] = createSignal(false);
|
||||||
|
const [token, setToken] = createSignal("");
|
||||||
|
const [username, setUsername] = createSignal("");
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
// This may not be the best place to do this.
|
||||||
|
localStorage.getItem("token") && setToken(localStorage.getItem("token")!);
|
||||||
|
localStorage.getItem("username") &&
|
||||||
|
setUsername(localStorage.getItem("username")!);
|
||||||
|
});
|
||||||
|
|
||||||
|
function loggedIn(): boolean {
|
||||||
|
return token() != "" && username() != "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<ModalContext.Provider value={{ loginModalOpen, setLoginModalOpen }}>
|
||||||
|
<LoginContext.Provider
|
||||||
|
value={{ token, setToken, username, setUsername, loggedIn }}
|
||||||
|
>
|
||||||
|
{props.children}
|
||||||
|
</LoginContext.Provider>
|
||||||
|
</ModalContext.Provider>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
|
@ -1,8 +1,8 @@
|
||||||
import { JSXElement, Show, onCleanup, useContext } from "solid-js";
|
import { JSXElement, Show, onCleanup, useContext } from "solid-js";
|
||||||
|
|
||||||
|
import { ModalContext } from "./GlobalState";
|
||||||
import { LoginForm } from "./RegLogin/Login";
|
import { LoginForm } from "./RegLogin/Login";
|
||||||
import { RegisterForm } from "./RegLogin/Register";
|
import { RegisterForm } from "./RegLogin/Register";
|
||||||
import { ModalContext } from "./Root";
|
|
||||||
|
|
||||||
export function LoginModal(): JSXElement {
|
export function LoginModal(): JSXElement {
|
||||||
const modal_ctx = useContext(ModalContext)!;
|
const modal_ctx = useContext(ModalContext)!;
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import { A } from "@solidjs/router";
|
import { A } from "@solidjs/router";
|
||||||
import { JSXElement, Show, useContext } from "solid-js";
|
import { JSXElement, Show, useContext } from "solid-js";
|
||||||
|
|
||||||
|
import { LoginContext, ModalContext } from "./GlobalState";
|
||||||
import { Flake, Home, Plus, UserCircle } from "./Icons";
|
import { Flake, Home, Plus, UserCircle } from "./Icons";
|
||||||
import { LoginContext, ModalContext } from "./Root";
|
|
||||||
|
|
||||||
// Represents a single list item in the menu bar
|
// Represents a single list item in the menu bar
|
||||||
function MenuItem(props: { href: string; children: JSXElement }): JSXElement {
|
function MenuItem(props: { href: string; children: JSXElement }): JSXElement {
|
||||||
|
|
|
@ -1,13 +1,15 @@
|
||||||
import { useNavigate } from "@solidjs/router";
|
import { useNavigate } from "@solidjs/router";
|
||||||
import { JSXElement, Show, createSignal, onMount, useContext } from "solid-js";
|
import { JSXElement, Show, createSignal, onMount, useContext } from "solid-js";
|
||||||
|
|
||||||
import { LoginContext } from "./Root";
|
import { LoginContext } from "./GlobalState";
|
||||||
import { NewPost, createPost } from "./api";
|
import { NewPost, createPost } from "./api";
|
||||||
|
|
||||||
export function NewPostInputArea(): JSXElement {
|
export function NewPostInputArea(): JSXElement {
|
||||||
const [content, setContent] = createSignal("");
|
const [content, setContent] = createSignal("");
|
||||||
const [waiting, setWaiting] = createSignal(false);
|
const [waiting, setWaiting] = createSignal(false);
|
||||||
const login_ctx = useContext(LoginContext);
|
|
||||||
|
// We assumte this context is always available
|
||||||
|
const login_ctx = useContext(LoginContext)!;
|
||||||
|
|
||||||
const nav = useNavigate();
|
const nav = useNavigate();
|
||||||
|
|
||||||
|
@ -16,7 +18,7 @@ export function NewPostInputArea(): JSXElement {
|
||||||
|
|
||||||
const response = createPost({
|
const response = createPost({
|
||||||
content: content(),
|
content: content(),
|
||||||
token: login_ctx?.token(),
|
token: login_ctx.token(),
|
||||||
} as NewPost);
|
} as NewPost);
|
||||||
|
|
||||||
if (response) {
|
if (response) {
|
||||||
|
@ -28,8 +30,9 @@ export function NewPostInputArea(): JSXElement {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Bail out if not logged in
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
if (login_ctx?.token() == "") nav("/");
|
if (!login_ctx.loggedIn()) nav("/");
|
||||||
});
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { JSXElement, createSignal, useContext } from "solid-js";
|
import { JSXElement, createSignal, useContext } from "solid-js";
|
||||||
|
|
||||||
import { LoginContext, ModalContext } from "../Root";
|
import { LoginContext, ModalContext } from "../GlobalState";
|
||||||
|
|
||||||
export function LoginForm(): JSXElement {
|
export function LoginForm(): JSXElement {
|
||||||
const modal_ctx = useContext(ModalContext);
|
const modal_ctx = useContext(ModalContext);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { JSXElement, createSignal, useContext } from "solid-js";
|
import { JSXElement, createSignal, useContext } from "solid-js";
|
||||||
|
|
||||||
import { LoginContext, ModalContext } from "../Root";
|
import { LoginContext, ModalContext } from "../GlobalState";
|
||||||
|
|
||||||
export function RegisterForm(): JSXElement {
|
export function RegisterForm(): JSXElement {
|
||||||
const modal_ctx = useContext(ModalContext);
|
const modal_ctx = useContext(ModalContext);
|
||||||
|
|
|
@ -1,55 +1,23 @@
|
||||||
import { Accessor, JSXElement, createContext, createSignal } from "solid-js";
|
import { JSXElement } from "solid-js";
|
||||||
|
|
||||||
|
import { GlobalStateProvider } from "./GlobalState";
|
||||||
import { LoginModal } from "./LoginModal";
|
import { LoginModal } from "./LoginModal";
|
||||||
import { Navbar } from "./Navbar";
|
import { Navbar } from "./Navbar";
|
||||||
import { Primary } from "./Primary";
|
import { Primary } from "./Primary";
|
||||||
|
|
||||||
// Representing the state of varoious modals.
|
|
||||||
// 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<boolean>;
|
|
||||||
setLoginModalOpen: (value: boolean) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface LoginContextType {
|
|
||||||
token: Accessor<string>;
|
|
||||||
setToken: (value: string) => void;
|
|
||||||
username: Accessor<string>;
|
|
||||||
setUsername: (value: string) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
// It is unclear to me if this is the idiomatic way to do this in Solid
|
|
||||||
export const ModalContext = createContext<ModalContextType>();
|
|
||||||
export const LoginContext = createContext<LoginContextType>();
|
|
||||||
|
|
||||||
function Root(): JSXElement {
|
function Root(): JSXElement {
|
||||||
// All of these are passed into context providers
|
|
||||||
const [loginModalOpen, setLoginModalOpen] = createSignal(false);
|
|
||||||
const [token, setToken] = createSignal("");
|
|
||||||
const [username, setUsername] = createSignal("");
|
|
||||||
|
|
||||||
// This may not be the best place to do this.
|
|
||||||
localStorage.getItem("token") && setToken(localStorage.getItem("token")!);
|
|
||||||
localStorage.getItem("username") &&
|
|
||||||
setUsername(localStorage.getItem("username")!);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<ModalContext.Provider value={{ loginModalOpen, setLoginModalOpen }}>
|
<GlobalStateProvider>
|
||||||
<LoginContext.Provider
|
<FancyBackground />
|
||||||
value={{ token, setToken, username, setUsername }}
|
<div class="flex flex-col items-center">
|
||||||
>
|
<Navbar />
|
||||||
<FancyBackground />
|
<LoginModal />
|
||||||
<div class="flex flex-col items-center">
|
<div class="flex w-full flex-col items-center space-y-2 px-2 md:max-w-3xl">
|
||||||
<Navbar />
|
<Primary />
|
||||||
<LoginModal />
|
|
||||||
<div class="flex w-full flex-col items-center space-y-2 px-2 md:max-w-3xl">
|
|
||||||
<Primary />
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</LoginContext.Provider>
|
</div>
|
||||||
</ModalContext.Provider>
|
</GlobalStateProvider>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue