Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. import React from 'react'
  2.  
  3. export type ReplyType = {
  4. text: string
  5. votes: string
  6. username: string
  7. date: string
  8. replies?: Array<ReplyType>
  9. id: string
  10. }
  11.  
  12. export type CommentType = {
  13. username: string
  14. date: string
  15. text: string
  16. replies: Array<ReplyType>
  17. id: string
  18. }
  19.  
  20. export interface ReplyProps {
  21. reply: ReplyType
  22. }
  23.  
  24. export interface RenderChildReplyProps extends ReplyProps {
  25. RenderChildReply: React.FunctionComponent<RenderChildReplyProps>
  26. }
  27.  
  28. export interface CommentProps {
  29. comments: Array<CommentType>
  30. }
  31.  
  32. /**
  33. * used by __Reply__ to recursively render comments. It accepts itself
  34. * in order to recursively render children replies
  35. */
  36. const RenderReply = ({ reply, RenderChildReply }: RenderChildReplyProps) => (
  37. <div className='renderReply'>
  38. <p className='bold'>{reply.username}:</p>
  39. <p>{reply.text}</p>
  40. {reply.replies &&
  41. reply.replies.length > 0 &&
  42. reply.replies.map(reply => (
  43. <RenderChildReply reply={reply} RenderChildReply={RenderChildReply} />
  44. ))}
  45. </div>
  46. )
  47.  
  48. /**
  49. * used by __Comment__ to render each reply made to the main comment
  50. * if a reply has its own reply, we'll use `RenderReply` to render
  51. * that reply and pass it to itself to render children replies
  52. */
  53. const Reply = ({ reply }: ReplyProps) => {
  54. return (
  55. <div className='reply'>
  56. <h3>test</h3>
  57. <p className='bold'>{reply.username}:</p>
  58. <p>{reply.text}</p>
  59.  
  60. {reply.replies &&
  61. reply.replies.length > 0 &&
  62. reply.replies.map(reply => (
  63. <RenderReply reply={reply} RenderChildReply={RenderReply} />
  64. ))}
  65. </div>
  66. )
  67. }
  68.  
  69. /**
  70. * main component which renders the entire comment and child replies
  71. */
  72. const Comment = ({ comments }: CommentProps) => {
  73. return (
  74. <div className='comment'>
  75. <h1>Comments:</h1>
  76. {comments.map(comment => {
  77. return (
  78. <React.Fragment>
  79. <h2>Comment:</h2>
  80. <p className='bold'>{comment.username}:</p>
  81. <p>{comment.text}</p>
  82.  
  83. <h2>{comment.replies.length} replies:</h2>
  84. {comment.replies.length > 0 &&
  85. comment.replies.map(reply => (
  86. <Reply key={reply.id} reply={reply} />
  87. ))}
  88. </React.Fragment>
  89. )
  90. })}
  91. </div>
  92. )
  93. }
  94.  
  95. export default Comment
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement