{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
module DevForms (ServerBuilder, FormBuilder, QuestionBuilder, devFormServer, form, questionCheckbox, questionLikert, questionChoice, questionDate, questionTime, questionInteger, setLowerBoundInclusive, setUpperBoundInclusive) where
import Control.Monad.Writer
import Form (Form (..), FormBuilder)
import Question (Question (..), QuestionBuilder, QuestionOptions (..))
import Server (Server (..), ServerBuilder, runServer)
devFormServer :: Int -> ServerBuilder () -> IO ()
devFormServer :: Int -> ServerBuilder () -> IO ()
devFormServer = Int -> ServerBuilder () -> IO ()
runServer
form :: Text -> Text -> FormBuilder () -> ServerBuilder ()
form :: Text -> Text -> FormBuilder () -> ServerBuilder ()
form Text
title Text
formId FormBuilder ()
formBuilder = do
let f :: Form
f = Endo Form -> Form -> Form
forall a. Endo a -> a -> a
appEndo (FormBuilder () -> Endo Form
forall w a. Writer w a -> w
execWriter FormBuilder ()
formBuilder) (Form -> Form) -> Form -> Form
forall a b. (a -> b) -> a -> b
$ Form{title :: Text
title = Text
title, formId :: Text
formId = Text
formId, questions :: [Question]
questions = []}
Endo Server -> ServerBuilder ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell (Endo Server -> ServerBuilder ())
-> Endo Server -> ServerBuilder ()
forall a b. (a -> b) -> a -> b
$ (Server -> Server) -> Endo Server
forall a. (a -> a) -> Endo a
Endo ((Server -> Server) -> Endo Server)
-> (Server -> Server) -> Endo Server
forall a b. (a -> b) -> a -> b
$ \server :: Server
server@Server{[Form]
forms :: [Form]
forms :: Server -> [Form]
forms} -> Server
server{forms = forms <> [f]}
addQuestion :: (MonadWriter (Endo Form) m) => Question -> m ()
addQuestion :: forall (m :: * -> *). MonadWriter (Endo Form) m => Question -> m ()
addQuestion Question
question =
Endo Form -> m ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell (Endo Form -> m ()) -> Endo Form -> m ()
forall a b. (a -> b) -> a -> b
$ (Form -> Form) -> Endo Form
forall a. (a -> a) -> Endo a
Endo ((Form -> Form) -> Endo Form) -> (Form -> Form) -> Endo Form
forall a b. (a -> b) -> a -> b
$ \f :: Form
f@Form{[Question]
questions :: Form -> [Question]
questions :: [Question]
questions} -> Form
f{questions = questions <> [question]}
questionCheckbox :: Text -> FormBuilder ()
questionCheckbox :: Text -> FormBuilder ()
questionCheckbox =
Question -> FormBuilder ()
forall (m :: * -> *). MonadWriter (Endo Form) m => Question -> m ()
addQuestion (Question -> FormBuilder ())
-> (Text -> Question) -> Text -> FormBuilder ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Question
QuestionCheckbox
questionLikert :: Text -> FormBuilder ()
questionLikert :: Text -> FormBuilder ()
questionLikert = Question -> FormBuilder ()
forall (m :: * -> *). MonadWriter (Endo Form) m => Question -> m ()
addQuestion (Question -> FormBuilder ())
-> (Text -> Question) -> Text -> FormBuilder ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Question
QuestionLikert
questionChoice :: Text -> [Text] -> FormBuilder ()
questionChoice :: Text -> [Text] -> FormBuilder ()
questionChoice Text
title [Text]
qOptions =
Question -> FormBuilder ()
forall (m :: * -> *). MonadWriter (Endo Form) m => Question -> m ()
addQuestion (Question -> FormBuilder ()) -> Question -> FormBuilder ()
forall a b. (a -> b) -> a -> b
$ Text -> [Text] -> Question
QuestionChoice Text
title [Text]
qOptions
questionDate :: Text -> FormBuilder ()
questionDate :: Text -> FormBuilder ()
questionDate = Question -> FormBuilder ()
forall (m :: * -> *). MonadWriter (Endo Form) m => Question -> m ()
addQuestion (Question -> FormBuilder ())
-> (Text -> Question) -> Text -> FormBuilder ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Question
QuestionDate
questionTime :: Text -> FormBuilder ()
questionTime :: Text -> FormBuilder ()
questionTime = Question -> FormBuilder ()
forall (m :: * -> *). MonadWriter (Endo Form) m => Question -> m ()
addQuestion (Question -> FormBuilder ())
-> (Text -> Question) -> Text -> FormBuilder ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Question
QuestionTime
questionInteger :: Text -> QuestionBuilder () -> FormBuilder ()
questionInteger :: Text -> QuestionBuilder () -> FormBuilder ()
questionInteger Text
title QuestionBuilder ()
questionBuilder = do
let questionOptions :: QuestionOptions
questionOptions = Endo QuestionOptions -> QuestionOptions -> QuestionOptions
forall a. Endo a -> a -> a
appEndo (QuestionBuilder () -> Endo QuestionOptions
forall w a. Writer w a -> w
execWriter QuestionBuilder ()
questionBuilder) (QuestionOptions -> QuestionOptions)
-> QuestionOptions -> QuestionOptions
forall a b. (a -> b) -> a -> b
$ QuestionOptions{lowerBoundInclusive :: Maybe Integer
lowerBoundInclusive = Maybe Integer
forall a. Maybe a
Nothing, upperBoundInclusive :: Maybe Integer
upperBoundInclusive = Maybe Integer
forall a. Maybe a
Nothing}
Question -> FormBuilder ()
forall (m :: * -> *). MonadWriter (Endo Form) m => Question -> m ()
addQuestion (Question -> FormBuilder ()) -> Question -> FormBuilder ()
forall a b. (a -> b) -> a -> b
$ Text -> QuestionOptions -> Question
QuestionInteger Text
title QuestionOptions
questionOptions
setLowerBoundInclusive :: Integer -> QuestionBuilder ()
setLowerBoundInclusive :: Integer -> QuestionBuilder ()
setLowerBoundInclusive Integer
bound = Endo QuestionOptions -> QuestionBuilder ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell (Endo QuestionOptions -> QuestionBuilder ())
-> Endo QuestionOptions -> QuestionBuilder ()
forall a b. (a -> b) -> a -> b
$ (QuestionOptions -> QuestionOptions) -> Endo QuestionOptions
forall a. (a -> a) -> Endo a
Endo ((QuestionOptions -> QuestionOptions) -> Endo QuestionOptions)
-> (QuestionOptions -> QuestionOptions) -> Endo QuestionOptions
forall a b. (a -> b) -> a -> b
$ \QuestionOptions
questionOptions -> QuestionOptions
questionOptions{lowerBoundInclusive = Just bound}
setUpperBoundInclusive :: Integer -> QuestionBuilder ()
setUpperBoundInclusive :: Integer -> QuestionBuilder ()
setUpperBoundInclusive Integer
bound = Endo QuestionOptions -> QuestionBuilder ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell (Endo QuestionOptions -> QuestionBuilder ())
-> Endo QuestionOptions -> QuestionBuilder ()
forall a b. (a -> b) -> a -> b
$ (QuestionOptions -> QuestionOptions) -> Endo QuestionOptions
forall a. (a -> a) -> Endo a
Endo ((QuestionOptions -> QuestionOptions) -> Endo QuestionOptions)
-> (QuestionOptions -> QuestionOptions) -> Endo QuestionOptions
forall a b. (a -> b) -> a -> b
$ \QuestionOptions
questionOptions -> QuestionOptions
questionOptions{upperBoundInclusive = Just bound}