2016-12-14 19:54:17 +00:00
|
|
|
module App.BlogEntry where
|
|
|
|
|
2016-12-14 22:53:00 +00:00
|
|
|
import App.Utils (mdify)
|
2016-12-14 19:54:17 +00:00
|
|
|
import Control.Monad.Aff (attempt)
|
|
|
|
import DOM (DOM)
|
|
|
|
import Data.Argonaut (class DecodeJson, decodeJson, (.?))
|
|
|
|
import Data.Either (Either(..), either)
|
|
|
|
import Data.Maybe (Maybe(..))
|
|
|
|
import Network.HTTP.Affjax (AJAX, get)
|
|
|
|
import Prelude (bind, pure, show, ($), (<>), (<<<))
|
|
|
|
import Pux (EffModel, noEffects)
|
2016-12-17 14:15:29 +00:00
|
|
|
import Pux.DocumentTitle (documentTitle)
|
2016-12-18 05:14:25 +00:00
|
|
|
import Pux.Html (Html, div, h1, p, text)
|
|
|
|
import Pux.Html.Attributes (dangerouslySetInnerHTML, className, id_, title)
|
2016-12-14 19:54:17 +00:00
|
|
|
|
|
|
|
data Action = RequestPost
|
|
|
|
| ReceivePost (Either String Post)
|
|
|
|
| RenderPost
|
|
|
|
|
|
|
|
type State =
|
|
|
|
{ status :: String
|
|
|
|
, hack :: String
|
|
|
|
, id :: Maybe Int
|
|
|
|
, post :: Post
|
|
|
|
, name :: String }
|
|
|
|
|
|
|
|
data Post = Post
|
|
|
|
{ title :: String
|
|
|
|
, body :: String
|
|
|
|
, date :: String }
|
|
|
|
|
|
|
|
instance decodeJsonPost :: DecodeJson Post where
|
|
|
|
decodeJson json = do
|
|
|
|
obj <- decodeJson json
|
|
|
|
title <- obj .? "title"
|
|
|
|
body <- obj .? "body"
|
|
|
|
date <- obj .? "date"
|
|
|
|
pure $ Post { title: title, body: body, date: date }
|
|
|
|
|
|
|
|
init :: State
|
|
|
|
init =
|
|
|
|
{ status: "Loading..."
|
|
|
|
, hack: ""
|
|
|
|
, post: Post
|
|
|
|
{ title: ""
|
|
|
|
, body: ""
|
|
|
|
, date: "" }
|
|
|
|
, name: ""
|
|
|
|
, id: Nothing }
|
|
|
|
|
|
|
|
update :: Action -> State -> EffModel State Action (ajax :: AJAX, dom :: DOM)
|
|
|
|
update (ReceivePost (Left err)) state =
|
|
|
|
noEffects $ state { id = Nothing, status = err }
|
|
|
|
update (ReceivePost (Right post)) state =
|
|
|
|
{ state: state { status = "", id = Just 1, post = post }
|
|
|
|
, effects: [ pure $ RenderPost ]
|
|
|
|
}
|
|
|
|
update RequestPost state =
|
|
|
|
{ state: state
|
|
|
|
, effects: [ do
|
|
|
|
res <- attempt $ get ("/api/blog/post?name=" <> state.name)
|
|
|
|
let decode r = decodeJson r.response :: Either String Post
|
|
|
|
let post = either (Left <<< show) decode res
|
|
|
|
pure $ ReceivePost post
|
|
|
|
]
|
|
|
|
}
|
|
|
|
update RenderPost state =
|
|
|
|
noEffects $ state { hack = mdify "blogpost" }
|
|
|
|
|
|
|
|
view :: State -> Html Action
|
|
|
|
view { id: id, status: status, post: (Post post) } =
|
|
|
|
case id of
|
|
|
|
Nothing -> div [] []
|
|
|
|
(Just _) ->
|
2016-12-14 20:04:18 +00:00
|
|
|
div [ className "row" ]
|
2016-12-14 19:54:17 +00:00
|
|
|
[ h1 [] [ text status ]
|
2016-12-17 14:15:29 +00:00
|
|
|
, documentTitle [ title $ post.title <> " - Christine Dodrill" ] []
|
2016-12-14 21:59:08 +00:00
|
|
|
, div [ className "col s8 offset-s2" ]
|
2016-12-18 05:14:25 +00:00
|
|
|
[ p [ id_ "blogpost", dangerouslySetInnerHTML post.body ] [] ]
|
2016-12-14 19:54:17 +00:00
|
|
|
]
|