From a2527c34e6b0e719a948cfd36bfee5ffad095a30 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 23 Aug 2015 10:54:45 +0200 Subject: UI payment delete, not done on server side yet --- src/client/Model/Payment.elm | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'src/client/Model') diff --git a/src/client/Model/Payment.elm b/src/client/Model/Payment.elm index fa59943..e773be3 100644 --- a/src/client/Model/Payment.elm +++ b/src/client/Model/Payment.elm @@ -2,17 +2,22 @@ module Model.Payment ( Payments , Payment , PaymentId + , PaymentWithId , paymentsDecoder + , addPayment + , removePayment ) where import Date exposing (..) import Json.Decode as Json exposing ((:=)) +import Dict exposing (..) -type alias Payments = List Payment +type alias Payments = Dict PaymentId Payment + +type alias PaymentWithId = (PaymentId, Payment) type alias Payment = - { id : PaymentId - , creation : Date + { creation : Date , name : String , cost : Int , userName : String @@ -21,12 +26,15 @@ type alias Payment = type alias PaymentId = String paymentsDecoder : Json.Decoder Payments -paymentsDecoder = Json.list paymentDecoder +paymentsDecoder = Json.map Dict.fromList (Json.list paymentWithIdDecoder) + +paymentWithIdDecoder : Json.Decoder (PaymentId, Payment) +paymentWithIdDecoder = + paymentDecoder `Json.andThen` (\payment -> Json.map (\id -> (id, payment)) ("id" := Json.string)) paymentDecoder : Json.Decoder Payment paymentDecoder = - Json.object5 Payment - ("id" := paymentIdDecoder) + Json.object4 Payment ("creation" := dateDecoder) ("name" := Json.string) ("cost" := Json.int) @@ -37,3 +45,9 @@ paymentIdDecoder = Json.string dateDecoder : Json.Decoder Date dateDecoder = Json.customDecoder Json.string Date.fromString + +addPayment : Payments -> (PaymentId, Payment) -> Payments +addPayment payments (paymentId, payment) = Dict.insert paymentId payment payments + +removePayment : Payments -> PaymentId -> Payments +removePayment payments paymentId = Dict.remove paymentId payments -- cgit v1.2.3