Created
December 5, 2021 16:26
-
-
Save pete-murphy/d87c919fc52ca5fb3f1dc9f4e0bd36e5 to your computer and use it in GitHub Desktop.
checkDateChange debugging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Prelude | |
import Data.Date (Date) | |
import Data.Date as Date | |
import Data.Enum as Enum | |
import Data.Maybe (Maybe(..)) | |
import Data.Maybe as Maybe | |
import Data.String.CodePoints as CodePoints | |
import Debug as Debug | |
import Effect (Effect) | |
import Effect.Exception as Exception | |
import Effect.Now as Now | |
import Effect.Timer as Timer | |
import React.Basic.DOM as R | |
import React.Basic.Hooks (Component, (/\)) | |
import React.Basic.Hooks as Hooks | |
import Web.HTML as HTML | |
import Web.HTML.HTMLDocument as HTMLDocument | |
import Web.HTML.HTMLElement as HTMLElement | |
import Web.HTML.Window as Window | |
main :: Effect Unit | |
main = do | |
maybeBody <- | |
HTML.window | |
>>= Window.document | |
>>= HTMLDocument.body | |
case maybeBody of | |
Nothing -> Exception.throw "Could not find body" | |
Just body -> do | |
app <- mkApp | |
R.render | |
(app unit) | |
(HTMLElement.toElement body) | |
mkApp :: Component Unit | |
mkApp = Hooks.component "App" \_ -> Hooks.do | |
thisDay /\ setThisDay <- Hooks.useState (Nothing :: Maybe Date) | |
let | |
checkDateChange = do | |
today <- Just <$> Now.nowDate | |
setThisDay \thisDay' -> | |
Debug.trace ("thisDay=" <> (show thisDay') <> ", nowDate=" <> (show today)) | |
\_ -> if today /= thisDay' then today else thisDay' | |
Hooks.useEffectOnce do | |
checkDateChange | |
timerId <- Timer.setInterval 1000 $ checkDateChange | |
pure (Timer.clearInterval timerId) | |
let | |
format2 = _.after <<< (\s -> CodePoints.splitAt (CodePoints.length s - 2) s) <<< ("00" <> _) <<< show | |
todayStr = Maybe.fromMaybe "2021-12-31" | |
$ | |
( \d -> (show $ Enum.fromEnum $ Date.year d) | |
<> "-" | |
<> (format2 $ Enum.fromEnum $ Date.month d) | |
<> "-" | |
<> (format2 $ Enum.fromEnum $ Date.day d) | |
) | |
<$> thisDay | |
pure $ R.div | |
{ children: | |
[ R.input | |
{ type: "date" | |
, id: "valueDate" | |
, value: Debug.trace todayStr \_ -> todayStr | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment