From a6727f104f808e533052f2bd83bc89cd6bfa0522 Mon Sep 17 00:00:00 2001 From: Joris Guyonvarch Date: Sun, 19 Jul 2015 00:45:42 +0200 Subject: Adding UI to sign in and sign out --- src/client/ServerCommunication.elm | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/client/ServerCommunication.elm (limited to 'src/client/ServerCommunication.elm') 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 -- cgit v1.2.3