List.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use client';
  2. import '../style.scss';
  3. import React from 'react';
  4. import Loading from '@/app/component/Loading';
  5. import { BoardResponse } from '@/types/response/forum/board';
  6. import { PostResponse } from '@/types/response/forum/post';
  7. import { CommentListResponse } from '@/types/response/forum/comment';
  8. import { type CommentItem } from '@/types/forum/comment';
  9. import WriteForm from './WriteForm';
  10. import Item from './Item';
  11. type Props = {
  12. _board: BoardResponse,
  13. _post: PostResponse,
  14. _data: CommentListResponse;
  15. loading: boolean;
  16. replyTargetID: number|null;
  17. onReply: (commentID: number) => void;
  18. onDelete: (commentID: number, parentID?: number) => void;
  19. onSuccess: (comment?: CommentItem) => void;
  20. canWriteReply?: boolean;
  21. }
  22. export default function List({ _board, _post, _data, loading, replyTargetID, onReply, onSuccess, onDelete, canWriteReply } : Props)
  23. {
  24. return (
  25. <>
  26. {loading ? (
  27. <Loading type={2} />
  28. ) : (
  29. <>
  30. {_data.total > 0 && (
  31. <article className="comment-list">
  32. <ol>
  33. {_data.list.map((row) => {
  34. return (
  35. <React.Fragment key={row.id}>
  36. <Item
  37. _board={_board}
  38. _post={_post}
  39. _comment={row}
  40. isReplying={replyTargetID === row.id}
  41. onReply={() => onReply(row.id)}
  42. onDelete={onDelete}
  43. onSuccess={onSuccess}
  44. canWriteReply={canWriteReply}
  45. >
  46. {/* row에 대한 답글 폼(토글) */}
  47. {canWriteReply !== false && replyTargetID === row.id && (
  48. <WriteForm
  49. _board={_board}
  50. _post={_post}
  51. _comment={row}
  52. onSuccess={onSuccess}
  53. onCancel={() => onReply(row.id)}
  54. />
  55. )}
  56. </Item>
  57. {/* 대댓글 목록 */}
  58. {Array.isArray(row.children) && row.children.length > 0 && (
  59. <li>
  60. <ol className="pl-[72px]">
  61. {row.children.map((ch) => (
  62. <Item
  63. key={ch.id}
  64. _board={_board}
  65. _post={_post}
  66. _comment={ch}
  67. onReply={() => onReply(ch.id)}
  68. onDelete={onDelete}
  69. onSuccess={onSuccess}
  70. canWriteReply={canWriteReply}
  71. >
  72. {canWriteReply !== false && replyTargetID != null && replyTargetID === ch.id && (
  73. <WriteForm
  74. _board={_board}
  75. _post={_post}
  76. _comment={ch}
  77. onSuccess={onSuccess}
  78. onCancel={() => onReply(ch.id)}
  79. />
  80. )}
  81. </Item>
  82. ))}
  83. </ol>
  84. </li>
  85. )}
  86. </React.Fragment>
  87. )
  88. })}
  89. </ol>
  90. </article>
  91. )}
  92. </>
  93. )}
  94. </>
  95. );
  96. }