devforms-0.1.0.0: src/Form.hs
module Form (Form (..), FormBuilder, renderForm, getSubmitUrl, getFormUrl) where
import Control.Monad.Writer
import Htmx.Lucid.Core
import Lucid
import Question (Question, renderQuestion)
data Form = Form {title :: Text, formId :: Text, questions :: [Question]} deriving (Show)
type FormBuilder = Writer (Endo Form)
renderForm :: Form -> Html ()
renderForm form@Form{title, questions} = do
main_ $ do
h2_ $ toHtml title
form_ [hxPost_ $ getSubmitUrl form, script_ "on keydown[key is 'Enter'] from <input/> halt"] $ do
forM_ (reverse questions) renderQuestion
input_ [type_ "submit"]
getFormUrl :: (IsString a) => Form -> a
getFormUrl Form{formId} = fromString $ "/" <> toString formId
getSubmitUrl :: (IsString a) => Form -> a
getSubmitUrl Form{formId} = fromString $ "/api/submit/" <> toString formId