aboutsummaryrefslogtreecommitdiff
path: root/src/client/ServerCommunication.elm
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/ServerCommunication.elm')
-rw-r--r--src/client/ServerCommunication.elm62
1 files changed, 42 insertions, 20 deletions
diff --git a/src/client/ServerCommunication.elm b/src/client/ServerCommunication.elm
index 9359160..719a563 100644
--- a/src/client/ServerCommunication.elm
+++ b/src/client/ServerCommunication.elm
@@ -20,8 +20,8 @@ import Update.Payment as UP
type Communication =
NoCommunication
| SignIn String
- | AddPayment String Int
- | DeletePayment PaymentId
+ | AddPayment String String Int
+ | DeletePayment PaymentId String Int Int
| UpdatePage Int
| SignOut
@@ -34,8 +34,7 @@ sendRequest communication =
Nothing ->
Task.succeed U.NoOp
Just request ->
- Http.send Http.defaultSettings request
- |> Task.map (communicationToAction communication)
+ (Http.send Http.defaultSettings request) `Task.andThen` (serverResult communication)
getRequest : Communication -> Maybe Http.Request
getRequest communication =
@@ -44,15 +43,19 @@ getRequest communication =
Nothing
SignIn login ->
Just (simple "post" ("/signIn?login=" ++ login))
- AddPayment name cost ->
- Just (simple "post" ("/payment/add?name=" ++ name ++ "&cost=" ++ (toString cost)))
- DeletePayment paymentId ->
+ AddPayment userName paymentName cost ->
+ Just (simple "post" ("/payment/add?name=" ++ paymentName ++ "&cost=" ++ (toString cost)))
+ DeletePayment paymentId _ _ _ ->
Just (simple "post" ("payment/delete?id=" ++ paymentId))
UpdatePage page ->
- Just (simple "get" ("payments?page=" ++ toString page ++ "&perPage=" ++ toString perPage))
+ Just (updatePageRequest page)
SignOut ->
Just (simple "post" "/signOut")
+updatePageRequest : Int -> Http.Request
+updatePageRequest page =
+ simple "get" ("payments?page=" ++ toString page ++ "&perPage=" ++ toString perPage)
+
simple : String -> String -> Http.Request
simple method url =
{ verb = method
@@ -61,29 +64,47 @@ simple method url =
, body = Http.empty
}
-communicationToAction : Communication -> Http.Response -> U.Action
-communicationToAction communication response =
+serverResult : Communication -> Http.Response -> Task Http.RawError U.Action
+serverResult communication response =
if response.status == 200
then
case communication of
NoCommunication ->
- U.NoOp
+ Task.succeed U.NoOp
SignIn login ->
- U.UpdateSignIn (ValidLogin login)
- AddPayment name cost ->
- decodeResponse
- response
- messageDecoder
- (\id -> U.UpdatePayment (UP.AddPayment id name cost))
- DeletePayment id ->
- U.UpdatePayment (UP.Remove id)
+ Task.succeed (U.UpdateSignIn (ValidLogin login))
+ AddPayment userName paymentName cost ->
+ Http.send Http.defaultSettings (updatePageRequest 1)
+ |> Task.map (\response ->
+ if response.status == 200
+ then
+ decodeResponse
+ response
+ paymentsDecoder
+ (\payments -> U.UpdatePayment (UP.AddPayment userName cost payments))
+ else
+ U.NoOp
+ )
+ DeletePayment id userName cost currentPage ->
+ Http.send Http.defaultSettings (updatePageRequest currentPage)
+ |> Task.map (\response ->
+ if response.status == 200
+ then
+ decodeResponse
+ response
+ paymentsDecoder
+ (\payments -> U.UpdatePayment (UP.Remove userName cost payments))
+ else
+ U.NoOp
+ )
UpdatePage page ->
decodeResponse
response
paymentsDecoder
(\payments -> U.UpdatePayment (UP.UpdatePage page payments))
+ |> Task.succeed
SignOut ->
- U.GoSignInView
+ Task.succeed (U.GoSignInView)
else
decodeResponse
response
@@ -95,6 +116,7 @@ communicationToAction communication response =
_ ->
U.NoOp
)
+ |> Task.succeed
decodeResponse : Http.Response -> Decoder a -> (a -> U.Action) -> U.Action
decodeResponse response decoder responseToAction =