From c1f44a5890fbb26faf6f17c676662ea1bd495f2e Mon Sep 17 00:00:00 2001 From: Joris Date: Fri, 4 Sep 2015 09:52:20 +0200 Subject: Adding paging (need some fixes) --- src/client/ServerCommunication.elm | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) (limited to 'src/client/ServerCommunication.elm') diff --git a/src/client/ServerCommunication.elm b/src/client/ServerCommunication.elm index 9bf2008..9359160 100644 --- a/src/client/ServerCommunication.elm +++ b/src/client/ServerCommunication.elm @@ -11,7 +11,7 @@ import Json.Decode exposing (..) import Date import Model.Message exposing (messageDecoder) -import Model.Payment exposing (PaymentId) +import Model.Payment exposing (PaymentId, perPage, paymentsDecoder) import Update as U import Update.SignIn exposing (..) @@ -22,6 +22,7 @@ type Communication = | SignIn String | AddPayment String Int | DeletePayment PaymentId + | UpdatePage Int | SignOut serverCommunications : Signal.Mailbox Communication @@ -42,17 +43,19 @@ getRequest communication = NoCommunication -> Nothing SignIn login -> - Just (simplePost ("/signIn?login=" ++ login)) + Just (simple "post" ("/signIn?login=" ++ login)) AddPayment name cost -> - Just (simplePost ("/payment/add?name=" ++ name ++ "&cost=" ++ (toString cost))) + Just (simple "post" ("/payment/add?name=" ++ name ++ "&cost=" ++ (toString cost))) DeletePayment paymentId -> - Just (simplePost ("payment/delete?id=" ++ paymentId)) + Just (simple "post" ("payment/delete?id=" ++ paymentId)) + UpdatePage page -> + Just (simple "get" ("payments?page=" ++ toString page ++ "&perPage=" ++ toString perPage)) SignOut -> - Just (simplePost "/signOut") + Just (simple "post" "/signOut") -simplePost : String -> Http.Request -simplePost url = - { verb = "post" +simple : String -> String -> Http.Request +simple method url = + { verb = method , headers = [] , url = url , body = Http.empty @@ -70,14 +73,21 @@ communicationToAction communication response = AddPayment name cost -> decodeResponse response + messageDecoder (\id -> U.UpdatePayment (UP.AddPayment id name cost)) DeletePayment id -> U.UpdatePayment (UP.Remove id) + UpdatePage page -> + decodeResponse + response + paymentsDecoder + (\payments -> U.UpdatePayment (UP.UpdatePage page payments)) SignOut -> U.GoSignInView else decodeResponse response + messageDecoder (\error -> case communication of SignIn _ -> @@ -86,11 +96,11 @@ communicationToAction communication response = U.NoOp ) -decodeResponse : Http.Response -> (String -> U.Action) -> U.Action -decodeResponse response responseToAction = +decodeResponse : Http.Response -> Decoder a -> (a -> U.Action) -> U.Action +decodeResponse response decoder responseToAction = case response.value of Http.Text text -> - case decodeString messageDecoder text of + case decodeString decoder text of Ok x -> responseToAction x Err _ -> -- cgit v1.2.3