From 0c9d2b91e73f045067f7bcce6e4235fc9008f309 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 27 Mar 2016 21:13:37 +0200 Subject: Regroup add payment modules --- src/client/elm/LoggedIn/Action.elm | 4 +- src/client/elm/LoggedIn/AddPayment/Action.elm | 11 ++ src/client/elm/LoggedIn/AddPayment/Model.elm | 29 +++++ src/client/elm/LoggedIn/AddPayment/Update.elm | 55 ++++++++++ src/client/elm/LoggedIn/AddPayment/View.elm | 127 ++++++++++++++++++++++ src/client/elm/LoggedIn/Model.elm | 6 +- src/client/elm/LoggedIn/Update.elm | 17 +-- src/client/elm/LoggedIn/View.elm | 5 +- src/client/elm/Model/Action/AddPaymentAction.elm | 11 -- src/client/elm/Model/View/LoggedIn/AddPayment.elm | 29 ----- src/client/elm/Update/LoggedIn/AddPayment.elm | 54 --------- src/client/elm/View/LoggedIn/AddPayment.elm | 127 ---------------------- 12 files changed, 238 insertions(+), 237 deletions(-) create mode 100644 src/client/elm/LoggedIn/AddPayment/Action.elm create mode 100644 src/client/elm/LoggedIn/AddPayment/Model.elm create mode 100644 src/client/elm/LoggedIn/AddPayment/Update.elm create mode 100644 src/client/elm/LoggedIn/AddPayment/View.elm delete mode 100644 src/client/elm/Model/Action/AddPaymentAction.elm delete mode 100644 src/client/elm/Model/View/LoggedIn/AddPayment.elm delete mode 100644 src/client/elm/Update/LoggedIn/AddPayment.elm delete mode 100644 src/client/elm/View/LoggedIn/AddPayment.elm (limited to 'src/client') diff --git a/src/client/elm/LoggedIn/Action.elm b/src/client/elm/LoggedIn/Action.elm index bd224cd..32c7e0a 100644 --- a/src/client/elm/LoggedIn/Action.elm +++ b/src/client/elm/LoggedIn/Action.elm @@ -4,13 +4,13 @@ module LoggedIn.Action import Model.Payment exposing (Payments, Payment, PaymentId, PaymentFrequency) import Model.Action.MonthlyAction exposing (MonthlyAction) -import Model.Action.AddPaymentAction exposing (AddPaymentAction) import LoggedIn.Account.Action as AccountAction +import LoggedIn.AddPayment.Action as AddPaymentAction type Action = NoOp - | UpdateAdd AddPaymentAction + | UpdateAdd AddPaymentAction.Action | UpdatePayments Payments | AddPayment String String PaymentFrequency | ValidateAddPayment PaymentId String Int PaymentFrequency diff --git a/src/client/elm/LoggedIn/AddPayment/Action.elm b/src/client/elm/LoggedIn/AddPayment/Action.elm new file mode 100644 index 0000000..41d4f5b --- /dev/null +++ b/src/client/elm/LoggedIn/AddPayment/Action.elm @@ -0,0 +1,11 @@ +module LoggedIn.AddPayment.Action + ( Action(..) + ) where + +type Action = + NoOp + | UpdateName String + | UpdateCost String + | AddError (Maybe String) (Maybe String) + | ToggleFrequency + | WaitingServer diff --git a/src/client/elm/LoggedIn/AddPayment/Model.elm b/src/client/elm/LoggedIn/AddPayment/Model.elm new file mode 100644 index 0000000..2aa32c2 --- /dev/null +++ b/src/client/elm/LoggedIn/AddPayment/Model.elm @@ -0,0 +1,29 @@ +module LoggedIn.AddPayment.Model + ( Model + , init + ) where + +import Result as Result exposing (Result(..)) +import Json.Decode exposing ((:=)) + +import Model.Translations exposing (..) +import Model.Payment exposing (PaymentFrequency(..)) + +type alias Model = + { name : String + , nameError : Maybe String + , cost : String + , costError : Maybe String + , frequency : PaymentFrequency + , waitingServer : Bool + } + +init : PaymentFrequency -> Model +init frequency = + { name = "" + , nameError = Nothing + , cost = "" + , costError = Nothing + , frequency = frequency + , waitingServer = False + } diff --git a/src/client/elm/LoggedIn/AddPayment/Update.elm b/src/client/elm/LoggedIn/AddPayment/Update.elm new file mode 100644 index 0000000..eb4384b --- /dev/null +++ b/src/client/elm/LoggedIn/AddPayment/Update.elm @@ -0,0 +1,55 @@ +module LoggedIn.AddPayment.Update + ( update + , addPaymentError + ) where + +import Maybe +import Json.Decode as Json exposing ((:=)) + +import LoggedIn.AddPayment.Action as AddPaymentAction +import LoggedIn.AddPayment.Model as AddPaymentModel + +import Model.Translations exposing (Translations, getMessage) +import Model.Payment exposing (PaymentFrequency(..)) + +update : AddPaymentAction.Action -> AddPaymentModel.Model -> AddPaymentModel.Model +update action addPayment = + case action of + + AddPaymentAction.NoOp -> + addPayment + + AddPaymentAction.UpdateName name -> + { addPayment | name = name } + + AddPaymentAction.UpdateCost cost -> + { addPayment | cost = cost } + + AddPaymentAction.AddError nameError costError -> + { addPayment + | nameError = nameError + , costError = costError + , waitingServer = False + } + + AddPaymentAction.ToggleFrequency -> + { addPayment + | frequency = if addPayment.frequency == Punctual then Monthly else Punctual + } + + AddPaymentAction.WaitingServer -> + { addPayment | waitingServer = True } + +addPaymentError : Translations -> String -> Maybe AddPaymentAction.Action +addPaymentError translations jsonErr = + let decoder = + Json.object2 (,) + (Json.maybe <| "name" := Json.string) + (Json.maybe <| "cost" := Json.string) + in case Json.decodeString decoder jsonErr of + Err _ -> + Nothing + Ok (mbNameKey, mbCostKey) -> + Just <| AddPaymentAction.AddError + (Maybe.map (flip getMessage translations) mbNameKey) + (Maybe.map (flip getMessage translations) mbCostKey) diff --git a/src/client/elm/LoggedIn/AddPayment/View.elm b/src/client/elm/LoggedIn/AddPayment/View.elm new file mode 100644 index 0000000..50f7708 --- /dev/null +++ b/src/client/elm/LoggedIn/AddPayment/View.elm @@ -0,0 +1,127 @@ +module LoggedIn.AddPayment.View + ( view + ) where + +import Result exposing (..) +import Signal exposing (Address) + +import Html as H exposing (..) +import Html.Attributes exposing (..) +import Html.Events exposing (..) + +import LoggedIn.Action as LoggedInAction +import LoggedIn.Model as LoggedInModel + +import LoggedIn.AddPayment.Action as AddPaymentAction +import LoggedIn.AddPayment.Model as AddPaymentModel + +import Model exposing (Model) +import Model.Payment exposing (PaymentFrequency(..)) +import Model.Translations exposing (getMessage) +import Model.Action as Action exposing (..) + +import View.Events exposing (onSubmitPrevDefault) +import View.Icon exposing (..) + +import Utils.Maybe exposing (isJust) +import Utils.Either exposing (toMaybeError) + +view : Address Action -> Model -> LoggedInModel.Model -> Html +view address model loggedInModel = + H.form + [ let update = + if loggedInModel.add.waitingServer + then + Action.NoOp + else + UpdateLoggedIn <| LoggedInAction.AddPayment loggedInModel.add.name loggedInModel.add.cost loggedInModel.add.frequency + in onSubmitPrevDefault address update + , class "addPayment" + ] + [ addPaymentName address loggedInModel.add + , addPaymentCost address model loggedInModel.add + , paymentFrequency address model loggedInModel.add + , button + [ type' "submit" + , classList + [ ("add", True) + , ("waitingServer", loggedInModel.add.waitingServer) + ] + ] + [ text (getMessage "Add" model.translations) + , if loggedInModel.add.waitingServer then renderSpinIcon else text "" + ] + ] + +addPaymentName : Address Action -> AddPaymentModel.Model -> Html +addPaymentName address addPayment = + div + [ classList + [ ("name", True) + , ("error", isJust addPayment.nameError) + ] + ] + [ input + [ id "nameInput" + , value addPayment.name + , on "input" targetValue (Signal.message address << UpdateLoggedIn << LoggedInAction.UpdateAdd << AddPaymentAction.UpdateName) + , maxlength 20 + ] + [] + , label + [ for "nameInput" ] + [ renderIcon "shopping-cart" ] + , case addPayment.nameError of + Just error -> + div [ class "errorMessage" ] [ text error ] + Nothing -> + text "" + ] + +addPaymentCost : Address Action -> Model -> AddPaymentModel.Model -> Html +addPaymentCost address model addPayment = + div + [ classList + [ ("cost", True) + , ("error", isJust addPayment.costError) + ] + ] + [ input + [ id "costInput" + , value addPayment.cost + , on "input" targetValue (Signal.message address << UpdateLoggedIn << LoggedInAction.UpdateAdd << AddPaymentAction.UpdateCost) + , maxlength 7 + ] + [] + , label + [ for "costInput" ] + [ text model.conf.currency ] + , case addPayment.costError of + Just error -> + div [ class "errorMessage" ] [ text error ] + Nothing -> + text "" + ] + +paymentFrequency : Address Action -> Model -> AddPaymentModel.Model -> Html +paymentFrequency address model addPayment = + button + [ type' "button" + , class "frequency" + , onClick address (UpdateLoggedIn << LoggedInAction.UpdateAdd <| AddPaymentAction.ToggleFrequency) + ] + [ div + [ classList + [ ("punctual", True) + , ("selected", addPayment.frequency == Punctual) + ] + ] + [ text (getMessage "Punctual" model.translations) ] + , div + [ classList + [ ("monthly", True) + , ("selected", addPayment.frequency == Monthly) + ] + ] + [ text (getMessage "Monthly" model.translations) ] + ] diff --git a/src/client/elm/LoggedIn/Model.elm b/src/client/elm/LoggedIn/Model.elm index 0f677c1..c1c573f 100644 --- a/src/client/elm/LoggedIn/Model.elm +++ b/src/client/elm/LoggedIn/Model.elm @@ -8,15 +8,15 @@ import Model.Payment exposing (Payments, PaymentFrequency(..)) import Model.Payer exposing (Payers) import Model.Init exposing (..) -import Model.View.LoggedIn.AddPayment exposing (..) import Model.View.LoggedIn.Edition exposing (..) import Model.View.LoggedIn.Monthly exposing (..) import LoggedIn.Account.Model as AccountModel +import LoggedIn.AddPayment.Model as AddPaymentModel type alias Model = { users : Users - , add : AddPayment + , add : AddPaymentModel.Model , monthly : Monthly , account : AccountModel.Model , payments : Payments @@ -28,7 +28,7 @@ type alias Model = init : Init -> Model init initData = { users = initData.users - , add = initAddPayment Punctual + , add = AddPaymentModel.init Punctual , monthly = initMonthly initData.monthlyPayments , account = AccountModel.init initData.me initData.incomes , payments = initData.payments diff --git a/src/client/elm/LoggedIn/Update.elm b/src/client/elm/LoggedIn/Update.elm index e017423..35ffaff 100644 --- a/src/client/elm/LoggedIn/Update.elm +++ b/src/client/elm/LoggedIn/Update.elm @@ -19,15 +19,16 @@ import LoggedIn.Model as LoggedInModel import LoggedIn.Account.Action as AccountAction import LoggedIn.Account.Update as AccountUpdate +import LoggedIn.AddPayment.Action as AddPaymentAction +import LoggedIn.AddPayment.Model as AddPaymentModel +import LoggedIn.AddPayment.Update as AddPaymentUpdate + import Model exposing (Model) import Model.User exposing (UserId) import Model.Payment exposing (..) import Model.Action.MonthlyAction as Monthly -import Model.Action.AddPaymentAction as AddPayment -import Model.View.LoggedIn.AddPayment exposing (..) import Model.Translations exposing (Translations, getMessage) -import Update.LoggedIn.AddPayment exposing (updateAddPayment, addPaymentError) import Update.LoggedIn.Monthly exposing (updateMonthly) update : Model -> LoggedInAction.Action -> LoggedInModel.Model -> (LoggedInModel.Model, Effects LoggedInAction.Action) @@ -37,7 +38,7 @@ update model action loggedInView = LoggedInAction.NoOp -> (loggedInView, Effects.none) LoggedInAction.UpdateAdd addPaymentAction -> - ( { loggedInView | add = updateAddPayment addPaymentAction loggedInView.add } + ( { loggedInView | add = AddPaymentUpdate.update addPaymentAction loggedInView.add } , Effects.none ) @@ -47,19 +48,19 @@ update model action loggedInView = ) LoggedInAction.AddPayment name cost frequency -> - ( { loggedInView | add = updateAddPayment AddPayment.WaitingServer loggedInView.add } + ( { loggedInView | add = AddPaymentUpdate.update AddPaymentAction.WaitingServer loggedInView.add } , Server.addPayment name cost frequency |> Task.map (\paymentId -> case String.toInt cost of Err _ -> - LoggedInAction.UpdateAdd (AddPayment.AddError Nothing (Just (getMessage "CostRequired" model.translations))) + LoggedInAction.UpdateAdd (AddPaymentAction.AddError Nothing (Just (getMessage "CostRequired" model.translations))) Ok costNumber -> LoggedInAction.ValidateAddPayment paymentId name costNumber frequency ) |> flip Task.onError (\err -> case err of BadResponse 400 jsonErr -> - case addPaymentError model.translations jsonErr of + case AddPaymentUpdate.addPaymentError model.translations jsonErr of Just addPaymentAction -> Task.succeed (LoggedInAction.UpdateAdd addPaymentAction) Nothing -> Task.succeed LoggedInAction.NoOp _ -> @@ -70,7 +71,7 @@ update model action loggedInView = LoggedInAction.ValidateAddPayment paymentId name cost frequency -> let newPayment = Payment paymentId (Date.fromTime model.currentTime) name cost loggedInView.account.me - newAdd = initAddPayment frequency + newAdd = AddPaymentModel.init frequency in case frequency of Punctual -> ( { loggedInView diff --git a/src/client/elm/LoggedIn/View.elm b/src/client/elm/LoggedIn/View.elm index 8817dd2..8561d2c 100644 --- a/src/client/elm/LoggedIn/View.elm +++ b/src/client/elm/LoggedIn/View.elm @@ -8,14 +8,13 @@ import Html exposing (..) import Html.Attributes exposing (..) import LoggedIn.Model as LoggedInModel - import LoggedIn.Account.View as AccountView +import LoggedIn.AddPayment.View as AddPaymentView import Model exposing (Model) import Model.Payment exposing (Payments) import Model.Action exposing (Action) -import View.LoggedIn.AddPayment exposing (addPayment) import View.LoggedIn.Monthly exposing (monthlyPayments) import View.LoggedIn.Table exposing (paymentsTable) import View.LoggedIn.Paging exposing (paymentsPaging) @@ -24,7 +23,7 @@ view : Address Action -> Model -> LoggedInModel.Model -> Html view address model loggedInModel = div [ class "loggedIn" ] - [ addPayment address model loggedInModel + [ AddPaymentView.view address model loggedInModel , div [ class "expandables" ] [ AccountView.view address model loggedInModel diff --git a/src/client/elm/Model/Action/AddPaymentAction.elm b/src/client/elm/Model/Action/AddPaymentAction.elm deleted file mode 100644 index 2d4f92a..0000000 --- a/src/client/elm/Model/Action/AddPaymentAction.elm +++ /dev/null @@ -1,11 +0,0 @@ -module Model.Action.AddPaymentAction - ( AddPaymentAction(..) - ) where - -type AddPaymentAction = - NoOp - | UpdateName String - | UpdateCost String - | AddError (Maybe String) (Maybe String) - | ToggleFrequency - | WaitingServer diff --git a/src/client/elm/Model/View/LoggedIn/AddPayment.elm b/src/client/elm/Model/View/LoggedIn/AddPayment.elm deleted file mode 100644 index c7680bb..0000000 --- a/src/client/elm/Model/View/LoggedIn/AddPayment.elm +++ /dev/null @@ -1,29 +0,0 @@ -module Model.View.LoggedIn.AddPayment - ( AddPayment - , initAddPayment - ) where - -import Result as Result exposing (Result(..)) -import Json.Decode exposing ((:=)) - -import Model.Translations exposing (..) -import Model.Payment exposing (PaymentFrequency(..)) - -type alias AddPayment = - { name : String - , nameError : Maybe String - , cost : String - , costError : Maybe String - , frequency : PaymentFrequency - , waitingServer : Bool - } - -initAddPayment : PaymentFrequency -> AddPayment -initAddPayment frequency = - { name = "" - , nameError = Nothing - , cost = "" - , costError = Nothing - , frequency = frequency - , waitingServer = False - } diff --git a/src/client/elm/Update/LoggedIn/AddPayment.elm b/src/client/elm/Update/LoggedIn/AddPayment.elm deleted file mode 100644 index 4c9c484..0000000 --- a/src/client/elm/Update/LoggedIn/AddPayment.elm +++ /dev/null @@ -1,54 +0,0 @@ -module Update.LoggedIn.AddPayment - ( updateAddPayment - , addPaymentError - ) where - -import Maybe -import Json.Decode as Json exposing ((:=)) - -import Model.Action.AddPaymentAction exposing (..) -import Model.View.LoggedIn.AddPayment exposing (..) -import Model.Translations exposing (Translations, getMessage) -import Model.Payment exposing (PaymentFrequency(..)) - -updateAddPayment : AddPaymentAction -> AddPayment -> AddPayment -updateAddPayment action addPayment = - case action of - - NoOp -> - addPayment - - UpdateName name -> - { addPayment | name = name } - - UpdateCost cost -> - { addPayment | cost = cost } - - AddError nameError costError -> - { addPayment - | nameError = nameError - , costError = costError - , waitingServer = False - } - - ToggleFrequency -> - { addPayment - | frequency = if addPayment.frequency == Punctual then Monthly else Punctual - } - - WaitingServer -> - { addPayment | waitingServer = True } - -addPaymentError : Translations -> String -> Maybe AddPaymentAction -addPaymentError translations jsonErr = - let decoder = - Json.object2 (,) - (Json.maybe <| "name" := Json.string) - (Json.maybe <| "cost" := Json.string) - in case Json.decodeString decoder jsonErr of - Err _ -> - Nothing - Ok (mbNameKey, mbCostKey) -> - Just <| AddError - (Maybe.map (flip getMessage translations) mbNameKey) - (Maybe.map (flip getMessage translations) mbCostKey) diff --git a/src/client/elm/View/LoggedIn/AddPayment.elm b/src/client/elm/View/LoggedIn/AddPayment.elm deleted file mode 100644 index 010ecd3..0000000 --- a/src/client/elm/View/LoggedIn/AddPayment.elm +++ /dev/null @@ -1,127 +0,0 @@ -module View.LoggedIn.AddPayment - ( addPayment - ) where - -import Result exposing (..) -import Signal exposing (Address) - -import Html as H exposing (..) -import Html.Attributes exposing (..) -import Html.Events exposing (..) - -import LoggedIn.Action as LoggedInAction -import LoggedIn.Model as LoggedInModel - -import Model exposing (Model) -import Model.Payment exposing (PaymentFrequency(..)) -import Model.Translations exposing (getMessage) -import Model.Action as Action exposing (..) -import Model.Action.AddPaymentAction exposing (..) - -import Model.View.LoggedIn.AddPayment exposing (..) - -import View.Events exposing (onSubmitPrevDefault) -import View.Icon exposing (..) - -import Utils.Maybe exposing (isJust) -import Utils.Either exposing (toMaybeError) - -addPayment : Address Action -> Model -> LoggedInModel.Model -> Html -addPayment address model loggedInModel = - H.form - [ let update = - if loggedInModel.add.waitingServer - then - Action.NoOp - else - UpdateLoggedIn <| LoggedInAction.AddPayment loggedInModel.add.name loggedInModel.add.cost loggedInModel.add.frequency - in onSubmitPrevDefault address update - , class "addPayment" - ] - [ addPaymentName address loggedInModel.add - , addPaymentCost address model loggedInModel.add - , paymentFrequency address model loggedInModel.add - , button - [ type' "submit" - , classList - [ ("add", True) - , ("waitingServer", loggedInModel.add.waitingServer) - ] - ] - [ text (getMessage "Add" model.translations) - , if loggedInModel.add.waitingServer then renderSpinIcon else text "" - ] - ] - -addPaymentName : Address Action -> AddPayment -> Html -addPaymentName address addPayment = - div - [ classList - [ ("name", True) - , ("error", isJust addPayment.nameError) - ] - ] - [ input - [ id "nameInput" - , value addPayment.name - , on "input" targetValue (Signal.message address << UpdateLoggedIn << LoggedInAction.UpdateAdd << UpdateName) - , maxlength 20 - ] - [] - , label - [ for "nameInput" ] - [ renderIcon "shopping-cart" ] - , case addPayment.nameError of - Just error -> - div [ class "errorMessage" ] [ text error ] - Nothing -> - text "" - ] - -addPaymentCost : Address Action -> Model -> AddPayment -> Html -addPaymentCost address model addPayment = - div - [ classList - [ ("cost", True) - , ("error", isJust addPayment.costError) - ] - ] - [ input - [ id "costInput" - , value addPayment.cost - , on "input" targetValue (Signal.message address << UpdateLoggedIn << LoggedInAction.UpdateAdd << UpdateCost) - , maxlength 7 - ] - [] - , label - [ for "costInput" ] - [ text model.conf.currency ] - , case addPayment.costError of - Just error -> - div [ class "errorMessage" ] [ text error ] - Nothing -> - text "" - ] - -paymentFrequency : Address Action -> Model -> AddPayment -> Html -paymentFrequency address model addPayment = - button - [ type' "button" - , class "frequency" - , onClick address (UpdateLoggedIn << LoggedInAction.UpdateAdd <| ToggleFrequency) - ] - [ div - [ classList - [ ("punctual", True) - , ("selected", addPayment.frequency == Punctual) - ] - ] - [ text (getMessage "Punctual" model.translations) ] - , div - [ classList - [ ("monthly", True) - , ("selected", addPayment.frequency == Monthly) - ] - ] - [ text (getMessage "Monthly" model.translations) ] - ] -- cgit v1.2.3