Commits

gauthier  committed 06f5314

actual logic to pass data to CalHeatMap widget

  • Participants
  • Parent commits 6570862

Comments (0)

Files changed (1)

File Yesod/Widgets/CalHeatMap.hs

+{-# LANGUAGE FlexibleContexts  #-}
 {-# LANGUAGE OverloadedStrings #-}
 {-# LANGUAGE QuasiQuotes       #-}
+
 module Yesod.Widgets.CalHeatMap where
-import           Data.Aeson as J
-import           Data.Text
+import           Data.Aeson            as J
+import           Data.List             (init, map)
+import qualified Data.Text             as T
 import           Data.Time
+import           Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds)
 import           Prelude
+import           System.Posix.Types    (EpochTime)
+import           Text.Blaze.Internal
 import           Yesod.Core
 
-data CalHeatMapData
- = CalHeatMapRawData [(UTCTime, Double)]
- | CalHeatMapDataUri Text
 
-data CalHeatMapSettings
-  = CalHeatMapSettings {
+data CalHeatMapData = CalHeatMapRawData [(UTCTime, Double)]
+                    | CalHeatMapDataUri T.Text
+
+data CalHeatMapSettings = CalHeatMapSettings {
     chmData        :: CalHeatMapData
     , chmStartDate :: UTCTime
     }
 
 --
 --
-
 calHeatMapWidget d = do
   divId <- newIdent
   toWidgetBody [hamlet|
 })()
 |]
   where
-    serializedData = case (chmData d) of
+    serializedData = case chmData d of
      (CalHeatMapRawData rd) -> serializeRawData rd
-     (CalHeatMapDataUri uri) -> uri
+     (CalHeatMapDataUri uri) -> J.String uri
 
-serializeRawData rd = "asdasd"
+serializeRawData :: [(UTCTime, Double)] -> Value
+serializeRawData items = J.object $ map transformToPair items
+  where
+    transformToPair (time, value) = (formattedTime time, toJSON value)
+    formattedTime = transformTimeToPOSIXEpochString
+    transformTimeToPOSIXEpochString :: UTCTime -> T.Text
+    transformTimeToPOSIXEpochString t = T.pack $ init $ show $ utcTimeToPOSIXSeconds t