Skip to content

Commit 64a3154

Browse files
authored
Update Halogen and React cards recipes (#143)
1 parent 94193ef commit 64a3154

File tree

6 files changed

+67
-57
lines changed

6 files changed

+67
-57
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Running a web-compatible recipe:
6969
| | :heavy_check_mark: | [BookReact](recipes/BookReact) | A React port of the ["HTTP - Book" Elm Example](https://elm-lang.org/examples/book). |
7070
| | :heavy_check_mark: | [ButtonsHalogen](recipes/ButtonsHalogen) | A Halogen port of the ["User Input - Buttons" Elm Example](https://elm-lang.org/examples). |
7171
| | :heavy_check_mark: | [ButtonsReact](recipes/ButtonsReact) | A React port of the ["User Input - Buttons" Elm Example](https://elm-lang.org/examples/buttons). |
72-
| | :heavy_check_mark: | [CardsHalogen](recipes/CardsHalogen) | A Halogen port of the ["Random - Cards" Elm Example](https://elm-lang.org/examples). |
72+
| | :heavy_check_mark: | [CardsHalogen](recipes/CardsHalogen) | A Halogen port of the ["Random - Cards" Elm Example](https://elm-lang.org/examples/cards). |
7373
| | :heavy_check_mark: | [CardsReact](recipes/CardsReact) | A React port of the ["Random - Cards" Elm Example](https://elm-lang.org/examples/cards). |
7474
| | :heavy_check_mark: | [CatGifsHalogen](recipes/CatGifsHalogen) | A Halogen port of the ["HTTP - Cat GIFs" Elm Example](https://elm-lang.org/examples). |
7575
| | :heavy_check_mark: | [CatGifsReact](recipes/CatGifsReact) | A React port of the ["HTTP - Cat GIFs" Elm Example](https://elm-lang.org/examples/cat-gifs). |

recipes/CardsHalogen/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# CardsHalogen
22

3-
A Halogen port of the ["Random - Cards" Elm Example](https://elm-lang.org/examples).
3+
A Halogen port of the ["Random - Cards" Elm Example](https://elm-lang.org/examples/cards).
44

55
## Expected Behavior:
66

77
### Browser
88

9-
If the user clicks the "Draw" button, a random card will be displayed. Note: the Elm example uses a uniform generator whereas this example does not because PureScript does not have an out-of-box uniform generator.
9+
When the user clicks the "Draw" button, a random card is drawn from a deck of 13 spades and displayed.

recipes/CardsHalogen/spago.dhall

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
, "effect"
55
, "halogen-css"
66
, "halogen-hooks"
7+
, "nonempty"
78
, "psci-support"
8-
, "random"
9+
, "quickcheck"
910
]
1011
, packages = ../../packages.dhall
1112
, sources = [ "recipes/CardsHalogen/src/**/*.purs" ]

recipes/CardsHalogen/src/Main.purs

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ import Prelude
44

55
import CSS (fontSize, em)
66
import Data.Maybe (Maybe(..))
7+
import Data.NonEmpty ((:|))
78
import Data.Tuple.Nested ((/\))
89
import Effect (Effect)
910
import Effect.Class (class MonadEffect)
10-
import Effect.Random (randomInt)
11-
import Halogen (liftEffect)
1211
import Halogen as H
1312
import Halogen.Aff as HA
1413
import Halogen.HTML as HH
15-
import Halogen.HTML.Events as HE
1614
import Halogen.HTML.CSS as HC
15+
import Halogen.HTML.Events as HE
1716
import Halogen.Hooks as Hooks
1817
import Halogen.VDom.Driver (runUI)
18+
import Test.QuickCheck (mkSeed)
19+
import Test.QuickCheck.Gen (Gen, elements, runGen)
1920

2021
main :: Effect Unit
2122
main =
@@ -28,23 +29,25 @@ hookComponent
2829
. MonadEffect anyMonad
2930
=> H.Component HH.HTML unusedQuery unusedInput unusedOutput anyMonad
3031
hookComponent = Hooks.component \_ _ -> Hooks.do
31-
card /\ cardIdx <- Hooks.useState Three
32+
let
33+
initialGenState = { newSeed: mkSeed 3, size: 1 }
34+
-- "Card state" is a tuple of the card and generator state.
35+
-- We don't need the actual generator state for rendering, so we're ignoring it with `_`.
36+
(card /\ _) /\ cardStateIdx <- Hooks.useState (runGen cardGenerator initialGenState)
3237
Hooks.pure $
3338
HH.div_
3439
[ HH.button
3540
[ HE.onClick \_ -> Just do
36-
-- Elm uses a uniform generator
37-
-- PureScript doesn't have an out-of-box version of that,
38-
-- so we'll just use a random value.
39-
i <- liftEffect $ randomInt 1 13
40-
Hooks.put cardIdx $ intToCard i
41+
-- Modify the card generator state by re-running the generator.
42+
-- We don't need the card value for this update function, so it is ignored with `_`.
43+
Hooks.modify_ cardStateIdx \(_ /\ genState) -> runGen cardGenerator genState
4144
]
4245
[ HH.text "Draw" ]
4346
, HH.div
4447
[ HC.style do
4548
fontSize $ em 12.0
4649
]
47-
[ HH.text $ renderCard card ]
50+
[ HH.text $ viewCard card ]
4851
]
4952

5053
data Card
@@ -62,24 +65,26 @@ data Card
6265
| Queen
6366
| King
6467

65-
intToCard :: Int -> Card
66-
intToCard = case _ of
67-
1 -> Ace
68-
2 -> Two
69-
3 -> Three
70-
4 -> Four
71-
5 -> Five
72-
6 -> Six
73-
7 -> Seven
74-
8 -> Eight
75-
9 -> Nine
76-
10 -> Ten
77-
11 -> Jack
78-
12 -> Queen
79-
_ -> King
68+
cardGenerator :: Gen Card
69+
cardGenerator =
70+
elements
71+
$ Ace
72+
:| [ Two
73+
, Three
74+
, Four
75+
, Five
76+
, Six
77+
, Seven
78+
, Eight
79+
, Nine
80+
, Ten
81+
, Jack
82+
, Queen
83+
, King
84+
]
8085

81-
renderCard :: Card -> String
82-
renderCard = case _ of
86+
viewCard :: Card -> String
87+
viewCard = case _ of
8388
Ace -> "🂡"
8489
Two -> "🂢"
8590
Three -> "🂣"

recipes/CardsReact/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A React port of the ["Random - Cards" Elm Example](https://elm-lang.org/examples
66

77
### Browser
88

9-
If the user clicks the "Draw" button, a random card will be drawn from a deck of 13 spades and displayed.
9+
When the user clicks the "Draw" button, a random card is drawn from a deck of 13 spades and displayed.
1010

1111
## Dependencies Used:
1212

recipes/CardsReact/src/Main.purs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,35 @@ import Web.HTML (window)
1717
import Web.HTML.HTMLDocument (toNonElementParentNode)
1818
import Web.HTML.Window (document)
1919

20+
main :: Effect Unit
21+
main = do
22+
container <- getElementById "root" =<< map toNonElementParentNode (document =<< window)
23+
case container of
24+
Nothing -> throw "Root element not found."
25+
Just c -> do
26+
cardsComponent <- mkCardsComponent
27+
render (cardsComponent {}) c
28+
29+
mkCardsComponent :: Component {}
30+
mkCardsComponent = do
31+
let
32+
initialGenState = { newSeed: mkSeed 3, size: 1 }
33+
component "Cards" \_ -> React.do
34+
-- "Card state" is a tuple of the card and generator state.
35+
-- We don't need the actual generator state for rendering, so we're ignoring it with `_`.
36+
(card /\ _) /\ setCardState <- useState (runGen cardGenerator initialGenState)
37+
let
38+
onClick =
39+
handler_ do
40+
-- Modify the card generator state by re-running the generator.
41+
-- We don't need the card value for this update function, so it is ignored with `_`.
42+
setCardState \(_ /\ genState) -> runGen cardGenerator genState
43+
pure
44+
$ R.div_
45+
[ R.button { onClick, children: [ R.text "Draw" ] }
46+
, R.div { style: css { fontSize: "12em" }, children: [ R.text (viewCard card) ] }
47+
]
48+
2049
data Card
2150
= Ace
2251
| Two
@@ -50,31 +79,6 @@ cardGenerator =
5079
, King
5180
]
5281

53-
main :: Effect Unit
54-
main = do
55-
container <- getElementById "root" =<< map toNonElementParentNode (document =<< window)
56-
case container of
57-
Nothing -> throw "Root element not found."
58-
Just c -> do
59-
cardsComponent <- mkCardsComponent
60-
render (cardsComponent {}) c
61-
62-
mkCardsComponent :: Component {}
63-
mkCardsComponent = do
64-
let
65-
initialGenState = { newSeed: mkSeed 3, size: 1 }
66-
component "Cards" \_ -> React.do
67-
(card /\ _) /\ setCardState <- useState (runGen cardGenerator initialGenState)
68-
let
69-
onClick =
70-
handler_ do
71-
setCardState \(_ /\ genState) -> runGen cardGenerator genState
72-
pure
73-
$ R.div_
74-
[ R.button { onClick, children: [ R.text "Draw" ] }
75-
, R.div { style: css { fontSize: "12em" }, children: [ R.text (viewCard card) ] }
76-
]
77-
7882
viewCard :: Card -> String
7983
viewCard = case _ of
8084
Ace -> "🂡"

0 commit comments

Comments
 (0)