import { Accessor, Show, createSignal, useContext } from "solid-js"; import { createContext } from "solid-js"; import { createPost } from "./api"; import { NewPost } from "./api"; import { Navbar } from "./Navbar"; import { Primary } from "./Primary"; import { Login } from "./Navbar"; import { useNavigate } from "@solidjs/router"; // 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 ( <>
); } export function NewPostInputArea() { const [content, setContent] = createSignal(""); const [waiting, setWaiting] = createSignal(false); const login_ctx = useContext(LoginContext); const nav = useNavigate(); const sendPost = () => { setWaiting(true); const response = createPost({ content: content(), token: login_ctx?.token(), } as NewPost); if (response) { response.then(() => { setWaiting(false); setContent(""); nav("/"); }); } }; return ( } >
); } export default Root;