Buttons reworked after call; not yet working
This commit is contained in:
parent
25547be0fc
commit
c71910f22e
@ -16,33 +16,42 @@ import Data.Time
|
|||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import Yesod.Form.Bootstrap3
|
import Yesod.Form.Bootstrap3
|
||||||
|
|
||||||
|
import Web.PathPieces (showToPathPiece, readFromPathPiece)
|
||||||
|
|
||||||
import Colonnade
|
import Colonnade
|
||||||
import Yesod.Colonnade
|
import Yesod.Colonnade
|
||||||
|
|
||||||
import qualified Data.UUID.Cryptographic as UUID
|
import qualified Data.UUID.Cryptographic as UUID
|
||||||
|
|
||||||
|
-- BEGIN - Buttons needed only here
|
||||||
|
data CreateButton = CreateMath | CreateInf -- Dummy for Example
|
||||||
|
deriving (Enum, Eq, Ord, Bounded, Read, Show)
|
||||||
|
|
||||||
|
instance PathPiece CreateButton where -- for displaying the button only, not really for paths
|
||||||
|
toPathPiece = showToPathPiece
|
||||||
|
fromPathPiece = readFromPathPiece
|
||||||
|
|
||||||
|
instance Button CreateButton where
|
||||||
|
label CreateMath = [whamlet|Mathematik|]
|
||||||
|
label CreateInf = "Informatik"
|
||||||
|
|
||||||
|
-- END Button needed here
|
||||||
|
|
||||||
|
|
||||||
getHomeR :: Handler Html
|
getHomeR :: Handler Html
|
||||||
getHomeR = defaultLayout $ do
|
getHomeR = do
|
||||||
(crInfWdgt, crInfEnctype) <- generateFormPost $ postButtonForm "Informatik"
|
(crBtnWdgt, crBtnEnctype) <- generateFormPost $ buttonForm
|
||||||
(crMatWdgt, crMatEnctype) <- generateFormPost $ postButtonForm "Mathematik"
|
defaultLayout $ do
|
||||||
setTitle "Willkommen zum ReWorX Test!"
|
setTitle "Willkommen zum ReWorX Test!"
|
||||||
$(widgetFile "home")
|
$(widgetFile "home")
|
||||||
|
|
||||||
|
|
||||||
postHomeR :: Handler Html
|
postHomeR :: Handler Html
|
||||||
postHomeR = do
|
postHomeR = do
|
||||||
((infResult,_), _) <- runFormPost $ postButtonForm "Informatik"
|
((btnResult,_), _) <- runFormPost $ buttonForm
|
||||||
((matResult,_), _) <- runFormPost $ postButtonForm "Mathematik"
|
$(logDebug) $ tshow btnResult
|
||||||
$(logDebug) $ tshow infResult
|
case btnResult of
|
||||||
$(logDebug) $ tshow matResult
|
(FormSuccess CreateInf) -> setMessage "Informatik anlegen"
|
||||||
setMessage "ButtonTest"
|
(FormSuccess CreateMath) -> setMessage "Mathematik anlegen"
|
||||||
case infResult of
|
|
||||||
(FormSuccess ()) -> setMessage "Informatik anlegen" -- does not work somehow
|
|
||||||
-- TODO runDB
|
|
||||||
_other -> return ()
|
|
||||||
case matResult of
|
|
||||||
(FormSuccess ()) -> setMessage "Mathematik anlegen"
|
|
||||||
-- TODO runDB
|
|
||||||
_other -> return ()
|
_other -> return ()
|
||||||
getHomeR
|
getHomeR
|
||||||
|
|||||||
@ -27,62 +27,39 @@ import Text.Blaze (Markup)
|
|||||||
-- Buttons (new version ) --
|
-- Buttons (new version ) --
|
||||||
----------------------------
|
----------------------------
|
||||||
|
|
||||||
class (Enum a, Bounded a, PathPiece a) => Button a where
|
class (Enum a, Bounded a, Ord a, PathPiece a) => Button a where
|
||||||
label :: a -> Widget
|
label :: a -> Widget
|
||||||
label = toWidget . toPathPiece
|
label = toWidget . toPathPiece
|
||||||
|
|
||||||
data CreateButton = CreateMath | CreateInf
|
|
||||||
deriving (Enum, Bounded, Read, Show)
|
|
||||||
|
|
||||||
|
buttonForm :: Button a => Markup -> MForm Handler (FormResult a, (a -> FieldView UniWorX))
|
||||||
instance PathPiece CreateButton where
|
buttonForm html = do
|
||||||
toPathPiece = showToPathPiece
|
|
||||||
fromPathPiece = readFromPathPiece
|
|
||||||
|
|
||||||
instance Button CreateButton
|
|
||||||
|
|
||||||
|
|
||||||
-- buttonForm :: (Monad m, Button a) => a -> Markup -> MForm m (FormResult (Maybe a), (a -> Widget))
|
|
||||||
-- buttonForm :: (Monad m, Button a) => a -> Markup -> MForm m (FormResult (Maybe a), (a -> WidgetT (HandlerSite m) IO ()))
|
|
||||||
-- buttonForm :: (Monad m, Button a) => a -> MForm m a
|
|
||||||
{- Inference yields this monster:
|
|
||||||
buttonForm
|
|
||||||
:: (HandlerSite m ~ UniWorX, Ord k, Text.Blaze.ToMarkup a,
|
|
||||||
Button (Either Text k), MonadHandler m, Enum k, Bounded k,
|
|
||||||
PathPiece k) =>
|
|
||||||
Data.Proxy.Proxy k
|
|
||||||
-> a
|
|
||||||
-> Control.Monad.Trans.RWS.Lazy.RWST
|
|
||||||
(Maybe (Env, FileEnv), HandlerSite m, [Lang])
|
|
||||||
Enctype
|
|
||||||
Ints
|
|
||||||
m
|
|
||||||
(FormResult (Maybe k), k -> FieldView (HandlerSite m))
|
|
||||||
-}
|
|
||||||
buttonForm btn html = do
|
|
||||||
let
|
let
|
||||||
buttonValues = [(minBound `asProxyTypeOf` btn)..maxBound]
|
buttonValues = [minBound..maxBound]
|
||||||
buttonMap = Map.fromList $ zip buttonValues buttonValues
|
buttonMap = Map.fromList $ zip buttonValues buttonValues
|
||||||
button val = Field parse view UrlEncoded
|
button b = Field parse view UrlEncoded
|
||||||
where
|
where
|
||||||
parse [] _ = return $ Right Nothing
|
parse [] _ = return $ Right Nothing
|
||||||
parse [str] _
|
parse [str] _
|
||||||
| str == toPathPiece val = return $ Right $ Just val
|
| str == toPathPiece b = return $ Right $ Just b
|
||||||
| otherwise = return $ Left "Wrong button value"
|
| otherwise = return $ Left "Wrong button value"
|
||||||
parse _ _ = return $ Left "Multiple button values"
|
parse _ _ = return $ Left "Multiple button values"
|
||||||
|
|
||||||
view id name attrs val _ = do
|
view id name attrs _val _ = do
|
||||||
[whamlet|
|
[whamlet|
|
||||||
#{html}
|
#{html}
|
||||||
<button type=submit name=#{name} value=#{toPathPiece val} *{attrs} ##{id}>^{label val}
|
<button type=submit name=#{name} value=#{toPathPiece b} *{attrs} ##{id}>^{label b}
|
||||||
|]
|
|]
|
||||||
|
|
||||||
buttonIdent <- newFormIdent
|
buttonIdent <- newFormIdent
|
||||||
resultWidgetMap <- forM buttonMap $ \val -> mopt (button val) ("" { fsName = Just buttonIdent }) Nothing
|
resultWidgetMap <- forM buttonMap $ \val -> mopt (button val) ("" { fsName = Just buttonIdent }) Nothing
|
||||||
|
|
||||||
let result = asum $ fst <$> resultWidgetMap
|
let result = case (asum $ fst <$> resultWidgetMap) of
|
||||||
|
FormSuccess (Just x) -> FormSuccess x
|
||||||
|
FormSuccess Nothing -> FormMissing
|
||||||
|
_other -> FormMissing
|
||||||
let viewF = (Map.!) (snd <$> resultWidgetMap)
|
let viewF = (Map.!) (snd <$> resultWidgetMap)
|
||||||
|
|
||||||
|
|
||||||
return (result, viewF)
|
return (result, viewF)
|
||||||
|
|
||||||
----------------------------
|
----------------------------
|
||||||
|
|||||||
@ -65,10 +65,9 @@
|
|||||||
|
|
||||||
<li .list-group-item>
|
<li .list-group-item>
|
||||||
Institut einmalig in Datenbank einfügen:
|
Institut einmalig in Datenbank einfügen:
|
||||||
<form .form-inline method=post action=@{HomeR} enctype=#{crInfEnctype}>
|
<form .form-inline method=post action=@{HomeR} enctype=#{crBtnEnctype}>
|
||||||
^{crInfWdgt}
|
^{fvInput (crBtnWdgt CreateInf)}
|
||||||
<form .form-inline method=post action=@{HomeR} enctype=#{crMatEnctype}>
|
^{fvInput (crBtnWdgt CreateMath)}
|
||||||
^{crMatWdgt}
|
|
||||||
|
|
||||||
<li .list-group-item>
|
<li .list-group-item>
|
||||||
<a href=@{CourseEditR}>Kurse anlegen
|
<a href=@{CourseEditR}>Kurse anlegen
|
||||||
|
|||||||
@ -3,4 +3,4 @@
|
|||||||
|
|
||||||
<form method=POST enctype=#{selectEncoding} target=_blank action=@{SubmissionDownloadMultiArchiveR}>
|
<form method=POST enctype=#{selectEncoding} target=_blank action=@{SubmissionDownloadMultiArchiveR}>
|
||||||
^{submissionTable}
|
^{submissionTable}
|
||||||
<button type=submit >Markierte herunterladen
|
<button .btn .btn-default type=submit >Markierte herunterladen
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user