PostList.tsx 1.6 KB

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