From bb316286b0859b5648c61f44c88399f4c1aad9cd Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 2 Jan 2016 19:07:19 +0100 Subject: Use start-app for elm --- src/client/elm/View/LoggedIn/Account.elm | 65 +++++++-------- src/client/elm/View/LoggedIn/Add.elm | 122 --------------------------- src/client/elm/View/LoggedIn/AddPayment.elm | 123 ++++++++++++++++++++++++++++ src/client/elm/View/LoggedIn/Monthly.elm | 43 +++++----- src/client/elm/View/LoggedIn/Paging.elm | 47 +++++------ src/client/elm/View/LoggedIn/Table.elm | 41 +++++----- 6 files changed, 220 insertions(+), 221 deletions(-) delete mode 100644 src/client/elm/View/LoggedIn/Add.elm create mode 100644 src/client/elm/View/LoggedIn/AddPayment.elm (limited to 'src/client/elm/View/LoggedIn') diff --git a/src/client/elm/View/LoggedIn/Account.elm b/src/client/elm/View/LoggedIn/Account.elm index 706f7cc..9459740 100644 --- a/src/client/elm/View/LoggedIn/Account.elm +++ b/src/client/elm/View/LoggedIn/Account.elm @@ -2,23 +2,24 @@ module View.LoggedIn.Account ( account ) where +import List +import Signal exposing (Address) + import Html exposing (..) import Html as H exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (..) -import List - -import ServerCommunication as SC exposing (serverCommunications) - -import Update exposing (..) -import Update.LoggedIn exposing (..) -import Update.LoggedIn.Account exposing (..) import Model exposing (Model) import Model.User exposing (getUserName) import Model.Payer exposing (..) -import Model.View.LoggedInView exposing (LoggedInView) import Model.Translations exposing (getParamMessage, getMessage) +import Model.Action exposing (..) +import Model.Action.LoggedInAction exposing (..) +import Model.Action.AccountAction exposing (..) +import Model.Communication as Communication + +import Model.View.LoggedInView exposing (LoggedInView) import Model.View.LoggedIn.Account exposing (..) import View.Expand exposing (..) @@ -27,8 +28,8 @@ import View.Events exposing (onSubmitPrevDefault) import Utils.Either exposing (toMaybeError) -account : Model -> LoggedInView -> Html -account model loggedInView = +account : Address Action -> Model -> LoggedInView -> Html +account address model loggedInView = let account = loggedInView.account in div [ classList @@ -36,17 +37,17 @@ account model loggedInView = , ("detail", account.visibleDetail) ] ] - [ exceedingPayers model loggedInView + [ exceedingPayers address model loggedInView , if account.visibleDetail - then income model account + then income address model account else text "" ] -exceedingPayers : Model -> LoggedInView -> Html -exceedingPayers model loggedInView = +exceedingPayers : Address Action -> Model -> LoggedInView -> Html +exceedingPayers address model loggedInView = button [ class "header" - , onClick actions.address (UpdateLoggedIn << UpdateAccount <| ToggleDetail) + , onClick address (UpdateLoggedIn << UpdateAccount <| ToggleDetail) ] ( (List.map (exceedingPayer model loggedInView) (getOrderedExceedingPayers model.currentTime loggedInView.account.payers)) ++ [ expand ExpandDown loggedInView.account.visibleDetail ] @@ -68,16 +69,16 @@ exceedingPayer model loggedInView payer = [ text ("+ " ++ (price model payer.amount)) ] ] -income : Model -> Account -> Html -income model account = +income : Address Action -> Model -> Account -> Html +income address model account = case account.incomeEdition of - Just edition -> - incomeEdition model account edition Nothing -> - incomeRead model account + incomeRead address model account + Just edition -> + incomeEdition address model account edition -incomeRead : Model -> Account -> Html -incomeRead model account = +incomeRead : Address Action -> Model -> Account -> Html +incomeRead address model account = div [ class "income" ] [ ( case getCurrentIncome account of @@ -86,17 +87,17 @@ incomeRead model account = Just income -> text (getParamMessage [price model income] "Income" model.translations) ) - , toggleIncomeEdition "editIncomeEdition" (getMessage "Edit" model.translations) + , toggleIncomeEdition address "editIncomeEdition" (getMessage "Edit" model.translations) ] -incomeEdition : Model -> Account -> IncomeEdition -> Html -incomeEdition model account edition = +incomeEdition : Address Action -> Model -> Account -> IncomeEdition -> Html +incomeEdition address model account edition = H.form [ case validateIncome edition.income model.translations of Ok validatedAmount -> - onSubmitPrevDefault serverCommunications.address (SC.SetIncome model.currentTime validatedAmount) + onSubmitPrevDefault address (ServerCommunication <| Communication.SetIncome model.currentTime validatedAmount) Err error -> - onSubmitPrevDefault actions.address (UpdateLoggedIn << UpdateAccount << UpdateEditionError <| error) + onSubmitPrevDefault address (UpdateLoggedIn << UpdateAccount << UpdateEditionError <| error) , class "income" ] [ label @@ -105,7 +106,7 @@ incomeEdition model account edition = , input [ id "incomeInput" , value edition.income - , on "input" targetValue (Signal.message actions.address << UpdateLoggedIn << UpdateAccount << UpdateIncomeEdition) + , on "input" targetValue (Signal.message address << UpdateLoggedIn << UpdateAccount << UpdateIncomeEdition) , maxlength 10 ] [] @@ -114,17 +115,17 @@ incomeEdition model account edition = , class "validateIncomeEdition" ] [ text (getMessage "Validate" model.translations) ] - , toggleIncomeEdition "undoIncomeEdition" (getMessage "Undo" model.translations) + , toggleIncomeEdition address "undoIncomeEdition" (getMessage "Undo" model.translations) , case edition.error of Just error -> div [ class "error" ] [ text error ] Nothing -> text "" ] -toggleIncomeEdition : String -> String -> Html -toggleIncomeEdition className name = +toggleIncomeEdition : Address Action -> String -> String -> Html +toggleIncomeEdition address className name = button [ type' "button" , class className - , onClick actions.address (UpdateLoggedIn << UpdateAccount <| ToggleIncomeEdition) + , onClick address (UpdateLoggedIn << UpdateAccount <| ToggleIncomeEdition) ] [ text name ] diff --git a/src/client/elm/View/LoggedIn/Add.elm b/src/client/elm/View/LoggedIn/Add.elm deleted file mode 100644 index 2f580f9..0000000 --- a/src/client/elm/View/LoggedIn/Add.elm +++ /dev/null @@ -1,122 +0,0 @@ -module View.LoggedIn.Add - ( addPayment - ) where - -import Html as H exposing (..) -import Html.Attributes exposing (..) -import Html.Events exposing (..) -import Reads exposing (readInt) -import Result exposing (..) - -import ServerCommunication as SC exposing (serverCommunications) - -import Update exposing (..) -import Update.LoggedIn exposing (..) -import Update.LoggedIn.Add exposing (..) - -import Model exposing (Model) -import Model.View.LoggedIn.Add exposing (..) -import Model.Translations exposing (getMessage) -import Model.View.LoggedInView exposing (LoggedInView) - -import View.Events exposing (onSubmitPrevDefault) -import View.Icon exposing (renderIcon) - -import Utils.Maybe exposing (isJust) -import Utils.Either exposing (toMaybeError) - -addPayment : Model -> LoggedInView -> Html -addPayment model loggedInView = - H.form - [ case (validateName loggedInView.add.name model.translations, validateCost loggedInView.add.cost model.translations) of - (Ok name, Ok cost) -> - let action = - case loggedInView.add.frequency of - Punctual -> SC.AddPayment name cost - Monthly -> SC.AddMonthlyPayment name cost - in onSubmitPrevDefault serverCommunications.address action - (resName, resCost) -> - onSubmitPrevDefault actions.address (UpdateLoggedIn <| UpdateAdd <| AddError (toMaybeError resName) (toMaybeError resCost)) - , class "addPayment" - ] - [ addPaymentName loggedInView.add - , addPaymentCost model loggedInView.add - , paymentFrequency model loggedInView.add - , button - [ type' "submit" - , class "add" ] - [ text (getMessage "Add" model.translations)] - ] - -addPaymentName : AddPayment -> Html -addPaymentName addPayment = - div - [ classList - [ ("name", True) - , ("error", isJust addPayment.nameError) - ] - ] - [ input - [ id "nameInput" - , value addPayment.name - , on "input" targetValue (Signal.message actions.address << UpdateLoggedIn << UpdateAdd << UpdateName) - , maxlength 20 - ] - [] - , label - [ for "nameInput" ] - [ renderIcon "shopping-cart" ] - , case addPayment.nameError of - Just error -> - div [ class "errorMessage" ] [ text error ] - Nothing -> - text "" - ] - -addPaymentCost : Model -> AddPayment -> Html -addPaymentCost model addPayment = - div - [ classList - [ ("cost", True) - , ("error", isJust addPayment.costError) - ] - ] - [ input - [ id "costInput" - , value addPayment.cost - , on "input" targetValue (Signal.message actions.address << UpdateLoggedIn << UpdateAdd << UpdateCost) - , maxlength 7 - ] - [] - , label - [ for "costInput" ] - [ text model.config.currency ] - , case addPayment.costError of - Just error -> - div [ class "errorMessage" ] [ text error ] - Nothing -> - text "" - ] - -paymentFrequency : Model -> AddPayment -> Html -paymentFrequency model addPayment = - button - [ type' "button" - , class "frequency" - , onClick actions.address (UpdateLoggedIn << 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) ] - ] diff --git a/src/client/elm/View/LoggedIn/AddPayment.elm b/src/client/elm/View/LoggedIn/AddPayment.elm new file mode 100644 index 0000000..0fbe28e --- /dev/null +++ b/src/client/elm/View/LoggedIn/AddPayment.elm @@ -0,0 +1,123 @@ +module View.LoggedIn.AddPayment + ( addPayment + ) where + +import Reads exposing (readInt) +import Result exposing (..) +import Signal exposing (Address) + +import Html as H exposing (..) +import Html.Attributes exposing (..) +import Html.Events exposing (..) + +import Model exposing (Model) +import Model.Translations exposing (getMessage) +import Model.Action exposing (..) +import Model.Action.LoggedInAction exposing (..) +import Model.Action.AddPaymentAction exposing (..) +import Model.Communication as Communication + +import Model.View.LoggedIn.AddPayment exposing (..) +import Model.View.LoggedInView exposing (LoggedInView) + +import View.Events exposing (onSubmitPrevDefault) +import View.Icon exposing (renderIcon) + +import Utils.Maybe exposing (isJust) +import Utils.Either exposing (toMaybeError) + +addPayment : Address Action -> Model -> LoggedInView -> Html +addPayment address model loggedInView = + H.form + [ case (validateName loggedInView.add.name model.translations, validateCost loggedInView.add.cost model.translations) of + (Ok name, Ok cost) -> + let action = + case loggedInView.add.frequency of + Punctual -> Communication.AddPayment name cost + Monthly -> Communication.AddMonthlyPayment name cost + in onSubmitPrevDefault address (ServerCommunication action) + (resName, resCost) -> + onSubmitPrevDefault address (UpdateLoggedIn <| UpdateAdd <| AddError (toMaybeError resName) (toMaybeError resCost)) + , class "addPayment" + ] + [ addPaymentName address loggedInView.add + , addPaymentCost address model loggedInView.add + , paymentFrequency address model loggedInView.add + , button + [ type' "submit" + , class "add" ] + [ text (getMessage "Add" model.translations)] + ] + +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 << 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 << UpdateAdd << UpdateCost) + , maxlength 7 + ] + [] + , label + [ for "costInput" ] + [ text model.config.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 << 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) ] + ] diff --git a/src/client/elm/View/LoggedIn/Monthly.elm b/src/client/elm/View/LoggedIn/Monthly.elm index a274015..2c11993 100644 --- a/src/client/elm/View/LoggedIn/Monthly.elm +++ b/src/client/elm/View/LoggedIn/Monthly.elm @@ -3,29 +3,28 @@ module View.LoggedIn.Monthly ) where import String +import Signal exposing (Address) import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (..) -import Update exposing (..) -import Update.LoggedIn exposing (..) -import Update.LoggedIn.Monthly exposing (..) - import Model exposing (Model) -import Model.View.LoggedIn.Monthly exposing (Monthly) import Model.Payment exposing (Payments, Payment) -import Model.View.LoggedInView exposing (LoggedInView) import Model.Translations exposing (getMessage, getParamMessage) - -import ServerCommunication as SC exposing (serverCommunications) +import Model.Action exposing (..) +import Model.Action.LoggedInAction exposing (..) +import Model.Action.MonthlyAction exposing (..) +import Model.Communication as Communication +import Model.View.LoggedIn.Monthly exposing (Monthly) +import Model.View.LoggedInView exposing (LoggedInView) import View.Icon exposing (renderIcon) import View.Expand exposing (..) import View.Price exposing (price) -monthlyPayments : Model -> LoggedInView -> Html -monthlyPayments model loggedInView = +monthlyPayments : Address Action -> Model -> LoggedInView -> Html +monthlyPayments address model loggedInView = let monthly = loggedInView.monthly in if List.length monthly.payments == 0 then @@ -37,40 +36,40 @@ monthlyPayments model loggedInView = , ("detail", monthly.visibleDetail) ] ] - [ monthlyCount model monthly - , if monthly.visibleDetail then paymentsTable model loggedInView monthly else text "" + [ monthlyCount address model monthly + , if monthly.visibleDetail then paymentsTable address model loggedInView monthly else text "" ] -monthlyCount : Model -> Monthly -> Html -monthlyCount model monthly = +monthlyCount : Address Action -> Model -> Monthly -> Html +monthlyCount address model monthly = let count = List.length monthly.payments total = List.sum << List.map .cost <| monthly.payments key = if count > 1 then "PluralMonthlyCount" else "SingularMonthlyCount" in button [ class "header" - , onClick actions.address (UpdateLoggedIn << UpdateMonthly <| ToggleDetail) + , onClick address (UpdateLoggedIn << UpdateMonthly <| ToggleDetail) ] [ text (getParamMessage [toString count, price model total] key model.translations) , expand ExpandDown monthly.visibleDetail ] -paymentsTable : Model -> LoggedInView -> Monthly -> Html -paymentsTable model loggedInView monthly = +paymentsTable : Address Action -> Model -> LoggedInView -> Monthly -> Html +paymentsTable address model loggedInView monthly = div [ class "table" ] ( monthly.payments |> List.sortBy (String.toLower << .name) - |> List.map (paymentLine model loggedInView) + |> List.map (paymentLine address model loggedInView) ) -paymentLine : Model -> LoggedInView -> Payment -> Html -paymentLine model loggedInView payment = +paymentLine : Address Action -> Model -> LoggedInView -> Payment -> Html +paymentLine address model loggedInView payment = a [ classList [ ("row", True) , ("edition", loggedInView.paymentEdition == Just payment.id) ] - , onClick actions.address (UpdateLoggedIn (ToggleEdit payment.id)) + , onClick address (UpdateLoggedIn (ToggleEdit payment.id)) ] [ div [ class "cell category" ] [ text (payment.name) ] , div @@ -82,7 +81,7 @@ paymentLine model loggedInView payment = [ text (price model payment.cost) ] , div [ class "cell delete" - , onClick serverCommunications.address (SC.DeleteMonthlyPayment payment.id) + , onClick address (ServerCommunication <| Communication.DeleteMonthlyPayment payment.id) ] [ button [] [ renderIcon "times" ] ] diff --git a/src/client/elm/View/LoggedIn/Paging.elm b/src/client/elm/View/LoggedIn/Paging.elm index e40c5aa..154686a 100644 --- a/src/client/elm/View/LoggedIn/Paging.elm +++ b/src/client/elm/View/LoggedIn/Paging.elm @@ -2,23 +2,24 @@ module View.LoggedIn.Paging ( paymentsPaging ) where +import Signal exposing (Address) + import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (..) +import Model.Action exposing (..) +import Model.Action.LoggedInAction exposing (..) import Model.View.LoggedInView exposing (..) import Model.Payment exposing (perPage) -import Update exposing (..) -import Update.LoggedIn exposing (..) - import View.Icon exposing (renderIcon) showedPages : Int showedPages = 5 -paymentsPaging : LoggedInView -> Html -paymentsPaging loggedInView = +paymentsPaging : Address Action -> LoggedInView -> Html +paymentsPaging address loggedInView = let maxPage = ceiling (toFloat loggedInView.paymentsCount / toFloat perPage) pages = truncatePages loggedInView.currentPage [1..maxPage] in if maxPage == 1 @@ -28,12 +29,12 @@ paymentsPaging loggedInView = div [ class "pages" ] ( ( if loggedInView.currentPage > 1 - then [ firstPage, previousPage loggedInView ] + then [ firstPage address, previousPage address loggedInView ] else [] ) - ++ ( List.map (paymentsPage loggedInView) pages) + ++ ( List.map (paymentsPage address loggedInView) pages) ++ ( if loggedInView.currentPage < maxPage - then [ nextPage loggedInView, lastPage maxPage ] + then [ nextPage address loggedInView, lastPage address maxPage ] else [] ) ) @@ -52,47 +53,47 @@ truncatePages currentPage pages = [(currentPage - showedLeftPages)..(currentPage + showedRightPages)] in List.filter (flip List.member pages) truncatedPages -firstPage : Html -firstPage = +firstPage : Address Action -> Html +firstPage address = button [ class "page" - , onClick actions.address (UpdateLoggedIn (UpdatePage 1)) + , onClick address (UpdateLoggedIn (UpdatePage 1)) ] [ renderIcon "fast-backward" ] -previousPage : LoggedInView -> Html -previousPage loggedInView = +previousPage : Address Action -> LoggedInView -> Html +previousPage address loggedInView = button [ class "page" - , onClick actions.address (UpdateLoggedIn (UpdatePage (loggedInView.currentPage - 1))) + , onClick address (UpdateLoggedIn (UpdatePage (loggedInView.currentPage - 1))) ] [ renderIcon "backward" ] -nextPage : LoggedInView -> Html -nextPage loggedInView = +nextPage : Address Action -> LoggedInView -> Html +nextPage address loggedInView = button [ class "page" - , onClick actions.address (UpdateLoggedIn (UpdatePage (loggedInView.currentPage + 1))) + , onClick address (UpdateLoggedIn (UpdatePage (loggedInView.currentPage + 1))) ] [ renderIcon "forward" ] -lastPage : Int -> Html -lastPage maxPage = +lastPage : Address Action -> Int -> Html +lastPage address maxPage = button [ class "page" - , onClick actions.address (UpdateLoggedIn (UpdatePage maxPage)) + , onClick address (UpdateLoggedIn (UpdatePage maxPage)) ] [ renderIcon "fast-forward" ] -paymentsPage : LoggedInView -> Int -> Html -paymentsPage loggedInView page = +paymentsPage : Address Action -> LoggedInView -> Int -> Html +paymentsPage address loggedInView page = let onCurrentPage = page == loggedInView.currentPage in button [ classList [ ("page", True) , ("current", onCurrentPage) ] - , onClick actions.address <| + , onClick address <| if onCurrentPage then NoOp else UpdateLoggedIn (UpdatePage page) ] [ text (toString page) ] diff --git a/src/client/elm/View/LoggedIn/Table.elm b/src/client/elm/View/LoggedIn/Table.elm index 51a7b73..8590dc5 100644 --- a/src/client/elm/View/LoggedIn/Table.elm +++ b/src/client/elm/View/LoggedIn/Table.elm @@ -2,36 +2,33 @@ module View.LoggedIn.Table ( paymentsTable ) where -import Html exposing (..) -import Html.Attributes exposing (..) -import Html.Events exposing (..) import Dict exposing (..) - -import Date import Date exposing (Date) - +import Signal exposing (Address) import String exposing (append) +import Html exposing (..) +import Html.Attributes exposing (..) +import Html.Events exposing (..) + import Model exposing (Model) import Model.User exposing (getUserName) import Model.Payment exposing (..) -import Model.View.LoggedInView exposing (LoggedInView) import Model.Translations exposing (getMessage) - -import ServerCommunication as SC exposing (serverCommunications) - -import Update exposing (..) -import Update.LoggedIn exposing (..) +import Model.Action exposing (..) +import Model.Action.LoggedInAction exposing (..) +import Model.Communication as Communication +import Model.View.LoggedInView exposing (LoggedInView) import View.Icon exposing (renderIcon) import View.Date exposing (..) import View.Price exposing (price) -paymentsTable : Model -> LoggedInView -> Html -paymentsTable model loggedInView = +paymentsTable : Address Action -> Model -> LoggedInView -> Html +paymentsTable address model loggedInView = div [ class "table" ] - ( headerLine model :: paymentLines model loggedInView) + ( headerLine model :: paymentLines address model loggedInView) headerLine : Model -> Html headerLine model = @@ -44,23 +41,23 @@ headerLine model = , div [ class "cell" ] [] ] -paymentLines : Model -> LoggedInView -> List Html -paymentLines model loggedInView = +paymentLines : Address Action -> Model -> LoggedInView -> List Html +paymentLines address model loggedInView = loggedInView.payments |> List.sortBy (Date.toTime << .creation) |> List.reverse |> List.drop ((loggedInView.currentPage - 1) * perPage) |> List.take perPage - |> List.map (paymentLine model loggedInView) + |> List.map (paymentLine address model loggedInView) -paymentLine : Model -> LoggedInView -> Payment -> Html -paymentLine model loggedInView payment = +paymentLine : Address Action -> Model -> LoggedInView -> Payment -> Html +paymentLine address model loggedInView payment = a [ classList [ ("row", True) , ("edition", loggedInView.paymentEdition == Just payment.id) ] - , onClick actions.address (UpdateLoggedIn (ToggleEdit payment.id)) + , onClick address (UpdateLoggedIn (ToggleEdit payment.id)) ] [ div [ class "cell category" ] [ text payment.name ] , div @@ -91,7 +88,7 @@ paymentLine model loggedInView payment = div [ class "cell delete" ] [ button - [ onClick serverCommunications.address (SC.DeletePayment payment loggedInView.currentPage) ] + [ onClick address (ServerCommunication <| Communication.DeletePayment payment loggedInView.currentPage) ] [ renderIcon "times" ] ] else -- cgit v1.2.3