From bb19e02c668cf61c5566587a1f3a6319011c22c4 Mon Sep 17 00:00:00 2001 From: Eike Kettner Date: Sun, 22 Nov 2020 13:25:24 +0100 Subject: [PATCH] Ui improvements - don't show custom fields in edit menu if there are none. This reduces load of ui elements. The first custom field must be created in manage-data page. - Add more validation to the money type --- .../src/main/elm/Comp/CustomFieldInput.elm | 48 +++++++++---------- .../main/elm/Comp/CustomFieldMultiInput.elm | 6 +++ .../src/main/elm/Comp/ItemDetail/View.elm | 14 +++++- modules/webapp/src/main/elm/Data/Money.elm | 43 +++++++++++++++++ 4 files changed, 85 insertions(+), 26 deletions(-) create mode 100644 modules/webapp/src/main/elm/Data/Money.elm diff --git a/modules/webapp/src/main/elm/Comp/CustomFieldInput.elm b/modules/webapp/src/main/elm/Comp/CustomFieldInput.elm index df57d58e..deb3dc81 100644 --- a/modules/webapp/src/main/elm/Comp/CustomFieldInput.elm +++ b/modules/webapp/src/main/elm/Comp/CustomFieldInput.elm @@ -13,6 +13,7 @@ import Api.Model.CustomField exposing (CustomField) import Api.Model.ItemFieldValue exposing (ItemFieldValue) import Comp.DatePicker import Data.CustomFieldType exposing (CustomFieldType) +import Data.Money import Date exposing (Date) import DatePicker exposing (DatePicker) import Html exposing (..) @@ -130,14 +131,14 @@ initWith value = Data.CustomFieldType.Numeric -> let ( fm, _ ) = - updateFloatModel value.value identity + updateFloatModel value.value string2Float in NumberField fm Data.CustomFieldType.Money -> let ( fm, _ ) = - updateFloatModel value.value identity + updateFloatModel value.value Data.Money.fromString in MoneyField fm @@ -174,35 +175,32 @@ type alias UpdateResult = } -updateFloatModel : String -> (Float -> Float) -> ( FloatModel, FieldResult ) -updateFloatModel msg rounding = - case String.toFloat msg of - Just n -> - let - fieldVal = - if String.endsWith "." msg || String.endsWith ".0" msg then - msg - - else - String.fromFloat (rounding n) - in - ( { input = fieldVal - , result = Ok (rounding n) +updateFloatModel : String -> (String -> Result String Float) -> ( FloatModel, FieldResult ) +updateFloatModel msg parse = + case parse msg of + Ok n -> + ( { input = msg + , result = Ok n } - , Value (String.fromFloat (rounding n)) + , Value msg ) - Nothing -> + Err err -> ( { input = msg - , result = Err ("Not a number: " ++ msg) + , result = Err err } , NoResult ) -roundScale2 : Float -> Float -roundScale2 input = - (round (input * 100) |> toFloat) / 100 +string2Float : String -> Result String Float +string2Float str = + case String.toFloat str of + Just n -> + Ok n + + Nothing -> + Err ("Not a number: " ++ str) update : Msg -> Model -> UpdateResult @@ -218,7 +216,7 @@ update msg model = ( NumberMsg str, NumberField _ ) -> let ( fm, res ) = - updateFloatModel str identity + updateFloatModel str string2Float model_ = { model | fieldModel = NumberField fm } @@ -228,7 +226,9 @@ update msg model = ( MoneyMsg str, MoneyField _ ) -> let ( fm, res ) = - updateFloatModel str roundScale2 + updateFloatModel + str + Data.Money.fromString model_ = { model | fieldModel = MoneyField fm } diff --git a/modules/webapp/src/main/elm/Comp/CustomFieldMultiInput.elm b/modules/webapp/src/main/elm/Comp/CustomFieldMultiInput.elm index a64413d8..b5f7f57f 100644 --- a/modules/webapp/src/main/elm/Comp/CustomFieldMultiInput.elm +++ b/modules/webapp/src/main/elm/Comp/CustomFieldMultiInput.elm @@ -7,6 +7,7 @@ module Comp.CustomFieldMultiInput exposing , init , initCmd , initWith + , nonEmpty , setValues , update , view @@ -59,6 +60,11 @@ type alias FieldSelect = } +nonEmpty : Model -> Bool +nonEmpty model = + not (List.isEmpty model.availableFields && List.isEmpty model.visibleFields) + + initWith : List CustomField -> Model initWith fields = { fieldModels = Dict.empty diff --git a/modules/webapp/src/main/elm/Comp/ItemDetail/View.elm b/modules/webapp/src/main/elm/Comp/ItemDetail/View.elm index ddff5af6..d819a368 100644 --- a/modules/webapp/src/main/elm/Comp/ItemDetail/View.elm +++ b/modules/webapp/src/main/elm/Comp/ItemDetail/View.elm @@ -729,6 +729,10 @@ renderEditForm settings model = else span [ class "invisible hidden" ] [] + showCustomFields = + fieldVisible Data.Fields.CustomFields + && Comp.CustomFieldMultiInput.nonEmpty model.customFieldsModel + customFieldSettings = Comp.CustomFieldMultiInput.ViewSettings True "field" in @@ -777,14 +781,20 @@ item visible. This message will disappear then. """ ] ] - , optional [ Data.Fields.CustomFields ] <| + , if showCustomFields then h4 [ class "ui dividing header" ] [ Icons.customFieldIcon "" , text "Custom Fields" ] - , optional [ Data.Fields.CustomFields ] <| + + else + span [ class "hidden invisible" ] [] + , if showCustomFields then Html.map CustomFieldMsg (Comp.CustomFieldMultiInput.view customFieldSettings model.customFieldsModel) + + else + span [ class "hidden invisible" ] [] , optional [ Data.Fields.DueDate, Data.Fields.Date ] <| h4 [ class "ui dividing header" ] [ Icons.itemDatesIcon "" diff --git a/modules/webapp/src/main/elm/Data/Money.elm b/modules/webapp/src/main/elm/Data/Money.elm new file mode 100644 index 00000000..c62c3144 --- /dev/null +++ b/modules/webapp/src/main/elm/Data/Money.elm @@ -0,0 +1,43 @@ +module Data.Money exposing + ( Money + , format + , fromString + , roundMoney + ) + + +type alias Money = + Float + + +fromString : String -> Result String Money +fromString str = + let + points = + String.indexes "." str + + len = + String.length str + in + case points of + index :: [] -> + if index == (len - 3) then + String.toFloat str + |> Maybe.map Ok + |> Maybe.withDefault (Err "Two digits required after the dot.") + + else + Err ("Two digits required after the dot: " ++ str) + + _ -> + Err "One single dot + digits required for money." + + +format : Float -> String +format money = + String.fromFloat (roundMoney money) + + +roundMoney : Float -> Float +roundMoney input = + (round (input * 100) |> toFloat) / 100