wai-handler-devel problem

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes, TypeFamilies, TemplateHaskell, MultiParamTypeClasses #-}
import Yesod.Core
import Yesod.Form

import Yesod.Form.MassInput
import Control.Applicative
import Data.Text (Text, pack)
import Network.Wai.Handler.Warp (run)

-- This is basically a copy of hello-forms.hs  in package 'yesod-form'
-- for testing purposes    27.6.2011



-- Anwendung ------------------------

data HalloWelt = HalloWelt
type Handler = GHandler HalloWelt HalloWelt

instance RenderMessage HalloWelt FormMessage where
    renderMessage _ _ = defaultFormMessage

instance Yesod HalloWelt where
    approot _ = ""

main = toWaiApp HalloWelt >>= run 2500

withHalloWelt f = toWaiApp HalloWelt >>= f

-- Parse Routes------------------------------------
    
mkYesod "HalloWelt" [parseRoutes|
/ RootR GET
/mass MassR GET
|]

-- Hauptseite ---------------------------------------

getRootR = do
    ((res, form), enctype) <- myForm
    defaultLayout [whamlet|
<p>Result: #{show res}
<form enctype=#{enctype}>
    ^{form}
    <div>
        <input type=submit>
<p>
    <a href=@{MassR}>See the mass form
|]

data Frucht = Apfel | Birne | Traube | Kiwi | Mostbirne | Zwetschge
    deriving (Show, Enum, Bounded, Eq)

fruits :: [(Text, Frucht)]
fruits = map (\x -> (pack $ show x, x)) [minBound..maxBound]

myForm = fixType $ runFormGet $ renderDivs $ pure (,,,,,,,,)
    <*> areq boolField "Bool field" Nothing
    <*> aopt boolField "Opt bool field" Nothing
    <*> areq textField "Text field" Nothing
    <*> areq (selectField fruits) "Select field" Nothing
    <*> aopt (selectField fruits) "Opt select field" Nothing
    <*> areq (multiSelectField fruits) "Multi select field" Nothing
    <*> aopt (multiSelectField fruits) "Opt multi select field" Nothing
    <*> aopt intField "Opt int field" Nothing
    <*> aopt (radioField fruits) "Opt radio" Nothing

fixType :: Handler a -> Handler a
fixType = id


-- Masseneingabe ------------------------------------------

getMassR = do
    ((res, form), enctype) <- myMassForm
    defaultLayout [whamlet|
<p>Result: #{show res}
<form enctype=#{enctype}>
    <table>
        ^{form}
    <div>
        <input type=submit>
<p>
    <a href=@{RootR}>See the regular form
|]


myMassForm = fixType $ runFormGet $ renderTable $ inputList "Menschen" massTable
    (\x -> (,)
        <$> areq textField "Name" (fmap fst x)
        <*> areq intField "Age" (fmap snd x)) (Just [("Michael", 26)])