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.elm57
1 files changed, 30 insertions, 27 deletions
diff --git a/src/client/ServerCommunication.elm b/src/client/ServerCommunication.elm
index 1f35fa1..30bd2bf 100644
--- a/src/client/ServerCommunication.elm
+++ b/src/client/ServerCommunication.elm
@@ -12,12 +12,12 @@ import Date
import Model.Message exposing (messageDecoder)
import Model.User exposing (UserId)
-import Model.Payment exposing (PaymentId, perPage, paymentsDecoder)
+import Model.Payment exposing (..)
import Model.View.Payment.Add exposing (Frequency)
import Update as U
import Update.SignIn exposing (..)
-import Update.Payment as UP
+import Update.LoggedView as UL
type Communication =
NoCommunication
@@ -46,8 +46,8 @@ getRequest communication =
Nothing
SignIn login ->
Just (simple "post" ("/signIn?login=" ++ login))
- AddPayment userId paymentName cost frequency ->
- Just (simple "post" ("/payment/add?name=" ++ paymentName ++ "&cost=" ++ (toString cost) ++ "&frequency=" ++ (toString frequency)))
+ AddPayment userId name cost frequency ->
+ Just (simple "post" ("/payment/add?name=" ++ name ++ "&cost=" ++ (toString cost) ++ "&frequency=" ++ (toString frequency)))
DeletePayment paymentId _ _ _ ->
Just (simple "post" ("payment/delete?id=" ++ (toString paymentId)))
UpdatePage page ->
@@ -76,36 +76,40 @@ serverResult communication response =
Task.succeed U.NoOp
SignIn login ->
Task.succeed (U.UpdateSignIn (ValidLogin login))
- AddPayment userId paymentName cost frequency ->
- Http.send Http.defaultSettings (updatePageRequest 1)
- |> Task.map (\response ->
- if response.status == 200
- then
- decodeResponse
- response
- paymentsDecoder
- (\payments -> U.UpdatePayment (UP.AddPayment userId cost payments))
- else
- U.NoOp
- )
+ AddPayment userId name cost frequency ->
+ decodeResponse
+ response
+ ("id" := paymentIdDecoder)
+ (\paymentId ->
+ Http.send Http.defaultSettings (updatePageRequest 1)
+ |> flip Task.andThen (\response2 ->
+ if response2.status == 200
+ then
+ decodeResponse
+ response2
+ paymentsDecoder
+ (\payments -> Task.succeed <| U.UpdateLoggedView (UL.AddPayment userId paymentId name cost frequency payments))
+ else
+ Task.succeed U.NoOp
+ )
+ )
DeletePayment id userId cost currentPage ->
Http.send Http.defaultSettings (updatePageRequest currentPage)
- |> Task.map (\response ->
+ |> flip Task.andThen (\response ->
if response.status == 200
then
decodeResponse
response
paymentsDecoder
- (\payments -> U.UpdatePayment (UP.Remove userId cost payments))
+ (\payments -> Task.succeed <| U.UpdateLoggedView (UL.Remove userId cost payments))
else
- U.NoOp
+ Task.succeed U.NoOp
)
UpdatePage page ->
decodeResponse
response
paymentsDecoder
- (\payments -> U.UpdatePayment (UP.UpdatePage page payments))
- |> Task.succeed
+ (\payments -> Task.succeed <| U.UpdateLoggedView (UL.UpdatePage page payments))
SignOut ->
Task.succeed (U.GoSignInView)
else
@@ -115,13 +119,12 @@ serverResult communication response =
(\error ->
case communication of
SignIn _ ->
- U.UpdateSignIn (ErrorLogin error)
+ Task.succeed <| U.UpdateSignIn (ErrorLogin error)
_ ->
- U.NoOp
+ Task.succeed <| U.NoOp
)
- |> Task.succeed
-decodeResponse : Http.Response -> Decoder a -> (a -> U.Action) -> U.Action
+decodeResponse : Http.Response -> Decoder a -> (a -> Task b U.Action) -> Task b U.Action
decodeResponse response decoder responseToAction =
case response.value of
Http.Text text ->
@@ -129,6 +132,6 @@ decodeResponse response decoder responseToAction =
Ok x ->
responseToAction x
Err _ ->
- U.NoOp
+ Task.succeed U.NoOp
Http.Blob _ ->
- U.NoOp
+ Task.succeed U.NoOp