~alcinnz/bureaucromancy

ref: 8a7fc93730e66a3d0aa2d880a919c5e10247a8f0 bureaucromancy/src/Text/HTML/Form/WebApp.hs -rw-r--r-- 1.0 KiB
8a7fc937 — Adrian Cochrane Add infrastructure for rendering Ginger templates. 1 year, 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{-# 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