Fakultas Ilmu Komputer UI

Skip to content
Snippets Groups Projects
Commit 7ecd7b7f authored by Muhammad Zaki's avatar Muhammad Zaki
Browse files

feat: added comment component

parent 8a366673
No related branches found
No related tags found
No related merge requests found
type rec t = {
id: int,
level: int,
user: string,
time: int,
time_ago: string,
content: string,
comments: array<t>,
}
let pluralize = n => `${n->Js.Int.toString} ${n === 1 ? "reply" : "replies"}`
@@warning("-39")
@react.component
let rec make = (~comment) => {
let (open_, setOpen) = React.useState(_ => true)
<li className="comment">
<div className="by">
<Link href={`/users/${comment.user}`}> {comment.user->React.string} </Link>
{" "->React.string}
{comment.time_ago->React.string}
</div>
<div className="text" dangerouslySetInnerHTML={{"__html": comment.content}} />
{comment.comments->Belt.Array.length > 0
? <>
<div className={"toggle" ++ (open_ ? " open" : "")}>
<a onClick={_evt => setOpen(prevOpen => !prevOpen)}>
{(
open_ ? "[-]" : `[+] ${pluralize(comment.comments->Belt.Array.length)} collapsed`
)->React.string}
</a>
</div>
{open_
? <ul className="comment-children">
{comment.comments
->Belt.Array.map(comment =>
React.createElement(
make,
makeProps(~comment, ~key={comment.id->Js.Int.toString}, ()),
)
)
->React.array}
</ul>
: React.null}
</>
: React.null}
</li>
}
@@warning("-39")
type rec t = {
id: int,
level: int,
user: string,
time: int,
time_ago: string,
content: string,
comments: array<t>,
}
@react.component
let make: (~comment: t) => React.element
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment