FrostByte/client-solid/src/Components/Posts.tsx

44 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-10-27 16:04:58 +02:00
import { useNavigate } from "@solidjs/router";
2023-11-15 07:29:59 +01:00
import { For, JSXElement, Show, createSignal } from "solid-js";
2023-11-22 15:29:27 +01:00
import { Arrow, loadSpinner } from "../Util/Icons";
import { Post, getPosts } from "../Util/api";
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-15 16:07:04 +01:00
<For each={posts()}>
2023-11-15 07:29:59 +01:00
{(post): JSXElement => <PostSegment post={post} />}
2023-11-15 16:07:04 +01:00
</For>
</Show>
2023-10-21 08:51:33 +02:00
);
}
// This is the card container for a post
2023-11-15 07:29:59 +01:00
export function PostSegment(props: { 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-15 07:29:59 +01:00
<div class="card compact w-full flex-grow border-b-2 border-b-base-300 bg-base-200 text-base-content transition-all hover:bg-base-300">
2023-10-21 08:51:33 +02:00
<div class="card-body">
2023-12-09 11:53:10 +01:00
<p class="break-words text-base-content md:px-6 md:pt-2">{props.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
2023-11-15 07:29:59 +01:00
onClick={(): void => nav("/post/" + props.post?.id)}
2023-11-13 12:00:46 +01:00
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>
);
}