From 76f8b85eb9f796d6df861a04f702ef5f48630795 Mon Sep 17 00:00:00 2001 From: Joris Date: Tue, 29 Mar 2016 23:46:47 +0200 Subject: Move logged data to LoggedIn component --- src/client/elm/LoggedIn/Home/Account/Action.elm | 3 - src/client/elm/LoggedIn/Home/Account/Model.elm | 20 +++---- src/client/elm/LoggedIn/Home/Account/Update.elm | 50 ++++------------- src/client/elm/LoggedIn/Home/Account/View.elm | 75 +++++++++++++------------ 4 files changed, 59 insertions(+), 89 deletions(-) (limited to 'src/client/elm/LoggedIn/Home/Account') diff --git a/src/client/elm/LoggedIn/Home/Account/Action.elm b/src/client/elm/LoggedIn/Home/Account/Action.elm index 61dae42..4ce3b20 100644 --- a/src/client/elm/LoggedIn/Home/Account/Action.elm +++ b/src/client/elm/LoggedIn/Home/Account/Action.elm @@ -5,7 +5,6 @@ module LoggedIn.Home.Account.Action import Time exposing (Time) import Model.User exposing (UserId) -import Model.Income exposing (IncomeId) type Action = NoOp @@ -13,5 +12,3 @@ type Action = | ToggleIncomeEdition | UpdateIncomeEdition String | UpdateEditionError String - | UpdateIncome Time Int - | ValidateUpdateIncome IncomeId Time Int diff --git a/src/client/elm/LoggedIn/Home/Account/Model.elm b/src/client/elm/LoggedIn/Home/Account/Model.elm index d8bf748..d04f865 100644 --- a/src/client/elm/LoggedIn/Home/Account/Model.elm +++ b/src/client/elm/LoggedIn/Home/Account/Model.elm @@ -18,24 +18,20 @@ import Model.Income exposing (..) import Model.User exposing (UserId) type alias Model = - { me : UserId - , incomes : Incomes - , visibleDetail : Bool + { visibleDetail : Bool , incomeEdition : Maybe IncomeEdition } -init : UserId -> Incomes -> Model -init me incomes = - { me = me - , incomes = incomes - , visibleDetail = False +init : Model +init = + { visibleDetail = False , incomeEdition = Nothing } -getCurrentIncome : Model -> Maybe Int -getCurrentIncome account = - account.incomes - |> Dict.filter (\_ income -> income.userId == account.me) +getCurrentIncome : Incomes -> UserId -> Model -> Maybe Int +getCurrentIncome incomes me account = + incomes + |> Dict.filter (\_ income -> income.userId == me) |> Dict.values |> List.sortBy .creation |> List.reverse diff --git a/src/client/elm/LoggedIn/Home/Account/Update.elm b/src/client/elm/LoggedIn/Home/Account/Update.elm index 8d002a3..59f1402 100644 --- a/src/client/elm/LoggedIn/Home/Account/Update.elm +++ b/src/client/elm/LoggedIn/Home/Account/Update.elm @@ -3,20 +3,18 @@ module LoggedIn.Home.Account.Update ) where import Maybe -import Dict -import Task import Effects exposing (Effects) -import Server +import LoggedData exposing (LoggedData) import LoggedIn.Home.Account.Action as AccountAction import LoggedIn.Home.Account.Model as AccountModel import Utils.Maybe exposing (isJust) -update : AccountAction.Action -> AccountModel.Model -> (AccountModel.Model, Effects AccountAction.Action) -update action account = +update : LoggedData -> AccountAction.Action -> AccountModel.Model -> (AccountModel.Model, Effects AccountAction.Action) +update loggedData action account = case action of AccountAction.NoOp -> @@ -31,45 +29,21 @@ update action account = ( { account | incomeEdition = if isJust account.incomeEdition then Nothing - else Just (AccountModel.initIncomeEdition (Maybe.withDefault 0 (AccountModel.getCurrentIncome account))) + else Just (AccountModel.initIncomeEdition (Maybe.withDefault 0 (AccountModel.getCurrentIncome loggedData.incomes loggedData.me account))) } , Effects.none ) AccountAction.UpdateIncomeEdition income -> - case account.incomeEdition of - Just incomeEdition -> - ( { account | incomeEdition = Just { incomeEdition | income = income } } - , Effects.none - ) - Nothing -> - ( account - , Effects.none - ) - - AccountAction.UpdateEditionError error -> - case account.incomeEdition of - Just incomeEdition -> - ( { account | incomeEdition = Just { incomeEdition | error = Just error } } - , Effects.none - ) - Nothing -> - ( account - , Effects.none - ) - - AccountAction.UpdateIncome currentTime amount -> - ( account - , Server.setIncome currentTime amount - |> Task.map (\incomeId -> (AccountAction.ValidateUpdateIncome incomeId currentTime amount)) - |> flip Task.onError (always <| Task.succeed AccountAction.NoOp) - |> Effects.task + ( case account.incomeEdition of + Nothing -> account + Just incomeEdition -> { account | incomeEdition = Just { incomeEdition | income = income } } + , Effects.none ) - AccountAction.ValidateUpdateIncome incomeId currentTime amount -> - ( { account - | incomes = Dict.insert incomeId { userId = account.me, creation = currentTime, amount = amount } account.incomes - , incomeEdition = Nothing - } + AccountAction.UpdateEditionError error -> + ( case account.incomeEdition of + Nothing -> account + Just incomeEdition -> { account | incomeEdition = Just { incomeEdition | error = Just error } } , Effects.none ) diff --git a/src/client/elm/LoggedIn/Home/Account/View.elm b/src/client/elm/LoggedIn/Home/Account/View.elm index 252f8cf..a7d3e0c 100644 --- a/src/client/elm/LoggedIn/Home/Account/View.elm +++ b/src/client/elm/LoggedIn/Home/Account/View.elm @@ -3,13 +3,15 @@ module LoggedIn.Home.Account.View ) where import List -import Signal exposing (Address) +import Signal import Html exposing (..) import Html as H exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (..) +import LoggedData exposing (LoggedData) + import LoggedIn.Action as LoggedInAction import LoggedIn.Home.Action as HomeAction @@ -24,14 +26,15 @@ import LoggedIn.Home.Account.Model as AccountModel import Model exposing (Model) import Model.User exposing (getUserName) import Model.Translations exposing (getParamMessage, getMessage) -import Action exposing (..) +import Action +import Mailbox import View.Events exposing (onSubmitPrevDefault) import Utils.Either exposing (toMaybeError) -view : Address Action -> Model -> HomeModel.Model -> Html -view address model homeModel = +view : LoggedData -> HomeModel.Model -> Html +view loggedData homeModel = let account = homeModel.account in div [ classList @@ -39,76 +42,76 @@ view address model homeModel = , ("detail", account.visibleDetail) ] ] - [ exceedingPayers address model homeModel + [ exceedingPayers loggedData homeModel , if account.visibleDetail - then income address model account + then income loggedData account else text "" ] -exceedingPayers : Address Action -> Model -> HomeModel.Model -> Html -exceedingPayers address model homeModel = +exceedingPayers : LoggedData -> HomeModel.Model -> Html +exceedingPayers loggedData homeModel = button [ class "header" - , onClick address (UpdateLoggedIn << LoggedInAction.HomeAction << HomeAction.UpdateAccount <| AccountAction.ToggleDetail) + , onClick Mailbox.address (Action.UpdateLoggedIn << LoggedInAction.HomeAction << HomeAction.UpdateAccount <| AccountAction.ToggleDetail) ] - ( (List.map (exceedingPayer model homeModel) (getOrderedExceedingPayers model.currentTime homeModel.users homeModel.account.incomes homeModel.payments)) + ( (List.map (exceedingPayer loggedData homeModel) (getOrderedExceedingPayers loggedData.currentTime loggedData.users loggedData.incomes loggedData.payments)) ++ [ expand ExpandDown homeModel.account.visibleDetail ] ) -exceedingPayer : Model -> HomeModel.Model -> ExceedingPayer -> Html -exceedingPayer model homeModel payer = +exceedingPayer : LoggedData -> HomeModel.Model -> ExceedingPayer -> Html +exceedingPayer loggedData homeModel payer = div [ class "exceedingPayer" ] [ span [ class "userName" ] [ payer.userId - |> getUserName homeModel.users + |> getUserName loggedData.users |> Maybe.withDefault "−" |> text ] , span [ class "amount" ] - [ text ("+ " ++ (price model payer.amount)) ] + [ text ("+ " ++ (price loggedData.conf payer.amount)) ] ] -income : Address Action -> Model -> AccountModel.Model -> Html -income address model account = +income : LoggedData -> AccountModel.Model -> Html +income loggedData account = case account.incomeEdition of Nothing -> - incomeRead address model account + incomeRead loggedData account Just edition -> - incomeEdition address model account edition + incomeEdition loggedData account edition -incomeRead : Address Action -> Model -> AccountModel.Model -> Html -incomeRead address model account = +incomeRead : LoggedData -> AccountModel.Model -> Html +incomeRead loggedData account = div [ class "income" ] - [ ( case AccountModel.getCurrentIncome account of + [ ( case AccountModel.getCurrentIncome loggedData.incomes loggedData.me account of Nothing -> - text (getMessage "NoIncome" model.translations) + text (getMessage "NoIncome" loggedData.translations) Just income -> - text (getParamMessage [price model income] "Income" model.translations) + text (getParamMessage [price loggedData.conf income] "Income" loggedData.translations) ) - , toggleIncomeEdition address "editIncomeEdition" (getMessage "Edit" model.translations) + , toggleIncomeEdition loggedData "editIncomeEdition" (getMessage "Edit" loggedData.translations) ] -incomeEdition : Address Action -> Model -> AccountModel.Model -> AccountModel.IncomeEdition -> Html -incomeEdition address model account edition = +incomeEdition : LoggedData -> AccountModel.Model -> AccountModel.IncomeEdition -> Html +incomeEdition loggedData account edition = H.form - [ case AccountModel.validateIncome edition.income model.translations of + [ case AccountModel.validateIncome edition.income loggedData.translations of Ok validatedAmount -> - onSubmitPrevDefault address (UpdateLoggedIn << LoggedInAction.HomeAction << HomeAction.UpdateAccount <| AccountAction.UpdateIncome model.currentTime validatedAmount) + onSubmitPrevDefault Mailbox.address (Action.UpdateLoggedIn <| LoggedInAction.UpdateIncome validatedAmount) Err error -> - onSubmitPrevDefault address (UpdateLoggedIn << LoggedInAction.HomeAction << HomeAction.UpdateAccount << AccountAction.UpdateEditionError <| error) + onSubmitPrevDefault Mailbox.address (Action.UpdateLoggedIn << LoggedInAction.HomeAction << HomeAction.UpdateAccount << AccountAction.UpdateEditionError <| error) , class "income" ] [ label [ for "incomeInput" ] - [ text (getMessage "NewIncome" model.translations) ] + [ text (getMessage "NewIncome" loggedData.translations) ] , input [ id "incomeInput" , value edition.income - , on "input" targetValue (Signal.message address << UpdateLoggedIn << LoggedInAction.HomeAction << HomeAction.UpdateAccount << AccountAction.UpdateIncomeEdition) + , on "input" targetValue (Signal.message Mailbox.address << Action.UpdateLoggedIn << LoggedInAction.HomeAction << HomeAction.UpdateAccount << AccountAction.UpdateIncomeEdition) , maxlength 10 ] [] @@ -116,18 +119,18 @@ incomeEdition address model account edition = [ type' "submit" , class "validateIncomeEdition" ] - [ text (getMessage "Validate" model.translations) ] - , toggleIncomeEdition address "undoIncomeEdition" (getMessage "Undo" model.translations) + [ text (getMessage "Validate" loggedData.translations) ] + , toggleIncomeEdition loggedData "undoIncomeEdition" (getMessage "Undo" loggedData.translations) , case edition.error of Just error -> div [ class "error" ] [ text error ] Nothing -> text "" ] -toggleIncomeEdition : Address Action -> String -> String -> Html -toggleIncomeEdition address className name = +toggleIncomeEdition : LoggedData -> String -> String -> Html +toggleIncomeEdition loggedData className name = button [ type' "button" , class className - , onClick address (UpdateLoggedIn << LoggedInAction.HomeAction << HomeAction.UpdateAccount <| AccountAction.ToggleIncomeEdition) + , onClick Mailbox.address (Action.UpdateLoggedIn << LoggedInAction.HomeAction << HomeAction.UpdateAccount <| AccountAction.ToggleIncomeEdition) ] [ text name ] -- cgit v1.2.3