FrostByte/client-solid/src/Posts.tsx

45 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-11-14 13:34:14 +01:00
import { JSXElement, Show, createSignal, For } from "solid-js";
2023-10-21 08:51:33 +02:00
import { getPosts } from "./api";
import { Post } from "./api";
2023-10-27 16:04:58 +02:00
import { useNavigate } from "@solidjs/router";
2023-11-13 09:34:32 +01:00
import { Arrow } from "./Icons";
import { loadSpinner } from "./Icons";
2023-10-21 08:51:33 +02:00
2023-11-13 12:00:46 +01:00
export function Posts(): JSXElement {
2023-10-21 08:51:33 +02:00
const [posts, setPosts] = createSignal([] as Post[]);
const [loading, setLoading] = createSignal(true);
getPosts().then((posts) => {
2023-11-13 11:50:24 +01:00
setPosts(posts);
2023-10-21 08:51:33 +02:00
setLoading(false);
});
return (
<Show when={!loading()} fallback={loadSpinner()}>
2023-11-14 13:34:14 +01:00
<For each={posts()}>
{(post): JSXElement => <PostSegment post={post}></PostSegment>}
</For>
</Show>
2023-10-21 08:51:33 +02:00
);
}
// This is the card container for a post
2023-11-13 12:00:46 +01:00
export function PostSegment({ post }: { post: Post }): JSXElement {
2023-10-27 16:04:58 +02:00
const nav = useNavigate();
2023-10-21 08:51:33 +02:00
return (
2023-11-14 13:34:14 +01:00
<div class="card border-b-2 flex-grow border-b-base-300 bg-base-200 hover:bg-base-300 transition-all compact text-base-content w-full">
2023-10-21 08:51:33 +02:00
<div class="card-body">
2023-10-27 16:04:58 +02:00
<p class="text-base-content break-words">{post?.content}</p>
2023-11-13 09:34:32 +01:00
<div class="card-actions justify-end">
2023-11-13 12:00:46 +01:00
<button
onClick={(): void => nav("/post/" + post?.id)}
class="btn btn-xs"
>
2023-11-13 09:34:32 +01:00
<Arrow />
</button>
</div>
2023-10-21 08:51:33 +02:00
</div>
</div>
);
}