Created
December 30, 2015 20:01
-
-
Save freakingawesome/176e5b05e523e9275cb4 to your computer and use it in GitHub Desktop.
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
import Signal | |
import Http | |
import Task | |
import Effects | |
import Html exposing (..) | |
import Html.Events exposing (..) | |
import Json.Decode | |
type Action | |
= NoOp | |
| GetShortenedUrl | |
| SetShortenedUrl (Maybe String) | |
type alias Model = | |
{ shortenedUrl : Maybe String } | |
init : (Model, Effects.Effects Action) | |
init = | |
({ shortenedUrl = Nothing }, Effects.none) | |
update : Action -> Model -> (Model, Effects.Effects Action) | |
update action model = | |
case action of | |
NoOp -> | |
(model, Effects.none) | |
GetShortenedUrl -> | |
(model, shortenedUrlGetter) | |
SetShortenedUrl val -> | |
({ model | shortenedUrl = val }, Effects.none) | |
shortenedUrlGetter = | |
Http.post Json.Decode.string "./test.txt" body -- "http://localhost/tracker/link-admin/test.php" body | |
|> Task.toMaybe | |
|> Task.map SetShortenedUrl | |
|> Effects.task | |
body = Http.empty | |
--------------- | |
messages = | |
Signal.mailbox [] | |
actions : Signal.Mailbox Action | |
actions = | |
let | |
singleton action = | |
[action] | |
fromList list = | |
case list of | |
[] -> NoOp | |
(action::_) -> action | |
in | |
{ address = Signal.forwardTo messages.address singleton | |
, signal = Signal.map fromList messages.signal | |
} | |
effectsAndModel : Signal (Model, Effects.Effects Action) | |
effectsAndModel = | |
let | |
updateJustModel action (model,_) = update action model | |
in | |
Signal.foldp updateJustModel init actions.signal | |
model : Signal Model | |
model = | |
Signal.map fst effectsAndModel | |
port tasks : Signal (Task.Task Effects.Never ()) | |
port tasks = | |
Signal.map (Effects.toTask messages.address << snd) effectsAndModel | |
--------------- | |
main = | |
Signal.map (view actions.address) model | |
view address model = | |
div [] | |
[ button [ onClick address GetShortenedUrl ] [ text "Get Shortened Url" ] | |
, div [] | |
[ text "Shortened URL: " | |
, text <| toString model.shortenedUrl | |
] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment