From 9a95a674fbbf1e64d3ad07922d569c3a1c751cf2 Mon Sep 17 00:00:00 2001 From: Joris Date: Wed, 5 Apr 2017 17:04:33 +0200 Subject: Show aliments, glycemic index, carbohydrates and glycemic charge --- src/Aliment.purs | 13 ------------- src/Food.purs | 25 +++++++++++++++++++++++++ src/Format.purs | 29 +++++++++++++++++++++++++++++ src/Page.purs | 45 ++++++++++++++++++++++++++++++++++++++------- 4 files changed, 92 insertions(+), 20 deletions(-) delete mode 100644 src/Aliment.purs create mode 100644 src/Food.purs create mode 100644 src/Format.purs (limited to 'src') diff --git a/src/Aliment.purs b/src/Aliment.purs deleted file mode 100644 index e7c4fc8..0000000 --- a/src/Aliment.purs +++ /dev/null @@ -1,13 +0,0 @@ -module Aliment where - -all :: Array Aliment -all = - [ { name: "Oignon", gi: 15 } - , { name: "Olive", gi: 15 } - , { name: "Haricot rouge", gi: 35 } - ] - -type Aliment = - { name :: String - , gi :: Int - } diff --git a/src/Food.purs b/src/Food.purs new file mode 100644 index 0000000..b1e3d29 --- /dev/null +++ b/src/Food.purs @@ -0,0 +1,25 @@ +module Food + ( Aliment + , all + , glycemicLoad + ) where + +import Data.Int (toNumber) +import Prelude + +type Aliment = + { name :: String + , glycemicIndex :: Int + , carbohydrates :: Int -- for 100 grams + } + +glycemicLoad :: Aliment -> Number +glycemicLoad aliment = toNumber aliment.glycemicIndex * toNumber aliment.carbohydrates / 100.0 + +all :: Array Aliment +all = + [ { name: "oignon", glycemicIndex: 15, carbohydrates: 9 } + , { name: "olive", glycemicIndex: 15, carbohydrates: 6 } + , { name: "haricot rouge", glycemicIndex: 35, carbohydrates: 24 } + , { name: "haricot blanc", glycemicIndex: 35, carbohydrates: 13 } + ] diff --git a/src/Format.purs b/src/Format.purs new file mode 100644 index 0000000..22593e5 --- /dev/null +++ b/src/Format.purs @@ -0,0 +1,29 @@ +module Format where + +import Data.Int (toNumber, fromNumber) +import Data.Array (replicate) +import Data.Maybe (fromMaybe) +import Data.String (length, joinWith) +import Math (round, trunc, pow) + +import Prelude + +number :: Int -> Number -> String +number decimalLength num = formattedIntegerPart <> formattedDecimalPart + where + formattedIntegerPart = + (if decimalLength > 0 then trunc num else round num) + # fromNumber + # fromMaybe 0 + # show + + formattedDecimalPart = + if decimalLength > 0 then + ((num - trunc num) * pow 10.0 (toNumber decimalLength)) + # round + # fromNumber + # fromMaybe 0 + # show + # \str -> "," <> (joinWith "" $ replicate (decimalLength - length str) "0") <> str + else + "" diff --git a/src/Page.purs b/src/Page.purs index 83e4d73..76cdf8d 100644 --- a/src/Page.purs +++ b/src/Page.purs @@ -1,12 +1,14 @@ module Page where -import Prelude import Data.Maybe (Maybe(..)) import Halogen as H import Halogen.HTML as HH +import Halogen.HTML.Properties as HP +import Prelude -import Aliment as Aliment -import Aliment (Aliment) +import Food (Aliment) +import Food as Food +import Format as Format type State = Unit @@ -27,19 +29,48 @@ component = render state = HH.div [] - [ HH.h1 [] [ HH.text "Sucre" ] + [ HH.h1 [] [ HH.text "Glycémie" ] , HH.ul - [] - (map renderAliment Aliment.all) + [ HP.class_ $ HH.ClassName "aliments" ] + ([renderTitle] <> (map renderAliment Food.all)) ] eval :: Query ~> H.ComponentDSL State Query Message m eval = case _ of NoOp next -> pure next +renderTitle :: H.ComponentHTML Query +renderTitle = + HH.li + [ HP.class_ $ HH.ClassName "title" ] + [ HH.div + [] + [ HH.text "Aliment" ] + , HH.div + [] + [ HH.text "Index glycémique" ] + , HH.div + [] + [ HH.text "Glucides pour 100 g" ] + , HH.div + [] + [ HH.text "Charge glycémique" ] + ] + renderAliment :: Aliment -> H.ComponentHTML Query renderAliment aliment = HH.li [] - [ HH.text (aliment.name <> ", index glycémique: " <> (show aliment.gi)) + [ HH.div + [] + [ HH.text aliment.name ] + , HH.div + [ HP.class_ $ HH.ClassName "number" ] + [ HH.text (show aliment.glycemicIndex) ] + , HH.div + [ HP.class_ $ HH.ClassName "number" ] + [ HH.text (show aliment.carbohydrates) ] + , HH.div + [ HP.class_ $ HH.ClassName "number" ] + [ HH.text (Format.number 2 <<< Food.glycemicLoad $ aliment) ] ] -- cgit v1.2.3