|
| 1 | +module ClockReactHooks.Main where |
| 2 | + |
| 3 | +import Prelude |
| 4 | +import Data.JSDate (JSDate) |
| 5 | +import Data.JSDate as JSDate |
| 6 | +import Data.Maybe (Maybe(..)) |
| 7 | +import Data.Newtype (class Newtype) |
| 8 | +import Effect (Effect) |
| 9 | +import Effect.Exception (throw) |
| 10 | +import Effect.Timer (clearInterval, setInterval) |
| 11 | +import Math (cos, sin, tau) |
| 12 | +import React.Basic.DOM (render) |
| 13 | +import React.Basic.DOM.SVG as SVG |
| 14 | +import React.Basic.Hooks (Component, Hook, JSX, UseEffect, UseState, coerceHook, component, useEffectOnce, useState', (/\)) |
| 15 | +import React.Basic.Hooks as React |
| 16 | +import Web.DOM.NonElementParentNode (getElementById) |
| 17 | +import Web.HTML (window) |
| 18 | +import Web.HTML.HTMLDocument (toNonElementParentNode) |
| 19 | +import Web.HTML.Window (document) |
| 20 | + |
| 21 | +type Time |
| 22 | + = { hours :: Number, minutes :: Number, seconds :: Number } |
| 23 | + |
| 24 | +main :: Effect Unit |
| 25 | +main = do |
| 26 | + container <- getElementById "root" =<< map toNonElementParentNode (document =<< window) |
| 27 | + case container of |
| 28 | + Nothing -> throw "Root element not found." |
| 29 | + Just c -> do |
| 30 | + clock <- mkClock |
| 31 | + render (clock {}) c |
| 32 | + |
| 33 | +mkClock :: Component {} |
| 34 | +mkClock = do |
| 35 | + now <- JSDate.now >>= getTime |
| 36 | + component "Clock" \_ -> React.do |
| 37 | + { hours, minutes, seconds } <- useCurrentTime now |
| 38 | + pure |
| 39 | + $ SVG.svg |
| 40 | + { viewBox: "0 0 400 400" |
| 41 | + , width: "400" |
| 42 | + , height: "400" |
| 43 | + , children: |
| 44 | + [ SVG.circle { cx: "200", cy: "200", r: "120", fill: "#1293D8" } |
| 45 | + , hand 6 60.0 (hours / 12.0) |
| 46 | + , hand 6 90.0 (minutes / 60.0) |
| 47 | + , hand 3 90.0 (seconds / 60.0) |
| 48 | + ] |
| 49 | + } |
| 50 | + |
| 51 | +hand :: Int -> Number -> Number -> JSX |
| 52 | +hand width length turns = |
| 53 | + let |
| 54 | + t = tau * (turns - 0.25) |
| 55 | + |
| 56 | + x = 200.0 + length * cos t |
| 57 | + |
| 58 | + y = 200.0 + length * sin t |
| 59 | + in |
| 60 | + SVG.line |
| 61 | + { x1: "200" |
| 62 | + , y1: "200" |
| 63 | + , x2: show x |
| 64 | + , y2: show y |
| 65 | + , stroke: "white" |
| 66 | + , strokeWidth: show width |
| 67 | + , strokeLinecap: "round" |
| 68 | + } |
| 69 | + |
| 70 | +newtype UseCurrentTime hooks |
| 71 | + = UseCurrentTime (UseEffect Unit (UseState Time hooks)) |
| 72 | + |
| 73 | +derive instance ntUseCurrentTime :: Newtype (UseCurrentTime hooks) _ |
| 74 | + |
| 75 | +useCurrentTime :: Time -> Hook UseCurrentTime Time |
| 76 | +useCurrentTime initialTime = |
| 77 | + coerceHook React.do |
| 78 | + currentTime /\ setCurrentTime <- useState' initialTime |
| 79 | + useEffectOnce do |
| 80 | + intervalId <- setInterval 1000 (JSDate.now >>= getTime >>= setCurrentTime) |
| 81 | + pure (clearInterval intervalId) |
| 82 | + pure currentTime |
| 83 | + |
| 84 | +getTime :: JSDate -> Effect Time |
| 85 | +getTime date = ado |
| 86 | + hours <- JSDate.getHours date |
| 87 | + minutes <- JSDate.getMinutes date |
| 88 | + seconds <- JSDate.getSeconds date |
| 89 | + in { hours, minutes, seconds } |
0 commit comments