aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/BlogEntry.purs
diff options
context:
space:
mode:
authorChristine Dodrill <me@christine.website>2016-12-14 11:54:17 -0800
committerChristine Dodrill <me@christine.website>2016-12-14 11:54:17 -0800
commit81206835d5ddb8760086085d758b049f73ea0801 (patch)
treeb1a26f35e5c5a8c60bf620438fc3a5c424e2f113 /frontend/src/BlogEntry.purs
parent4a52f5006bc8c1e63d68c2bad04c7e3f33a6e11d (diff)
downloadxesite-81206835d5ddb8760086085d758b049f73ea0801.tar.xz
xesite-81206835d5ddb8760086085d758b049f73ea0801.zip
frontend: render blog posts
Diffstat (limited to 'frontend/src/BlogEntry.purs')
-rw-r--r--frontend/src/BlogEntry.purs79
1 files changed, 79 insertions, 0 deletions
diff --git a/frontend/src/BlogEntry.purs b/frontend/src/BlogEntry.purs
new file mode 100644
index 0000000..c104a78
--- /dev/null
+++ b/frontend/src/BlogEntry.purs
@@ -0,0 +1,79 @@
+module App.BlogEntry where
+
+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)
+import Pux.Html (Html, div, h1, h2, p, text)
+import Pux.Html.Attributes (id_)
+
+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 _) ->
+ div []
+ [ h1 [] [ text status ]
+ , div []
+ [ p [ id_ "blogpost" ] [ text post.body ] ]
+ ]
+
+foreign import mdify :: String -> String