{-# LANGUAGE OverloadedStrings #-}
module Text.HTML.Form.WebApp (renderPage, Form(..)) where
import Data.ByteString as BS
import Data.Text as Txt
import Text.Read (readMaybe)
import Text.HTML.Form (Form(..), Input(..))
import Text.HTML.Form.WebApp.Ginger (template)
renderPage :: Form -> [Text] -> [(ByteString, Maybe ByteString)] -> IO (Maybe Text)
renderPage form (n:ix:path) query
| Just ix' <- readMaybe $ Txt.unpack ix, ix' < Prelude.length inputs' =
renderInput form (inputs' !! ix') path query
| input:_ <- inputs' = renderInput form input (ix:path) query
where inputs' = Prelude.filter (\x -> inputName x == n) $ inputs form
renderPage form [n] query
| input:_ <- Prelude.filter (\x -> inputName x == n) $ inputs form =
renderInput form input [] query
renderPage _ _ _ = return Nothing
renderInput :: Form -> Input -> [Text] -> [(ByteString, Maybe ByteString)] ->
IO (Maybe Text)
renderInput form input@Input {inputType = "checkbox"} [] q =
template "checkbox" form input q
renderInput _ _ _ _ = return Nothing