PostList.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 className="w-full min-w-[180px] border border-gray-200 rounded box-border relative p-4 text-base flex flex-col" key={post.id}>
  40. <span className="font-medium mb-2">{post.title || "Untitled"}</span>
  41. <div className="absolute right-4 top-3 text-2xl z-[1]">
  42. <i
  43. className="bi bi-x cursor-pointer"
  44. onClick={() => {
  45. onDelete(post.id, post.title);
  46. }}
  47. />
  48. </div>
  49. <span className="overflow-hidden text-ellipsis whitespace-nowrap mb-2">{post.content || "No content"}</span>
  50. <Button
  51. type="button"
  52. onClick={() => {
  53. onEdit(post.id);
  54. }}
  55. >
  56. Edit
  57. </Button>
  58. </div>
  59. ))}
  60. </div>
  61. );
  62. }