module ServerCommunication ( sendRequest ) where import Signal import Task as Task exposing (Task) import Http import Json.Decode exposing (..) import Date import Time exposing (Time) import Debug import SimpleHTTP exposing (..) import Model.Communication exposing (..) import Model.Action as U import Model.Action.LoggedInAction as UL import Model.Action.MonthlyAction as UM import Model.Action.AccountAction as UA import Model.View.LoggedIn.AddPayment exposing (Frequency(..)) import Model.Payment exposing (..) import Update.SignIn exposing (updateSignIn) import InitViewAction exposing (initViewAction) sendRequest : Communication -> Task Http.Error U.Action sendRequest communication = case communication of NoCommunication -> Task.succeed U.NoOp SignIn assertion -> post ("/signIn?assertion=" ++ assertion) |> flip Task.andThen (always initViewAction) AddPayment name cost -> post (addPaymentURL name cost Punctual) |> flip Task.andThen (decodeHttpValue <| "id" := paymentIdDecoder) |> Task.map (\paymentId -> (U.UpdateLoggedIn (UL.AddPayment paymentId name cost))) AddMonthlyPayment name cost -> post (addPaymentURL name cost Monthly) |> flip Task.andThen (decodeHttpValue <| "id" := paymentIdDecoder) |> Task.map (\id -> U.UpdateLoggedIn (UL.AddMonthlyPayment id name cost)) DeletePayment payment currentPage -> post (deletePaymentURL payment.id) |> Task.map (always (U.UpdateLoggedIn (UL.DeletePayment payment))) DeleteMonthlyPayment id -> post (deletePaymentURL id) |> Task.map (always (U.UpdateLoggedIn (UL.UpdateMonthly (UM.DeletePayment id)))) SetIncome currentTime amount -> post ("/income?amount=" ++ (toString amount)) |> Task.map (always (U.UpdateLoggedIn (UL.UpdateAccount (UA.UpdateIncome currentTime amount)))) SignOut -> post "/signOut" |> Task.map (always U.GoSignInView) addPaymentURL : String -> Int -> Frequency -> String addPaymentURL name cost frequency = "/payment/add?name=" ++ name ++ "&cost=" ++ (toString cost) ++ "&frequency=" ++ (toString frequency) deletePaymentURL : PaymentId -> String deletePaymentURL id = "payment/delete?id=" ++ (toString id)