module Yesod.Comments.View
( showComments
, showComment
) where
import Yesod
import Yesod.Comments.Core
import Yesod.Comments.Utils
import Yesod.Markdown
import Data.Time.Format.Human
import Data.Monoid (mempty)
showComments :: YesodComments m => [Comment] -> WidgetT m IO ()
showComments comments = [whamlet|
<div .list>
$if not $ null comments
<h4>#{helper $ length comments}:
$forall comment <- comments
^{showComment comment}
|]
where
helper :: Int -> String
helper 0 = "no comments"
helper 1 = "1 comment"
helper n = show n ++ " comments"
showComment :: YesodComments m => Comment -> WidgetT m IO ()
showComment comment = do
mine <- handlerToWidget $ isCommentingUser comment
commentTimestamp <- handlerToWidget . liftIO . humanReadableTime $ cTimeStamp comment
UserDetails _ name email <- handlerToWidget $ commentUserDetails comment
let anchor = "comment_" ++ show (commentId comment)
[whamlet|
<div .comment :mine:.mine:>
<div .attribution>
<p>
<span .avatar>
<img src="#{gravatar 20 email}">
<a href="##{anchor}" id="#{anchor}">#{commentTimestamp}
, #{name} wrote:
<div .content>
<blockquote>
#{markdownToHtml $ cContent comment}
$if mine
<div .controls>
^{commentControls editRoute deleteRoute (cThreadId comment) (commentId comment)}
|]
commentControls :: Maybe (ThreadId -> CommentId -> Route m)
-> Maybe (ThreadId -> CommentId -> Route m)
-> ThreadId -> CommentId -> WidgetT m IO ()
commentControls e@(Just _) d@(Just _) thread cid = [whamlet|
^{commentControls e Nothing thread cid}
\ |
^{commentControls Nothing d thread cid}
|]
commentControls (Just editR) Nothing thread cid = [whamlet|<a href="@{editR thread cid}">Edit|]
commentControls Nothing (Just deleteR) thread cid = [whamlet|<a href="@{deleteR thread cid}">Delete|]
commentControls _ _ _ _ = mempty