import { Accessor, createSignal, useContext } from "solid-js"; import { createContext } from "solid-js"; import { Route, Routes, A } from "@solidjs/router"; import { createPost, getPosts } from "./api"; import { Post, NewPost } from "./api"; // 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; setLoginModalOpen: (value: boolean) => void; } interface LoginContextType { token: Accessor; setToken: (value: string) => void; username: Accessor; setUsername: (value: string) => void; } // It is unclear to me if this is the idiomatic way to do this in Solid export const ModalContext = createContext(); export const LoginContext = createContext(); function Root() { // 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 ( <>
); } function Navbar() { let modal_ctx = useContext(ModalContext); let login_ctx = useContext(LoginContext); return ( ); } function Menu() { return ( ); } function NewPostInputArea() { const [content, setContent] = createSignal(""); const login_ctx = useContext(LoginContext); return (
); } function Posts() { const [posts, setPosts] = createSignal([] as Post[]); const [loading, setLoading] = createSignal(true); getPosts().then((posts) => { setPosts(posts as any); setLoading(false); }); return (
{loading() ? ( ) : ( <> )} {posts().map((post) => { if (post.content == "") return; // Filtering out empty posts, remove this later return ; })}
); } function PostSegment({ post }: { post: Post }) { return (

{post.content}

); } function Primary() { return ( } /> } /> ); } // This is a modal function Login() { const modal_ctx = useContext(ModalContext); return ( ); } // This function is responsible for sending the login request to the server // and storing the token in localstorage async function submitLogin( username: string, password: string ): Promise { const response = await fetch("/api/login", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ username, password }), }); if (response.ok) { const data = await response.json(); if (data.token && data.username) { localStorage.setItem("token", data.token); localStorage.setItem("username", data.username); return data.token; } } return ""; } function LoginForm() { const modal_ctx = useContext(ModalContext); const [username, setUsername] = createSignal(""); const [password, setPassword] = createSignal(""); const [waiting, setWaiting] = createSignal(false); const [error, setError] = createSignal(false); async function loginFailed() { setError(true); setWaiting(false); setTimeout(() => { setError(false); }, 1000); } return (
{ setUsername(e.target.value); }} /> { setPassword(e.target.value); }} />
); } export default Root;