From 9b7bf4e3c8137f83ff79aa4fa4b1e5fa7aadb8df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philip=20Scho=CC=88nholzer?= Date: Fri, 8 Oct 2021 14:18:34 +0200 Subject: [PATCH] Hack to have both input ranges be created by websocket when either is changed --- Web/Component/CommonConsumption.hs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/Web/Component/CommonConsumption.hs b/Web/Component/CommonConsumption.hs index 3c79f69..b9f62e3 100644 --- a/Web/Component/CommonConsumption.hs +++ b/Web/Component/CommonConsumption.hs @@ -25,8 +25,8 @@ data CommonConsumption = CommonConsumption -- The set of actions data CommonConsumptionController - = SetCommonConsumptionValue {newAmount :: !Double, newGCo2 :: !Double} - | SetTimesPerYear {newTimesPerYear :: !Double, newGCo2 :: !Double} + = SetCommonConsumptionValue {newAmount :: !Double, newGCo2 :: !Double, newOther :: !Double} + | SetTimesPerYear {newTimesPerYear :: !Double, newGCo2 :: !Double, newOther :: !Double} deriving (Eq, Show, Data) $(deriveSSC ''CommonConsumptionController) @@ -62,13 +62,13 @@ instance Component CommonConsumption CommonConsumptionController where =  {((calcCo2Factor gCo2 1.0 amount) * timesPerYear) |> renderWeight} CO2e / year

- {renderInput amount minAmount maxAmount "SetCommonConsumptionValue" "newAmount" "Single consumption"} + {renderInput amount minAmount maxAmount timesPerYear "SetCommonConsumptionValue" "newAmount" "Single consumption"} - {renderInput timesPerYear minTimesPerYear maxTimesPerYear "SetTimesPerYear" "newTimesPerYear" "Times per year"} + {renderInput timesPerYear minTimesPerYear maxTimesPerYear amount "SetTimesPerYear" "newTimesPerYear" "Times per year"} |] where - renderInput :: Double -> Double -> Double -> String -> String -> String -> Html - renderInput value minValue maxValue action newField labelTitle = case (minValue, maxValue) of + renderInput :: Double -> Double -> Double -> Double -> String -> String -> String -> Html + renderInput value minValue maxValue otherValue action newField labelTitle = case (minValue, maxValue) of (min, max) | min == max -> [hsx||] _ -> [hsx| @@ -76,6 +76,7 @@ instance Component CommonConsumption CommonConsumptionController where action <> "', { " <> newField <> ": parseFloat(this.value), newGCo2: parseFloat(this.dataset.gco2) })" + onInput = "callServerAction('" <> action <> "', { " <> newField <> ": parseFloat(this.value), newGCo2: parseFloat(this.dataset.gco2), newOther: parseFloat(this.dataset.other) })" svg = [hsx| @@ -100,15 +101,17 @@ instance Component CommonConsumption CommonConsumptionController where |] -- The action handlers - action state SetCommonConsumptionValue {newAmount, newGCo2} = do + action state SetCommonConsumptionValue {newAmount, newGCo2, newOther} = do state |> set #amount newAmount |> set #gCo2 newGCo2 + |> set #timesPerYear newOther |> pure - action state SetTimesPerYear {newTimesPerYear, newGCo2} = do + action state SetTimesPerYear {newTimesPerYear, newGCo2, newOther} = do state |> set #timesPerYear newTimesPerYear |> set #gCo2 newGCo2 + |> set #amount newOther |> pure instance SetField "amount" CommonConsumption Double where setField amount' commonConsumption = commonConsumption {amount = amount'}