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
This commit is contained in:
Eike Kettner 2020-11-22 13:25:24 +01:00
parent ff30ed5558
commit bb19e02c66
4 changed files with 85 additions and 26 deletions

View File

@ -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 }

View File

@ -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

View File

@ -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 ""

View File

@ -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