Skip to content

Commit

Permalink
refactor: move amount formatting fn to Views.Format.
Browse files Browse the repository at this point in the history
  • Loading branch information
n1k0 committed Nov 20, 2024
1 parent 2f476ef commit ef03d3e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
20 changes: 1 addition & 19 deletions src/Page/Object.elm
Original file line number Diff line number Diff line change
Expand Up @@ -633,28 +633,10 @@ componentView selectedImpact detailedComponents ( quantity, name, processes ) it

processView : Definition -> ( Query.Amount, Process ) -> Results -> Html Msg
processView selectedImpact ( amount, process ) itemResults =
let
floatAmount =
Query.amountToFloat amount
in
tr [ class "fs-7" ]
[ td [] []
, td [ class "text-end text-nowrap" ]
[ case process.unit of
"kg" ->
Mass.kilograms floatAmount
|> Format.kg

"m3" ->
Volume.cubicMeters floatAmount
|> Format.m3

_ ->
String.fromFloat floatAmount
++ " "
++ process.unit
|> text
]
[ Format.amount process amount ]
, td [ class "align-middle text-truncate w-100" ]
[ text process.displayName ]
, td [ class "align-middle text-end text-nowrap" ]
Expand Down
27 changes: 26 additions & 1 deletion src/Views/Format.elm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Views.Format exposing
( complement
( amount
, complement
, days
, density
, formatFloat
Expand Down Expand Up @@ -28,6 +29,8 @@ module Views.Format exposing
import Area exposing (Area)
import Data.Impact as Impact exposing (Impacts)
import Data.Impact.Definition exposing (Definition)
import Data.Object.Process as ObjectProcess
import Data.Object.Query as ObjectQuery
import Data.Split as Split exposing (Split)
import Data.Textile.Economics as Economics
import Data.Unit as Unit
Expand Down Expand Up @@ -151,6 +154,28 @@ complement impact =
]


amount : ObjectProcess.Process -> ObjectQuery.Amount -> Html msg
amount { unit } amount_ =
let
floatAmount =
ObjectQuery.amountToFloat amount_
in
case unit of
"kg" ->
Mass.kilograms floatAmount
|> kg

"m3" ->
Volume.cubicMeters floatAmount
|> m3

_ ->
String.fromFloat floatAmount
++ " "
++ unit
|> text


kg : Mass -> Html msg
kg =
Mass.inKilograms >> formatRichFloat 3 "kg"
Expand Down

0 comments on commit ef03d3e

Please sign in to comment.