2023-10-27 16:04:58 +02:00
|
|
|
import { Accessor, Show, createSignal, useContext } from "solid-js";
|
2023-10-19 02:42:37 +02:00
|
|
|
import { createContext } from "solid-js";
|
|
|
|
|
2023-10-21 08:51:33 +02:00
|
|
|
import { createPost } from "./api";
|
|
|
|
import { NewPost } from "./api";
|
|
|
|
import { Navbar } from "./Navbar";
|
|
|
|
import { Primary } from "./Primary";
|
|
|
|
import { Login } from "./Navbar";
|
2023-10-27 16:04:58 +02:00
|
|
|
import { useNavigate } from "@solidjs/router";
|
2023-10-19 02:42:37 +02:00
|
|
|
|
2023-10-21 07:54:47 +02:00
|
|
|
// 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>();
|
2023-10-19 02:42:37 +02:00
|
|
|
|
|
|
|
function Root() {
|
2023-10-21 07:54:47 +02:00
|
|
|
// 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")!);
|
|
|
|
|
2023-10-19 02:42:37 +02:00
|
|
|
return (
|
|
|
|
<>
|
2023-10-21 07:54:47 +02:00
|
|
|
<ModalContext.Provider value={{ loginModalOpen, setLoginModalOpen }}>
|
|
|
|
<LoginContext.Provider
|
|
|
|
value={{ token, setToken, username, setUsername }}
|
|
|
|
>
|
|
|
|
<div class="flex flex-col items-center my-2">
|
|
|
|
<Navbar />
|
|
|
|
<Login />
|
|
|
|
<div class="flex flex-col items-center md:w-96 space-y-2">
|
|
|
|
<Primary />
|
|
|
|
</div>
|
2023-10-19 08:11:52 +02:00
|
|
|
</div>
|
2023-10-21 07:54:47 +02:00
|
|
|
</LoginContext.Provider>
|
|
|
|
</ModalContext.Provider>
|
2023-10-19 02:42:37 +02:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-21 08:51:33 +02:00
|
|
|
export function NewPostInputArea() {
|
2023-10-19 02:42:37 +02:00
|
|
|
const [content, setContent] = createSignal("");
|
2023-10-27 16:04:58 +02:00
|
|
|
const [waiting, setWaiting] = createSignal(false);
|
2023-10-21 07:54:47 +02:00
|
|
|
const login_ctx = useContext(LoginContext);
|
|
|
|
|
2023-10-27 16:04:58 +02:00
|
|
|
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("/");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-10-19 02:42:37 +02:00
|
|
|
return (
|
2023-10-27 16:04:58 +02:00
|
|
|
<Show
|
|
|
|
when={!waiting()}
|
|
|
|
fallback={
|
|
|
|
<span class="loading loading-spinner loading-lg self-center"></span>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<div class="flex flex-col space-y-2">
|
|
|
|
<textarea
|
|
|
|
class="textarea textarea-bordered"
|
|
|
|
placeholder="Speak your mind..."
|
|
|
|
maxLength={500}
|
|
|
|
oninput={(input) => {
|
|
|
|
setContent(input.target.value);
|
|
|
|
}}
|
|
|
|
></textarea>
|
|
|
|
<button
|
|
|
|
class={
|
|
|
|
"btn btn-primary self-end btn-sm" +
|
|
|
|
(content() == "" ? " btn-disabled" : "")
|
|
|
|
}
|
|
|
|
onclick={sendPost}
|
|
|
|
>
|
|
|
|
Submit
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</Show>
|
2023-10-19 02:42:37 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Root;
|