<commit_before>module Component.Article where

import Prelude
import Halogen
import qualified Halogen.HTML.Indexed as H
import qualified Halogen.HTML.Events.Indexed as E
import qualified Halogen.HTML.Properties.Indexed as P
import Control.Monad.Aff (Aff())

import Network.HTTP.Affjax (AJAX())
import qualified Network.HTTP.Affjax as Ajax

import Model

type State = Article

data Query a = Toggle a
             | Load Int a

type ArticleEffects eff = HalogenEffects (ajax :: AJAX | eff)

article :: forall eff. Component State Query (Aff (ArticleEffects eff))
article = component render eval
  where

  render :: State -> ComponentHTML Query
  render state =
    let title = H.h2 [E.onClick (E.input_ Toggle)
                     , P.initializer \_ -> action (Load state.id)]
                [H.text state.title] in
    H.div_
      if state.visible
      then
        [ title
        , H.text state.contents
        ]
      else
         [title]

  eval ::  Natural Query (ComponentDSL State Query (Aff (ArticleEffects eff)))
  eval (Toggle next) = do
    modify (\s -> s {visible = not s.visible})
    pure next
  eval (Load id next) = do
    c <- liftAff' $ getBlog id
    modify (\s -> s {contents = c})
    pure next

getBlog :: forall eff. Int -> Aff (ajax :: AJAX | eff) String
getBlog id =
  let url = "api/blog/" ++ show id in
  do
    res <- Ajax.get url
    return res.response<commit_msg>Parse the JSON<commit_after>module Component.Article where

import Prelude
import Halogen
import qualified Halogen.HTML.Indexed as H
import qualified Halogen.HTML.Events.Indexed as E
import qualified Halogen.HTML.Properties.Indexed as P
import Control.Monad.Aff (Aff())
import Data.Either (Either(..))
import Network.HTTP.Affjax (AJAX())
import qualified Network.HTTP.Affjax as Ajax

import Data.Foreign
import Data.Foreign.Class

import Model

type State = Article

data Query a = Toggle a
             | Load Int a

type ArticleEffects eff = HalogenEffects (ajax :: AJAX | eff)
data ArticleContents = ArticleContents {title :: String, contents :: String}

article :: forall eff. Component State Query (Aff (ArticleEffects eff))
article = component render eval
  where

  render :: State -> ComponentHTML Query
  render state =
    let title = H.h2 [E.onClick (E.input_ Toggle)
                     , P.initializer \_ -> action (Load state.id)]
                [H.text state.title] in
    H.div_
      if state.visible
      then
        [ title
        , H.text state.contents
        ]
      else
         [title]

  eval ::  Natural Query (ComponentDSL State Query (Aff (ArticleEffects eff)))
  eval (Toggle next) = do
    modify (\s -> s {visible = not s.visible})
    pure next
  eval (Load id next) = do
    c <- liftAff' $ getBlog id
    modify (\s -> s {contents = c.contents, title = c.title})
    pure next

getBlog :: forall eff. Int -> Aff (ajax :: AJAX | eff) {title :: String, contents :: String}
getBlog id =
  let url = "api/blog/" ++ show id in
  do
    res <- Ajax.get url
    return case (do
      title <- readProp "title" res.response
      contents <- readProp "content" res.response
      return {title : title, contents: contents}) of
             Right c -> c 
             Left _ -> {title : "", contents : ""}
    
{'fraction_non_alphanumeric': 0.10049321824907521, 'fraction_numerical': 0.0006165228113440197, 'mean_word_length': 2.550328227571116, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4659999', 'n_tokens_mistral': 980, 'n_tokens_neox': 942, 'n_words': 351}