{- |
Helpers for commonly used form patterns.
-}

module FlexTask.Form.Helpers (
  anonymousRadioButtons,
  labeledRadioButtons,
  labeledCheckboxes,
  ) where


import Yesod                            (FieldSettings, SomeMessage)

import FlexTask.Form.Core (
  Alignment,
  ChoiceShape(..),
  SimpleFormPiece,
  multipleChoice,
  singleChoice,
  )
import FlexTask.Form.Util                (showToUniversalLabel, universalLabel)
import FlexTask.InputTypes (
  MultipleChoiceSelection,
  SingleChoiceSelection,
  )
import FlexTask.Form.Types              (FlexForm)



{- |
A multiple choice checkbox field.
Checkboxes are labeled with their index number and the provided (monolingual) labels.
-}
labeledCheckboxes
  :: Alignment
  -- ^ vertical or horizontal
  -> FieldSettings FlexForm
  -- ^ FieldSettings of the header label: attributes, label text, etc.
  -> [String]
  -- ^ individual option labels
  -> SimpleFormPiece t MultipleChoiceSelection
labeledCheckboxes :: forall t.
Alignment
-> FieldSettings FlexForm
-> [String]
-> SimpleFormPiece t MultipleChoiceSelection
labeledCheckboxes Alignment
alignment FieldSettings FlexForm
fSettings = ChoiceShape
-> FieldSettings FlexForm
-> [SomeMessage FlexForm]
-> SimpleFormPiece t MultipleChoiceSelection
forall t.
ChoiceShape
-> FieldSettings FlexForm
-> [SomeMessage FlexForm]
-> SimpleFormPiece t MultipleChoiceSelection
multipleChoice
  (Alignment -> ChoiceShape
Buttons Alignment
alignment)
  FieldSettings FlexForm
fSettings
  ([SomeMessage FlexForm]
 -> SimpleFormPiece t MultipleChoiceSelection)
-> ([String] -> [SomeMessage FlexForm])
-> [String]
-> SimpleFormPiece t MultipleChoiceSelection
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Integer -> String -> SomeMessage FlexForm)
-> [Integer] -> [String] -> [SomeMessage FlexForm]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\Integer
a String
b -> String -> SomeMessage FlexForm
universalLabel (String -> SomeMessage FlexForm) -> String -> SomeMessage FlexForm
forall a b. (a -> b) -> a -> b
$ Integer -> String
forall a. Show a => a -> String
show Integer
a String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
". " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
b) [Integer
1 :: Integer ..]


{- |
A single choice radio button field.
Buttons are labeled with just their index number.
-}
anonymousRadioButtons
  :: Integral i
  => Alignment
  -- ^ vertical or horizontal
  -> FieldSettings FlexForm
  -- ^ heading label, attributes, etc.
  -> i
  -- ^ the amount of options to provide
  -> SimpleFormPiece t SingleChoiceSelection
anonymousRadioButtons :: forall i t.
Integral i =>
Alignment
-> FieldSettings FlexForm
-> i
-> SimpleFormPiece t SingleChoiceSelection
anonymousRadioButtons Alignment
alignment FieldSettings FlexForm
fSettings i
amount = ChoiceShape
-> FieldSettings FlexForm
-> [SomeMessage FlexForm]
-> SimpleFormPiece t SingleChoiceSelection
forall t.
ChoiceShape
-> FieldSettings FlexForm
-> [SomeMessage FlexForm]
-> SimpleFormPiece t SingleChoiceSelection
singleChoice
  (Alignment -> ChoiceShape
Buttons Alignment
alignment)
  FieldSettings FlexForm
fSettings
  ([SomeMessage FlexForm] -> SimpleFormPiece t SingleChoiceSelection)
-> [SomeMessage FlexForm]
-> SimpleFormPiece t SingleChoiceSelection
forall a b. (a -> b) -> a -> b
$ (Integer -> SomeMessage FlexForm)
-> [Integer] -> [SomeMessage FlexForm]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> SomeMessage FlexForm
forall a. Show a => a -> SomeMessage FlexForm
showToUniversalLabel [Integer
1.. i -> Integer
forall a. Integral a => a -> Integer
toInteger i
amount]


{- |
A single choice radio button field.
Buttons are labeled with the given multilingual labels.
-}
labeledRadioButtons
  :: Alignment
  -- ^ vertical or horizontal
  -> FieldSettings FlexForm
  -- ^ heading label, attributes, etc.
  -> [SomeMessage FlexForm]
  -- ^ individual option labels
  -> SimpleFormPiece t SingleChoiceSelection
labeledRadioButtons :: forall t.
Alignment
-> FieldSettings FlexForm
-> [SomeMessage FlexForm]
-> SimpleFormPiece t SingleChoiceSelection
labeledRadioButtons Alignment
alignment = ChoiceShape
-> FieldSettings FlexForm
-> [SomeMessage FlexForm]
-> SimpleFormPiece t SingleChoiceSelection
forall t.
ChoiceShape
-> FieldSettings FlexForm
-> [SomeMessage FlexForm]
-> SimpleFormPiece t SingleChoiceSelection
singleChoice (Alignment -> ChoiceShape
Buttons Alignment
alignment)