Created
July 29, 2023 16:23
-
-
Save matthsena/62419d1c47cb7d206a090d75cdc5e9b1 to your computer and use it in GitHub Desktop.
PureScript Login Form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Prelude | |
import Data.Maybe (Maybe(..)) | |
import Effect (Effect) | |
import Effect.Aff.Class (class MonadAff) | |
import Effect.Console (log) | |
import Effect.Class (liftEffect) | |
import Halogen as H | |
import Halogen.Aff as HA | |
import Halogen.HTML as HH | |
import Halogen.HTML.Events as HE | |
import Halogen.HTML.Properties as HP | |
import Halogen.VDom.Driver (runUI) | |
data Action | |
= UpdateUsername String | |
| UpdatePassword String | |
| Submit | |
type State | |
= { username :: String | |
, password :: String | |
} | |
handleAction :: Action -> H.HalogenM State Action () Void Effect Unit | |
handleAction = case _ of | |
UpdateUsername username -> do | |
H.modify_ (_ { username = username }) | |
UpdatePassword password -> do | |
H.modify_ (_ { password = password }) | |
Submit -> do | |
s <- H.get | |
liftEffect $ log $ "Submitting: " <> s.username <> ", " <> s.password | |
component :: H.Component HH.HTML Action State Void | |
component = | |
H.mkComponent | |
{ initialState: const { username: "", password: "" } | |
, render | |
, eval: H.mkEval $ H.defaultEval { handleAction = handleAction } | |
} | |
render :: State -> H.ComponentHTML Action () | |
render state = | |
HH.form_ | |
[ HH.input | |
[ HP.type_ HP.InputText | |
, HP.value state.username | |
, HE.onValueInput $ Just <<< UpdateUsername | |
] | |
, HH.input | |
[ HP.type_ HP.InputPassword | |
, HP.value state.password | |
, HE.onValueInput $ Just <<< UpdatePassword | |
] | |
, HH.button | |
[ HE.onClick (\_ -> Just Submit) ] | |
[ HH.text "Submit" ] | |
] | |
main :: Effect Unit | |
main = | |
HA.runHalogenAff do | |
body <- HA.awaitBody | |
runUI component { username: "", password: "" } body |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment