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 9c5c262
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
22 changes: 1 addition & 21 deletions src/Page/Object.elm
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import Data.Uuid exposing (Uuid)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Mass
import Ports
import Route
import Set exposing (Set)
Expand All @@ -46,7 +45,6 @@ import Views.ImpactTabs as ImpactTabs
import Views.Link as Link
import Views.Modal as ModalView
import Views.Sidebar as SidebarView
import Volume


type alias Model =
Expand Down Expand Up @@ -633,28 +631,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 9c5c262

Please sign in to comment.