From 6b090b3bdef7108d51d93207e28b148c121767aa Mon Sep 17 00:00:00 2001 From: Joris Date: Mon, 4 Jan 2016 00:34:48 +0100 Subject: Simplify server communicaitons in client --- src/client/elm/Server.elm | 62 +++++++++++++++++------------------------------ 1 file changed, 22 insertions(+), 40 deletions(-) (limited to 'src/client/elm/Server.elm') diff --git a/src/client/elm/Server.elm b/src/client/elm/Server.elm index e50de7e..7b03a98 100644 --- a/src/client/elm/Server.elm +++ b/src/client/elm/Server.elm @@ -1,10 +1,10 @@ module Server - ( signIn + ( init + , signIn , addPayment , deletePayment , setIncome , signOut - , initViewAction ) where import Signal @@ -13,62 +13,44 @@ import Http import Json.Decode as Json exposing ((:=)) import Date import Time exposing (Time) -import Debug -import String -import SimpleHTTP exposing (..) +import Utils.Http exposing (..) -import Model.Action as U exposing (Action) -import Model.Action.AddPaymentAction as AddPayment -import Model.Action.LoggedInAction as UL exposing (LoggedInAction) -import Model.Action.MonthlyAction as UM exposing (MonthlyAction) -import Model.Action.AccountAction as UA exposing (AccountAction) import Model.Payment exposing (..) import Model.Payer exposing (Payers, payersDecoder) import Model.User exposing (Users, usersDecoder, UserId, userIdDecoder) -import Model.Translations exposing (Translations, getMessage) +import Model.Init exposing (Init) -import Update.SignIn exposing (updateSignIn) +init : Task Http.Error Init +init = + Task.map Init (Http.get usersDecoder "/users") + `Task.andMap` (Http.get ("id" := userIdDecoder) "/whoAmI") + `Task.andMap` (Http.get paymentsDecoder "/payments") + `Task.andMap` (Http.get paymentsDecoder "/monthlyPayments") + `Task.andMap` (Http.get ("number" := Json.int) "/payments/count") + `Task.andMap` (Http.get payersDecoder "/payers") -signIn : String -> Task Http.Error Action +signIn : String -> Task Http.Error Init signIn assertion = post ("/signIn?assertion=" ++ assertion) - |> flip Task.andThen (always initViewAction) + |> flip Task.andThen (always init) -addPayment : Translations -> String -> String -> PaymentFrequency -> Task Http.Error LoggedInAction -addPayment translations name cost frequency = +addPayment : String -> String -> PaymentFrequency -> Task Http.Error PaymentId +addPayment name cost frequency = post ("/payment/add?name=" ++ name ++ "&cost=" ++ cost ++ "&frequency=" ++ (toString frequency)) |> flip Task.andThen (decodeHttpValue <| "id" := paymentIdDecoder) - |> Task.map (\paymentId -> - case String.toInt cost of - Err _ -> - UL.UpdateAdd (AddPayment.AddError Nothing (Just (getMessage "CostRequired" translations))) - Ok costNumber -> - UL.ValidateAddPayment paymentId name costNumber frequency - ) -deletePayment : Payment -> PaymentFrequency -> Task Http.Error LoggedInAction +deletePayment : Payment -> PaymentFrequency -> Task Http.Error () deletePayment payment frequency = post ("payment/delete?id=" ++ (toString payment.id)) - |> Task.map (always (UL.ValidateDeletePayment payment frequency)) + |> Task.map (always ()) -setIncome : Time -> Int -> Task Http.Error AccountAction +setIncome : Time -> Int -> Task Http.Error () setIncome currentTime amount = post ("/income?amount=" ++ (toString amount)) - |> Task.map (always (UA.ValidateUpdateIncome currentTime amount)) + |> Task.map (always ()) -signOut : Task Http.Error Action +signOut : Task Http.Error () signOut = post "/signOut" - |> Task.map (always U.GoSignInView) - -initViewAction = Task.onError loggedInView (always <| Task.succeed U.GoSignInView) - -loggedInView : Task Http.Error Action -loggedInView = - Task.map U.GoLoggedInView (Http.get usersDecoder "/users") - `Task.andMap` (Http.get ("id" := userIdDecoder) "/whoAmI") - `Task.andMap` (Http.get paymentsDecoder "/monthlyPayments") - `Task.andMap` (Http.get paymentsDecoder "/payments") - `Task.andMap` (Http.get ("number" := Json.int) "/payments/count") - `Task.andMap` (Http.get payersDecoder "/payers") + |> Task.map (always ()) -- cgit v1.2.3