<commit_before>{-# LANGUAGE OverloadedStrings #-}
module Formalize.Actions
    ( home
    , submit
    ) where

import Control.Monad.IO.Class (liftIO)
import Data.String.Conversions as C
import Formalize.Html
import Formalize.Types
import Formalize.Pdf
import Formalize.Util
import Formalize.Validate
import Web.Spock

-- TODO: Extract logic out of Action.
-- Parse params and return PDF.
submit :: FormalizeAction ctx a
submit = do
    ps <- params
    case formFromParams ps of
     -- TODO: Show error message to user on redirect.
      Nothing         -> redirect "/"
      Just (formData) -> do
          pdf <- liftIO (createPDF formData)
          state <- getState
          liftIO (savePDF (sPath state) pdf)
          setHeader "Content-Type" "application/pdf"
          bytes pdf

-- Render form.
home :: FormalizeAction ctx a
home = do
    x <- liftIO formHtml
    html $ C.cs x
<commit_msg>Simplify Actions.<commit_after>{-# LANGUAGE OverloadedStrings #-}
module Formalize.Actions
    ( home
    , submit
    ) where

import Control.Monad.IO.Class (liftIO)
import Data.Text.Lazy as LT
import Formalize.Html
import Formalize.Types
import Formalize.Pdf
import Formalize.Util
import Formalize.Validate
import Web.Spock

-- Handle successful submit.
submitSuccess :: FormData -> FormalizeAction ctx a
submitSuccess formData = do
    pdf <- liftIO $ createPDF formData
    path <- fmap sPath getState
    liftIO $ savePDF path pdf
    setHeader "Content-Type" "application/pdf"
    bytes pdf

-- Handle failed submit.
submitFailure :: FormalizeAction ctx a
submitFailure = redirect "/"

-- Parse params and return PDF.
submit :: FormalizeAction ctx a
submit = maybe submitFailure submitSuccess =<< fmap formFromParams params

-- Render form.
home :: FormalizeAction ctx a
home = html =<< fmap LT.toStrict (liftIO formHtml)

{'fraction_non_alphanumeric': 0.08410704533042053, 'fraction_numerical': 0.0, 'mean_word_length': 3.126126126126126, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 6, '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': '6056330', 'n_tokens_mistral': 558, 'n_tokens_neox': 546, 'n_words': 186}