PostList.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import Button from "../components/form/Button.tsx";
  2. interface PostListProps {
  3. posts: { id: string; title: string; content: string; shared: boolean }[];
  4. }
  5. export default function PostList(props: PostListProps) {
  6. const onEdit = (id: string) => {
  7. location.href = `/${id}`;
  8. };
  9. const onDelete = (id: string, title: string) => {
  10. globalThis.$modal?.show(
  11. "Confirm delete",
  12. `Are you sure you want to delete ${title}?`,
  13. [
  14. {
  15. text: "Confirm",
  16. onClick: async () => {
  17. const resp = await fetch("/api/post", {
  18. method: "DELETE",
  19. headers: { "Content-Type": "application/json" },
  20. body: JSON.stringify({
  21. id,
  22. }),
  23. });
  24. const respJson = await resp.json();
  25. if (respJson.success) {
  26. location.reload();
  27. }
  28. },
  29. },
  30. {
  31. text: "Cancel",
  32. },
  33. ],
  34. );
  35. };
  36. return (
  37. <div className="w-full grid grid-cols-4 gap-4 mt-4 pb-4 overflow-auto">
  38. {props.posts.map((post) => (
  39. <div
  40. className="w-full min-w-[180px] border border-gray-200 dark:border-gray-700 dark:bg-gray-800 rounded box-border relative p-4 text-base flex flex-col"
  41. key={post.id}
  42. >
  43. <span className="font-medium mb-2">{post.title || "Untitled"}</span>
  44. <div className="absolute right-4 top-3 text-2xl z-[1]">
  45. <i
  46. className="bi bi-x cursor-pointer"
  47. onClick={() => {
  48. onDelete(post.id, post.title);
  49. }}
  50. />
  51. </div>
  52. <span className="overflow-hidden text-ellipsis whitespace-nowrap mb-2">
  53. {post.content || "No content"}
  54. </span>
  55. <Button
  56. type="button"
  57. onClick={() => {
  58. onEdit(post.id);
  59. }}
  60. >
  61. Edit
  62. </Button>
  63. </div>
  64. ))}
  65. </div>
  66. );
  67. }