aboutsummaryrefslogtreecommitdiff
path: root/src/client/ServerCommunication.elm
blob: e29b084a04b0122b9c8c9c120c85b46db346c316 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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