Polishing solid implementation

This commit is contained in:
Imbus 2023-10-19 08:11:52 +02:00
parent 5e2d0f3e3b
commit 2a7dffc3be
2 changed files with 82 additions and 33 deletions

View file

@ -1,7 +1,9 @@
import { createSignal } from "solid-js"; import { createSignal } from "solid-js";
import { createContext } from "solid-js"; import { createContext } from "solid-js";
import { createPost, getPosts, getPost } from "./api"; import { Route, Routes, A } from "@solidjs/router";
import { createPost, getPosts } from "./api";
import { Post, NewPost } from "./api"; import { Post, NewPost } from "./api";
export const TestContext = createContext("Test123"); export const TestContext = createContext("Test123");
@ -10,23 +12,51 @@ function Root() {
return ( return (
<> <>
<TestContext.Provider value="Test123"> <TestContext.Provider value="Test123">
<Primary /> <div class="flex flex-col items-center my-2">
<Navbar />
<div class="flex flex-col items-center md:w-96 space-y-2">
<Primary />
</div>
</div>
</TestContext.Provider> </TestContext.Provider>
</> </>
); );
} }
function Navbar() {
return (
<div class="navbar bg-base-100 max-w-3xl max-w flex justify-evenly">
<a class="btn btn-ghost normal-case text-xl">hello</a>
<Menu />
<A href="/login" class="btn btn-ghost normal-case text-sm">
Login
</A>
</div>
);
}
function Menu() { function Menu() {
return ( return (
<ul class="menu menu-horizontal bg-base-200 rounded-box space-x-1"> <ul class="menu menu-horizontal bg-base-200 rounded-box space-x-2 justify-end">
<li> <li>
<a>Home</a> <A href="/" end>
Home
</A>
</li> </li>
<li> <li>
<a>Boards</a> <A href="/new" end>
New
</A>
</li> </li>
<li> <li>
<a>New</a> <A href="/boards" end>
Boards
</A>
</li>
<li>
<A href="/login" end>
Login
</A>
</li> </li>
</ul> </ul>
); );
@ -35,59 +65,78 @@ function Menu() {
function NewPostInputArea() { function NewPostInputArea() {
const [content, setContent] = createSignal(""); const [content, setContent] = createSignal("");
return ( return (
<div class="flex flex-col my-4"> <div class="flex flex-col space-y-2">
<textarea <textarea
class="textarea textarea-bordered" class="textarea textarea-bordered"
placeholder="Bio" placeholder="Speak your mind..."
oninput={(input) => { oninput={(input) => {
setContent(input.target.value); setContent(input.target.value);
}} }}
></textarea> ></textarea>
<button class="btn btn-primary self-end btn-sm mt-4" onclick={() => { <button
createPost({ content: content(), token: "" } as NewPost); class={
}}>Submit</button> "btn btn-primary self-end btn-sm" +
(content() == "" ? " btn-disabled" : "")
}
onclick={() => {
if (content() == "") return;
createPost({ content: content(), token: "" } as NewPost);
}}
>
Submit
</button>
</div> </div>
); );
} }
function Posts() { function Posts() {
const [posts, setPosts] = createSignal([]); const [posts, setPosts] = createSignal([] as Post[]);
const [loading, setLoading] = createSignal(true);
getPosts().then((posts) => { getPosts().then((posts) => {
setPosts(posts as any); setPosts(posts as any);
setLoading(false)
}); });
return ( return (
<div class="flex flex-col my-4 space-y-2"> <div class="flex flex-col space-y-2 w-full md:w-96">
{ loading() ? <span class="loading loading-spinner loading-lg self-center"></span> : <></> }
{posts().map((post) => { {posts().map((post) => {
return ( if (post.content == "") return; // Filtering out empty posts, remove this later
<PostSegment post={post}></PostSegment> return <PostSegment post={post}></PostSegment>;
);
})} })}
</div> </div>
); );
} }
function PostSegment({ post }: { post: Post}) { function PostSegment({ post }: { post: Post }) {
return ( return (
<div class="card bg-base-200 shadow-lg compact text-base-content"> <div class="card bg-base-200 shadow-lg compact text-base-content w-full">
<div class="card-body"> <div class="card-body">
<h2 class="card-title">{post.content}</h2> <p class="text-base-content">{post.content}</p>
<p class="text-base-content">{post.uuid}</p> {/* <p>{post.votes.up}</p>
<p>{post.votes.up}</p> <p>{post.votes.down}</p> */}
<p>{post.votes.down}</p>
</div> </div>
</div> </div>
); );
} }
function Primary() { function Primary() {
return ( return (
<div class="flex flex-col items-center my-6"> <Routes>
<Menu></Menu> <Route path="/" element={<Posts />} />
<NewPostInputArea></NewPostInputArea> <Route path="/new" element={<NewPostInputArea />} />
<Posts></Posts> <Route path="/boards" element={<div>Boards</div>} />
<Route path="/login" element={<Login />} />
</Routes>
);
}
function Login() {
return (
<div>
Login
<input class="input input-bordered" type="text" />
</div> </div>
); );
} }

View file

@ -1,6 +1,6 @@
const PORT = 3000; // const PORT = 3000;
const API_URL = `http://localhost:${PORT}/api/`; // const API_URL = `http://localhost:${PORT}/api/`;
const API_URL2 = new URL(API_URL); // const API_URL2 = new URL(API_URL);
export interface NewPost { export interface NewPost {
content: string; content: string;
@ -20,20 +20,20 @@ export interface Post extends NewPost {
export async function getPosts(): Promise<Post[]> { export async function getPosts(): Promise<Post[]> {
// const res = await fetch(`${API_URL}/posts`); // const res = await fetch(`${API_URL}/posts`);
const res = await fetch(API_URL2.href); const res = await fetch("/api/");
const data = await res.json(); const data = await res.json();
return data; return data;
} }
export async function getPost(id: string): Promise<Post> { export async function getPost(id: string): Promise<Post> {
const res = await fetch(`${API_URL}/posts/${id}`); const res = await fetch(`/api/${id}`);
const data = await res.json(); const data = await res.json();
return data; return data;
} }
export async function createPost(post: NewPost): Promise<void> { export async function createPost(post: NewPost): Promise<void> {
// await fetch(`${API_URL}`, { // await fetch(`${API_URL}`, {
await fetch(API_URL2.href, { await fetch("/api/", {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",