From ef03d3eb8a8018dd2627b538eace4c08c3ecab89 Mon Sep 17 00:00:00 2001 From: Nicolas Perriault Date: Wed, 20 Nov 2024 16:05:06 +0100 Subject: [PATCH] refactor: move amount formatting fn to Views.Format. --- src/Page/Object.elm | 20 +------------------- src/Views/Format.elm | 27 ++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/Page/Object.elm b/src/Page/Object.elm index be479aa80..9d3f17369 100644 --- a/src/Page/Object.elm +++ b/src/Page/Object.elm @@ -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" ] diff --git a/src/Views/Format.elm b/src/Views/Format.elm index 00b856a7a..aa387de5c 100644 --- a/src/Views/Format.elm +++ b/src/Views/Format.elm @@ -1,5 +1,6 @@ module Views.Format exposing - ( complement + ( amount + , complement , days , density , formatFloat @@ -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 @@ -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"