datarekisteri/Client/FormFields.hs

62 lines
2.2 KiB
Haskell

{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Client.FormFields where
import Relude
import Yesod
import Client.Types
import Server.Types
emailField :: Field Handler Email
emailField = Field
{ fieldParse = \rawValues _ ->
case rawValues of
[] -> pure $ Right Nothing
[x] -> pure $ maybe (Left "could not parse as an email address") (Right . Just) $ toEmail x
_ -> pure $ Left $ "Expected one value"
, fieldView = \id name otherAttributes result isRequired ->
let result' = either (\x -> x) renderEmail result
in [whamlet|
<input type="email" id="#{id}" name="#{name}" value="#{result'}" *{otherAttributes} :isRequired:required="true">
|]
, fieldEnctype = UrlEncoded
}
telephoneField :: Field Handler PhoneNumber
telephoneField = Field
{ fieldParse = \rawValues _ ->
case rawValues of
[] -> pure $ Right Nothing
[x] -> pure $ maybe (Left "could not parse as a phone number") (Right . Just) $ toPhoneNumber x
_ -> pure $ Left $ "Expected one value"
, fieldView = \id name otherAttributes result isRequired ->
let result' = either (\x -> x) renderPhoneNumber result
in [whamlet|
<input type="tel" id="#{id}" name="#{name}" value="#{result'}" *{otherAttributes} :isRequired:required="true">
|]
, fieldEnctype = UrlEncoded
}
verifiedPasswordField :: Field Handler Text
verifiedPasswordField = Field
{ fieldParse = \rawValues _ ->
case rawValues of
[] -> pure $ Right Nothing
[x,y]
| x == y -> pure $ Right $ Just x
| otherwise -> pure $ Left "Salasanat eivät täsmää"
_ -> pure $ Left "Expected two values"
, fieldView = \id name otherAttributes _ isRequired ->
[whamlet|
<input type="password" id="#{id}" name="#{name}" :isRequired:required="true" *{otherAttributes}>
<label for="#{id}-confirm">
Toista salasana
<input type="password" id="#{id}-confirm" name="#{name}" :isRequired:required="true" *{otherAttributes}>
|]
, fieldEnctype = UrlEncoded
}