2023-11-13 11:50:24 +01:00
|
|
|
import { useNavigate } from "@solidjs/router";
|
2023-11-15 07:29:59 +01:00
|
|
|
import { JSXElement, Show, createSignal, onMount, useContext } from "solid-js";
|
|
|
|
|
2023-11-22 15:29:27 +01:00
|
|
|
import { LoginContext } from "../Context/GlobalState";
|
|
|
|
import { NewPost, createPost } from "../Util/api";
|
2023-11-13 11:50:24 +01:00
|
|
|
|
2023-11-13 12:00:46 +01:00
|
|
|
export function NewPostInputArea(): JSXElement {
|
2023-11-13 11:50:24 +01:00
|
|
|
const [content, setContent] = createSignal("");
|
|
|
|
const [waiting, setWaiting] = createSignal(false);
|
2023-11-15 10:33:57 +01:00
|
|
|
|
2024-03-08 10:04:35 +01:00
|
|
|
// We assume this context is always available
|
2023-11-15 10:33:57 +01:00
|
|
|
const login_ctx = useContext(LoginContext)!;
|
2023-11-13 11:50:24 +01:00
|
|
|
|
|
|
|
const nav = useNavigate();
|
|
|
|
|
2023-11-13 12:00:46 +01:00
|
|
|
const sendPost = (): void => {
|
2023-11-13 11:50:24 +01:00
|
|
|
setWaiting(true);
|
|
|
|
|
|
|
|
const response = createPost({
|
|
|
|
content: content(),
|
2023-11-15 10:33:57 +01:00
|
|
|
token: login_ctx.token(),
|
2023-11-13 11:50:24 +01:00
|
|
|
} as NewPost);
|
|
|
|
|
|
|
|
if (response) {
|
|
|
|
response.then(() => {
|
|
|
|
setWaiting(false);
|
|
|
|
setContent("");
|
|
|
|
nav("/");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-11-15 10:33:57 +01:00
|
|
|
// Bail out if not logged in
|
2023-11-14 13:34:14 +01:00
|
|
|
onMount(() => {
|
2023-11-15 10:33:57 +01:00
|
|
|
if (!login_ctx.loggedIn()) nav("/");
|
2023-11-14 13:34:14 +01:00
|
|
|
});
|
|
|
|
|
2023-11-13 11:50:24 +01:00
|
|
|
return (
|
|
|
|
<Show
|
|
|
|
when={!waiting()}
|
2023-11-15 07:29:59 +01:00
|
|
|
fallback={<span class="loading loading-spinner loading-lg self-center" />}
|
2023-11-13 11:50:24 +01:00
|
|
|
>
|
2023-11-15 07:29:59 +01:00
|
|
|
<div class="flex w-full flex-col space-y-2">
|
2023-11-13 11:50:24 +01:00
|
|
|
<textarea
|
|
|
|
class="textarea textarea-bordered h-32"
|
|
|
|
placeholder="Speak your mind..."
|
|
|
|
maxLength={500}
|
2023-11-15 07:29:59 +01:00
|
|
|
onInput={(input): void => {
|
2023-11-13 11:50:24 +01:00
|
|
|
setContent(input.target.value);
|
|
|
|
}}
|
2023-11-15 07:29:59 +01:00
|
|
|
/>
|
2023-11-13 11:50:24 +01:00
|
|
|
<button
|
2023-11-13 12:00:46 +01:00
|
|
|
class={
|
2023-11-15 07:29:59 +01:00
|
|
|
"btn btn-primary btn-sm self-end" +
|
2023-11-13 12:00:46 +01:00
|
|
|
(content() == "" ? " btn-disabled" : "")
|
|
|
|
}
|
2023-11-15 07:29:59 +01:00
|
|
|
onClick={sendPost}
|
2023-11-13 11:50:24 +01:00
|
|
|
>
|
|
|
|
Submit
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</Show>
|
|
|
|
);
|
|
|
|
}
|