Skip to content
This repository was archived by the owner on Mar 18, 2021. It is now read-only.

Commit 0e0105c

Browse files
Merge pull request #45 from JordanMartinez/development
Inline scaffolding code into each file
2 parents 951f7c3 + 8c8936a commit 0e0105c

36 files changed

+1618
-1474
lines changed

packages.dhall

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,9 @@ let mkPackage =
112112
https://raw.githubusercontent.com/purescript/package-sets/psc-0.13.0-20190626/src/mkPackage.dhall sha256:0b197efa1d397ace6eb46b243ff2d73a3da5638d8d0ac8473e8e4a8fc528cf57
113113

114114
let upstream =
115-
https://github.com/purescript/package-sets/releases/download/psc-0.13.3-20190818/packages.dhall sha256:c95c4a8b8033a48a350106b759179f68a695c7ea2208228c522866fd43814dc8
115+
https://github.com/purescript/package-sets/releases/download/psc-0.13.3-20190831/packages.dhall sha256:852cd4b9e463258baf4e253e8524bcfe019124769472ca50b316fe93217c3a47
116116

117-
let overrides =
118-
{ halogen =
119-
upstream.halogen // { version = "v5.0.0-rc.6" }
120-
, halogen-vdom =
121-
upstream.halogen-vdom // { version = "v6.1.0" }
122-
}
117+
let overrides = {=}
123118

124119
let additions = {=}
125120

src/01-Static-HTML/02-Static-HTML.purs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
module StaticHTML.StaticHTML where
22

3+
-- Imports for lesson
4+
import Halogen.HTML as HH
5+
6+
-- Imports for scaffolding
37
import Prelude
48

9+
import Data.Const (Const)
510
import Effect (Effect)
6-
import Halogen.HTML as HH
7-
import Scaffolding.StaticRenderer (runStaticHtml, StaticHTML)
8-
9-
main :: Effect Unit
10-
main = runStaticHtml staticHtml
11+
import Effect.Aff (Aff, launchAff_)
12+
import Halogen as H
13+
import Halogen.Aff (awaitBody)
14+
import Halogen.VDom.Driver (runUI)
1115

1216
-- | Shows how to use Halogen VDOM DSL to render HTML without properties or CSS
1317
staticHtml :: StaticHTML
@@ -22,3 +26,24 @@ staticHtml =
2226
, HH.button_
2327
[ HH.text "You can click me, but I don't do anything." ]
2428
]
29+
30+
--- Scaffolded code below ---
31+
32+
main :: Effect Unit
33+
main = launchAff_ do
34+
body <- awaitBody
35+
runUI (staticComponent staticHtml) unit body
36+
37+
-- | HTML written in Purescript via Halogen's HTML DSL
38+
-- | that is always rendered the same and does not include any event handling.
39+
type StaticHTML = H.ComponentHTML Unit () Aff
40+
41+
-- | Wraps Halogen types cleanly, so that one gets very clear compiler errors
42+
staticComponent :: StaticHTML
43+
-> H.Component HH.HTML (Const Unit) Unit Void Aff
44+
staticComponent renderHtml =
45+
H.mkComponent
46+
{ initialState: const unit
47+
, render: \_ -> renderHtml
48+
, eval: H.mkEval H.defaultEval
49+
}

src/01-Static-HTML/04-Adding-Properties.purs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ module StaticHTML.AddingProperties where
22

33
import Prelude
44

5-
import Effect (Effect)
5+
-- Imports for lesson
66
import Halogen.HTML (ClassName(..))
77
import Halogen.HTML as HH
88
import Halogen.HTML.Properties (ButtonType(..))
99
import Halogen.HTML.Properties as HP
10-
import Scaffolding.StaticRenderer (runStaticHtml, StaticHTML)
1110

12-
main :: Effect Unit
13-
main = runStaticHtml staticHtmlWithProps
11+
-- Imports for scaffolding
12+
import Data.Const (Const)
13+
import Effect (Effect)
14+
import Effect.Aff (Aff, launchAff_)
15+
import Halogen as H
16+
import Halogen.Aff (awaitBody)
17+
import Halogen.VDom.Driver (runUI)
1418

1519
-- | Shows how to use Halogen VDOM DSL to render static HTML
1620
-- | that also includes properties
@@ -28,3 +32,24 @@ staticHtmlWithProps =
2832
[ HP.type_ ButtonButton ]
2933
[ HH.text "You can click me, but I don't do anything." ]
3034
]
35+
36+
--- Scaffolded code below ---
37+
38+
main :: Effect Unit
39+
main = launchAff_ do
40+
body <- awaitBody
41+
runUI (staticComponent staticHtmlWithProps) unit body
42+
43+
-- | HTML written in Purescript via Halogen's HTML DSL
44+
-- | that is always rendered the same and does not include any event handling.
45+
type StaticHTML = H.ComponentHTML Unit () Aff
46+
47+
-- | Wraps Halogen types cleanly, so that one gets very clear compiler errors
48+
staticComponent :: StaticHTML
49+
-> H.Component HH.HTML (Const Unit) Unit Void Aff
50+
staticComponent renderHtml =
51+
H.mkComponent
52+
{ initialState: const unit
53+
, render: \_ -> renderHtml
54+
, eval: H.mkEval H.defaultEval
55+
}

src/01-Static-HTML/06-Adding-CSS.purs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ module StaticHTML.AddingCSS where
22

33
import Prelude
44

5+
-- Imports for lesson
56
import CSS (backgroundColor, fontSize, orange, px, red)
6-
import Effect (Effect)
77
import Halogen.HTML (ClassName(..))
88
import Halogen.HTML as HH
99
import Halogen.HTML.CSS as CSS
1010
import Halogen.HTML.Properties (ButtonType(..))
1111
import Halogen.HTML.Properties as HP
12-
import Scaffolding.StaticRenderer (runStaticHtml, StaticHTML)
1312

14-
main :: Effect Unit
15-
main = runStaticHtml staticHtmlWithPropsAndCss
13+
-- Imports for scaffolding
14+
import Data.Const (Const)
15+
import Effect (Effect)
16+
import Effect.Aff (Aff, launchAff_)
17+
import Halogen as H
18+
import Halogen.Aff (awaitBody)
19+
import Halogen.VDom.Driver (runUI)
1620

1721
-- | Shows how to use Halogen VDOM DSL to render static HTML
1822
-- | that also includes properties and CSS
@@ -34,3 +38,24 @@ staticHtmlWithPropsAndCss =
3438
[ HP.type_ ButtonButton ]
3539
[ HH.text "You can click me, but I don't do anything." ]
3640
]
41+
42+
--- Scaffolded code below ---
43+
44+
main :: Effect Unit
45+
main = launchAff_ do
46+
body <- awaitBody
47+
runUI (staticComponent staticHtmlWithPropsAndCss) unit body
48+
49+
-- | HTML written in Purescript via Halogen's HTML DSL
50+
-- | that is always rendered the same and does not include any event handling.
51+
type StaticHTML = H.ComponentHTML Unit () Aff
52+
53+
-- | Wraps Halogen types cleanly, so that one gets very clear compiler errors
54+
staticComponent :: StaticHTML
55+
-> H.Component HH.HTML (Const Unit) Unit Void Aff
56+
staticComponent renderHtml =
57+
H.mkComponent
58+
{ initialState: const unit
59+
, render: \_ -> renderHtml
60+
, eval: H.mkEval H.defaultEval
61+
}

src/02-Dynamic-HTML/02-Adding-State.purs

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,21 @@ module DynamicHtml.AddingState where
22

33
import Prelude
44

5-
import Effect (Effect)
5+
-- Imports for lesson
66
import Halogen.HTML as HH
7-
import Scaffolding.DynamicRenderer.StateOnly (runStateOnlyDynamicRenderer)
8-
import Scaffolding.StaticRenderer (StaticHTML)
7+
8+
-- Imports for scaffolding
9+
import Control.Monad.Error.Class (throwError)
10+
import Data.Const (Const)
11+
import Data.Maybe (maybe)
12+
import Effect (Effect)
13+
import Effect.Aff (Aff, launchAff_)
14+
import Effect.Exception (error)
15+
import Halogen as H
16+
import Halogen.Aff (awaitLoad, selectElement)
17+
import Halogen.VDom.Driver (runUI)
18+
import Web.DOM.ParentNode (QuerySelector(..))
19+
import Web.HTML (HTMLElement)
920

1021
main :: Effect Unit
1122
main =
@@ -18,3 +29,50 @@ main =
1829
simpleIntState :: Int -> StaticHTML
1930
simpleIntState state =
2031
HH.div_ [ HH.text $ "The state was: " <> show state ]
32+
33+
-- Scaffolded code below --
34+
35+
-- | HTML written in Purescript via Halogen's HTML DSL
36+
-- | that is always rendered the same and does not include any event handling.
37+
type StaticHTML = H.ComponentHTML Unit () Aff
38+
39+
-- | A function that uses the `state` type's value to render HTML
40+
-- | with no event-handling.
41+
type StateOnlyDynamicRenderer state = (state -> StaticHTML)
42+
43+
-- | Uses the `state` type's value to render dynamic HTML
44+
-- | using 3 different state values.
45+
runStateOnlyDynamicRenderer :: forall state.
46+
state
47+
-> state
48+
-> state
49+
-> StateOnlyDynamicRenderer state
50+
-> Effect Unit
51+
runStateOnlyDynamicRenderer firstState secondState thirdState rendererFunction =
52+
launchAff_ do
53+
awaitLoad
54+
55+
div1 <- selectElement' "could not find 'div#first'" $ QuerySelector "#first"
56+
div2 <- selectElement' "could not find 'div#second'" $ QuerySelector "#second"
57+
div3 <- selectElement' "could not find 'div#third'" $ QuerySelector "#third"
58+
59+
void $ runUI (stateOnlyStaticComponent firstState rendererFunction) unit div1
60+
void $ runUI (stateOnlyStaticComponent secondState rendererFunction) unit div2
61+
void $ runUI (stateOnlyStaticComponent thirdState rendererFunction) unit div3
62+
63+
-- | Wraps Halogen types cleanly, so that one gets very clear compiler errors
64+
stateOnlyStaticComponent :: forall state.
65+
state
66+
-> StateOnlyDynamicRenderer state
67+
-> H.Component HH.HTML (Const Unit) Unit Void Aff
68+
stateOnlyStaticComponent state dynamicRenderer =
69+
H.mkComponent
70+
{ initialState: const state
71+
, render: dynamicRenderer
72+
, eval: H.mkEval H.defaultEval
73+
}
74+
75+
selectElement' :: String -> QuerySelector -> Aff HTMLElement
76+
selectElement' errorMessage query = do
77+
maybeElem <- selectElement query
78+
maybe (throwError (error errorMessage)) pure maybeElem

src/02-Dynamic-HTML/05-Adding-Event-Handling.purs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@ module DynamicHtml.AddingEventHandling where
22

33
import Prelude
44

5+
-- Imports for lesson
56
import Control.Monad.State (get, put)
67
import Data.Maybe (Maybe(..))
7-
import Effect (Effect)
88
import Halogen.HTML as HH
99
import Halogen.HTML.Events as HE
10-
import Scaffolding.DynamicRenderer.StateAndEval (HandleSimpleAction, StateAndActionRenderer, runStateAndActionComponent)
10+
11+
-- Imports for scaffolding
12+
import Data.Const (Const)
13+
import Effect (Effect)
14+
import Effect.Aff (Aff, launchAff_)
15+
import Halogen (ComponentHTML)
16+
import Halogen as H
17+
import Halogen.Aff (awaitBody)
18+
import Halogen.VDom.Driver (runUI)
1119

1220
-- | Our state type. Either the button is 'on' or 'off'.
1321
type State = Boolean
@@ -45,3 +53,47 @@ main =
4553
, render: toggleButton
4654
, handleAction: handleAction
4755
}
56+
57+
-- Scaffolded Code --
58+
59+
-- | Renders HTML that can respond to events by translating them
60+
-- | into a value of the `action` that one uses to handle the event.
61+
type DynamicHtml action = ComponentHTML action () Aff
62+
63+
-- | A function that uses the `state` type's value to render HTML
64+
-- | with simple event-handling via the `action` type.
65+
type StateAndActionRenderer state action = (state -> DynamicHtml action)
66+
67+
-- | When an `action` type's value is received, this function
68+
-- | determines how to update the component (e.g. state updates).
69+
type HandleSimpleAction state action =
70+
(action -> H.HalogenM state action () Void Aff Unit)
71+
72+
-- | Combines all the code we need to define a simple componenet that supports
73+
-- | state and simple event handling
74+
type SimpleChildComponent state action =
75+
{ initialState :: state
76+
, render :: StateAndActionRenderer state action
77+
, handleAction :: HandleSimpleAction state action
78+
}
79+
80+
-- | Uses the `state` type's value to render dynamic HTML
81+
-- | with event handling via the `action` type.
82+
runStateAndActionComponent :: forall state action.
83+
SimpleChildComponent state action
84+
-> Effect Unit
85+
runStateAndActionComponent childSpec = do
86+
launchAff_ do
87+
body <- awaitBody
88+
runUI (stateAndActionCompontent childSpec) unit body
89+
90+
-- | Wraps Halogen types cleanly, so that one gets very clear compiler errors
91+
stateAndActionCompontent :: forall state action.
92+
SimpleChildComponent state action
93+
-> H.Component HH.HTML (Const Unit) Unit Void Aff
94+
stateAndActionCompontent spec =
95+
H.mkComponent
96+
{ initialState: const spec.initialState
97+
, render: spec.render
98+
, eval: H.mkEval $ H.defaultEval { handleAction = spec.handleAction }
99+
}

0 commit comments

Comments
 (0)