diff options
Diffstat (limited to 'src/client/ServerCommunication.elm')
-rw-r--r-- | src/client/ServerCommunication.elm | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/client/ServerCommunication.elm b/src/client/ServerCommunication.elm new file mode 100644 index 0000000..e29b084 --- /dev/null +++ b/src/client/ServerCommunication.elm @@ -0,0 +1,63 @@ +module ServerCommunication + ( Communication(..) + , sendRequest + , serverCommunications + ) where + +import Signal +import Task +import Task exposing (Task) +import Http + +import Update as U + +type Communication = + NoCommunication + | SignIn String + | SignOut + +serverCommunications : Signal.Mailbox Communication +serverCommunications = Signal.mailbox NoCommunication + +sendRequest : Communication -> Task Http.RawError U.Action +sendRequest communication = + case getRequest communication of + Nothing -> + Task.succeed U.NoOp + Just request -> + Http.send Http.defaultSettings request + |> Task.map (communicationToAction communication) + +getRequest : Communication -> Maybe Http.Request +getRequest communication = + case communication of + NoCommunication -> + Nothing + SignIn login -> + Just + { verb = "post" + , headers = [] + , url = "/signIn?login=" ++ login + , body = Http.empty + } + SignOut -> + Just + { verb = "post" + , headers = [] + , url = "/signOut" + , body = Http.empty + } + +communicationToAction : Communication -> Http.Response -> U.Action +communicationToAction communication response = + if response.status == 200 + then + case communication of + NoCommunication -> + U.NoOp + SignIn _ -> + U.NoOp + SignOut -> + U.SignIn + else + U.NoOp |