-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathEffectList.purs
executable file
·61 lines (54 loc) · 1.91 KB
/
EffectList.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module Test.Basic.EffectList (mount) where
import Prelude
import Data.Maybe (Maybe(..))
import Data.Maybe as DM
import Data.String as DS
import Data.String.CodeUnits as DSC
import Data.Tuple (Tuple)
import Effect (Effect)
import Effect.Aff (Aff)
import Effect.Class (liftEffect)
import Effect.Random as ER
import Flame (QuerySelector(..), Html, (:>))
import Flame.Application.EffectList as FAE
import Flame.Html.Attribute as HA
import Flame.Html.Element as HE
import Partial.Unsafe as UP
import Web.Event.Event as WEE
import Web.UIEvent.KeyboardEvent as WUK
type Model = String
data Message = Current String | Cut | Submit
update ∷ Model → Message → Tuple Model (Array (Aff (Maybe Message)))
update model = case _ of
Cut → model :>
[ Just <<< Current <$> cut model
]
Submit → "thanks" :> []
Current text → text :> []
where
cut text = do
amount ← liftEffect <<< ER.randomInt 1 $ DSC.length text
pure $ DS.drop amount text
view ∷ Model → Html Message
view model = HE.main_
[ HE.span [ HA.id "text-output" ] model
,
--we add extra events for each input to test if the correct message is used
HE.input [ HA.id "text-input", HA.type' "text", HA.onInput Current, HA.onFocus Cut, onEnterPressed Submit ]
, HE.input [ HA.id "cut-button", HA.type' "button", HA.onClick Cut, HA.onFocus (Current "") ]
]
where
onEnterPressed message = HA.createRawEvent "keypress" $ \event → do
let pressed = WUK.key $ UP.unsafePartial (DM.fromJust $ WUK.fromEvent event)
case pressed of
"Enter" → do
WEE.preventDefault event
pure $ Just message
_ → pure Nothing
mount ∷ Effect Unit
mount = FAE.mount_ (QuerySelector "#mount-point")
{ init: "" :> []
, subscribe: []
, update
, view
}