frontend: make mdify return rendered html

This commit is contained in:
Cadey Ratio 2016-12-18 07:03:16 -08:00
parent 62ffa5f008
commit e1a6482dc6
2 changed files with 5 additions and 16 deletions

View File

@ -8,18 +8,16 @@ import Data.Either (Either(..), either)
import Data.Maybe (Maybe(..)) import Data.Maybe (Maybe(..))
import Network.HTTP.Affjax (AJAX, get) import Network.HTTP.Affjax (AJAX, get)
import Prelude (bind, pure, show, ($), (<>), (<<<)) import Prelude (bind, pure, show, ($), (<>), (<<<))
import Pux (EffModel, noEffects) import Pux (noEffects, EffModel)
import Pux.DocumentTitle (documentTitle) import Pux.DocumentTitle (documentTitle)
import Pux.Html (Html, div, h1, p, text) import Pux.Html (Html, div, h1, p, text)
import Pux.Html.Attributes (dangerouslySetInnerHTML, className, id_, title) import Pux.Html.Attributes (dangerouslySetInnerHTML, className, id_, title)
data Action = RequestPost data Action = RequestPost
| ReceivePost (Either String Post) | ReceivePost (Either String Post)
| RenderPost
type State = type State =
{ status :: String { status :: String
, hack :: String
, id :: Maybe Int , id :: Maybe Int
, post :: Post , post :: Post
, name :: String } , name :: String }
@ -40,7 +38,6 @@ instance decodeJsonPost :: DecodeJson Post where
init :: State init :: State
init = init =
{ status: "Loading..." { status: "Loading..."
, hack: ""
, post: Post , post: Post
{ title: "" { title: ""
, body: "" , body: ""
@ -51,10 +48,7 @@ init =
update :: Action -> State -> EffModel State Action (ajax :: AJAX, dom :: DOM) update :: Action -> State -> EffModel State Action (ajax :: AJAX, dom :: DOM)
update (ReceivePost (Left err)) state = update (ReceivePost (Left err)) state =
noEffects $ state { id = Nothing, status = err } noEffects $ state { id = Nothing, status = err }
update (ReceivePost (Right post)) state = update (ReceivePost (Right post)) state = noEffects $ state { status = "", id = Just 1, post = post }
{ state: state { status = "", id = Just 1, post = post }
, effects: [ pure $ RenderPost ]
}
update RequestPost state = update RequestPost state =
{ state: state { state: state
, effects: [ do , effects: [ do
@ -64,8 +58,6 @@ update RequestPost state =
pure $ ReceivePost post pure $ ReceivePost post
] ]
} }
update RenderPost state =
noEffects $ state { hack = mdify "blogpost" }
view :: State -> Html Action view :: State -> Html Action
view { id: id, status: status, post: (Post post) } = view { id: id, status: status, post: (Post post) } =
@ -76,5 +68,5 @@ view { id: id, status: status, post: (Post post) } =
[ h1 [] [ text status ] [ h1 [] [ text status ]
, documentTitle [ title $ post.title <> " - Christine Dodrill" ] [] , documentTitle [ title $ post.title <> " - Christine Dodrill" ] []
, div [ className "col s8 offset-s2" ] , div [ className "col s8 offset-s2" ]
[ p [ id_ "blogpost", dangerouslySetInnerHTML post.body ] [] ] [ p [ id_ "blogpost", dangerouslySetInnerHTML $ mdify post.body ] [] ]
] ]

View File

@ -1,9 +1,6 @@
// Module App.BlogEntry // Module App.BlogEntry
exports.mdify = function(id) { exports.mdify = function(corpus) {
var converter = new showdown.Converter() var converter = new showdown.Converter()
elem = document.getElementById(id); return converter.makeHtml(corpus);
md = elem.innerHTML;
elem.innerHTML = converter.makeHtml(md);
return "done :)";
} }