{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Ditto.Lucid where
import Data.Foldable (traverse_)
import Data.Text (Text)
import Lucid
import Ditto.Core
import Ditto.Generalized as G
import Ditto.Result (FormId)
import qualified Data.Text as T
encodeFormId :: FormId -> Text
encodeFormId = T.pack . show
formGenGET
:: (Applicative m)
=> Text
-> [(Text, Text)]
-> HtmlT m b
-> HtmlT m b
formGenGET action hidden children = do
form_ [action_ action, method_ "GET", enctype_ "application/xxx-form-urlencoded"] $
traverse_ mkHidden hidden *>
children
where
mkHidden (name, value) = input_ [type_ "hidden", name_ name, value_ value]
formGenPOST
:: (Applicative m)
=> Text
-> [(Text, Text)]
-> HtmlT m b
-> HtmlT m b
formGenPOST action hidden children = do
form_ [action_ action, method_ "POST", enctype_ "application/xxx-form-urlencoded"] $
traverse_ mkHidden hidden *>
children
where
mkHidden (name, value) = input_ [type_ "hidden", name_ name, value_ value]
setAttr
:: (Monad m, Functor m, Applicative f)
=> [Attribute]
-> Form m input error (HtmlT f ()) a
-> Form m input error (HtmlT f ()) a
setAttr attr form = mapView (\x -> x `with` attr) form
errorList
:: (Monad m, ToHtml error, Monad f)
=> Form m input error (HtmlT f ()) ()
errorList = G.errors mkErrors
where
mkErrors :: Monad f => ToHtml a => [a] -> HtmlT f ()
mkErrors [] = mempty
mkErrors errs = ul_ [class_ "ditto-error-list"] $ traverse_ mkError errs
mkError :: Monad f => ToHtml a => a -> HtmlT f ()
mkError e = li_ [] $ toHtml e
childErrorList
:: (Monad m, ToHtml error, Monad f)
=> Form m input error (HtmlT f ()) ()
childErrorList = G.childErrors mkErrors
where
mkErrors :: Monad f => ToHtml a => [a] -> HtmlT f ()
mkErrors [] = mempty
mkErrors errs = ul_ [class_ "ditto-error-list"] $ traverse_ mkError errs
mkError :: Monad f => ToHtml a => a -> HtmlT f ()
mkError e = li_ [] $ toHtml e
withErrors
:: (Monad m, ToHtml error, Monad f)
=> [Attribute]
-> ([error] -> HtmlT f ())
-> Form m input error (HtmlT f ()) a
-> Form m input error (HtmlT f ()) a
withErrors attrs renderError form = G.withErrors mkErrors form
where
mkErrors formlet errs =
formlet `with` attrs
<* renderError errs
br :: (Monad m, Applicative f) => Form m input error (HtmlT f ()) ()
br = view (br_ [])
fieldset
:: (Monad m, Functor m, Applicative f)
=> Form m input error (HtmlT f ()) a
-> Form m input error (HtmlT f ()) a
fieldset frm = mapView (fieldset_ [class_ "ditto"]) frm
ol
:: (Monad m, Functor m, Applicative f)
=> Form m input error (HtmlT f ()) a
-> Form m input error (HtmlT f ()) a
ol frm = mapView (ol_ [class_ "ditto"]) frm
ul
:: (Monad m, Functor m, Applicative f)
=> Form m input error (HtmlT f ()) a
-> Form m input error (HtmlT f ()) a
ul frm = mapView (ul_ [class_ "ditto"]) frm
li
:: (Monad m, Functor m, Applicative f)
=> Form m input error (HtmlT f ()) a
-> Form m input error (HtmlT f ()) a
li frm = mapView (li_ [class_ "ditto"]) frm