Last active
August 29, 2015 14:26
-
-
Save beefsack/77eb6cea07723b1a74ca to your computer and use it in GitHub Desktop.
Signals confusion in Elm
This file contains hidden or 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
| import Html | |
| import Router exposing (..) | |
| import History | |
| import StartApp | |
| import View.Auth | |
| import View.Home | |
| import View.Games | |
| main : Signal Html.Html | |
| main = Signal.map route History.hash | |
| actions = Signal.mailbox Nothing | |
| address = Signal.forwardTo actions.address Just | |
| route : Route Html.Html | |
| route = match | |
| [ "" :-> View.Home.display | |
| , "#auth" :-> \url -> View.Auth.view address View.Auth.init | |
| , "#games" :-> View.Games.display | |
| ] display404 | |
| display404 = always (Html.text "Page not found") |
This file contains hidden or 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
| import Html | |
| import Router exposing (..) | |
| import History | |
| import StartApp | |
| import View.Auth | |
| import View.Home | |
| import View.Games | |
| main : Signal (Signal Html.Html) | |
| main = Signal.map route History.hash | |
| actions = Signal.mailbox Nothing | |
| address = Signal.forwardTo actions.address Just | |
| route : Route (Signal Html.Html) | |
| route = match | |
| [ "#auth" :-> always View.Auth.main | |
| ] display404 | |
| display404 = always (Signal.constant (Html.text "Page not found")) |
This file contains hidden or 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 View.Auth where | |
| import StartApp | |
| import Html | |
| import Html.Attributes as Attr | |
| import Html.Events as Evt | |
| import Json.Decode | |
| main : Signal Html.Html | |
| main = StartApp.start { model = init, view = view, update = update } | |
| type alias Model = | |
| { email : String | |
| , confirmation : String | |
| , sending : Bool | |
| } | |
| init : Model | |
| init = | |
| { email = "" | |
| , confirmation = "" | |
| , sending = False | |
| } | |
| type Action | |
| = Submit | |
| | UpdateEmail String | |
| | UpdateConfirmation String | |
| update : Action -> Model -> Model | |
| update action model = | |
| case action of | |
| Submit -> | |
| { model | | |
| sending <- True | |
| } | |
| UpdateEmail newEmail -> | |
| { model | | |
| email <- newEmail | |
| } | |
| UpdateConfirmation newConfirmation -> | |
| { model | | |
| confirmation <- newConfirmation | |
| } | |
| view : Signal.Address Action -> Model -> Html.Html | |
| view address model = | |
| Html.form | |
| [ Evt.onWithOptions | |
| "submit" | |
| { stopPropagation = False, preventDefault = True } | |
| Json.Decode.value | |
| (\_ -> Signal.message address Submit) | |
| ] | |
| [ Html.div [] | |
| [ Html.input | |
| [ Attr.placeholder "Email address" | |
| , Attr.value model.email | |
| , Attr.disabled model.sending | |
| , Evt.on | |
| "input" | |
| Evt.targetValue | |
| (\str -> Signal.message address (UpdateEmail str)) | |
| ] | |
| [] | |
| ] | |
| , Html.div [] | |
| [ Html.input | |
| [ Attr.placeholder "Code" | |
| , Attr.value model.confirmation | |
| , Attr.disabled model.sending | |
| , Evt.on | |
| "input" | |
| Evt.targetValue | |
| (\str -> Signal.message address (UpdateConfirmation str)) | |
| ] | |
| [] | |
| ] | |
| , Html.div [] | |
| (if model.sending then [ Html.text "sending" ] else []) | |
| , Html.div [] | |
| [ Html.button | |
| [ Attr.type' "submit" | |
| , Attr.disabled model.sending | |
| ] | |
| [ Html.text (if model.confirmation == "" then "Log in" else "Confirm") ] | |
| ] | |
| ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment