PostList.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /** @jsx h */
  2. import { h } from "preact";
  3. interface PostListProps {
  4. posts: { id: string; title: string; content: string; shared: boolean }[];
  5. }
  6. export default function PostList(props: PostListProps) {
  7. const onEdit = (id: string) => {
  8. location.href = `/${id}`;
  9. };
  10. const onDelete = (id: string, title: string) => {
  11. window.$modal?.show(
  12. "Confirm delete",
  13. `Are you sure you want to delete ${title}?`,
  14. [
  15. {
  16. text: "Confirm",
  17. onClick: async () => {
  18. const resp = await fetch("/api/post", {
  19. method: "DELETE",
  20. headers: { "Content-Type": "application/json" },
  21. body: JSON.stringify({
  22. id,
  23. }),
  24. });
  25. const respJson = await resp.json();
  26. if (respJson.success) {
  27. location.reload();
  28. }
  29. },
  30. },
  31. {
  32. text: "Cancel",
  33. },
  34. ]
  35. );
  36. };
  37. return (
  38. <div className="pd-post-list">
  39. {props.posts.map((post) => (
  40. <div className="pd-post" key={post.id}>
  41. <span className="pd-post-title">{post.title || "Untitled"}</span>
  42. <div className="pd-post-action">
  43. <i
  44. className="bi bi-x"
  45. onClick={() => {
  46. onDelete(post.id, post.title);
  47. }}
  48. />
  49. </div>
  50. <span className="pd-post-digest">{post.content || "No content"}</span>
  51. <button
  52. onClick={() => {
  53. onEdit(post.id);
  54. }}
  55. >
  56. Edit
  57. </button>
  58. </div>
  59. ))}
  60. </div>
  61. );
  62. }