{-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE FlexibleContexts #-} module Handler.Utils.Form where import Import -- import Data.Time import Data.Proxy import qualified Data.Map as Map import Handler.Utils.DateTime import Data.String (IsString(..)) import qualified Data.Foldable as Foldable -- import Yesod.Core import qualified Data.Text as T -- import Yesod.Form.Types import Yesod.Form.Functions (parseHelper) import Yesod.Form.Bootstrap3 import Web.PathPieces (showToPathPiece, readFromPathPiece) import Text.Blaze (Markup) ---------------------------- -- Buttons (new version ) -- ---------------------------- class (Enum a, Bounded a, Ord a, PathPiece a) => Button a where label :: a -> Widget label = toWidget . toPathPiece buttonForm :: Button a => Markup -> MForm Handler (FormResult a, (a -> FieldView UniWorX)) buttonForm html = do let buttonValues = [minBound..maxBound] buttonMap = Map.fromList $ zip buttonValues buttonValues button b = Field parse view UrlEncoded where parse [] _ = return $ Right Nothing parse [str] _ | str == toPathPiece b = return $ Right $ Just b | otherwise = return $ Left "Wrong button value" parse _ _ = return $ Left "Multiple button values" view id name attrs _val _ = do [whamlet| #{html}