Skip to content

Commit b4e66e4

Browse files
committed
Fix basic example
1 parent f5021a4 commit b4e66e4

File tree

8 files changed

+580
-44
lines changed

8 files changed

+580
-44
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
/.vscode/
1111
/.spago/
1212
/output-pulp/
13+
.psc-*

examples/basic/Makefile

Lines changed: 0 additions & 8 deletions
This file was deleted.

examples/basic/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
## Building
44

5+
You can build this example from the root of the `purescript-react-dnd-basic` project:
6+
57
```sh
68
npm install
7-
make all
9+
npm run example:basic
810
```
911

10-
This will compile the PureScript source files, bundle them, and use Browserify to combine PureScript and NPM sources into a single bundle.
12+
This will compile the PureScript source files, bundle them, and combine PureScript and NPM sources into a single bundle.
1113

1214
Then open `html/index.html` in your browser.

examples/basic/package.json

Lines changed: 0 additions & 17 deletions
This file was deleted.

examples/basic/spago.dhall

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
let config = ./../../spago.dhall
2+
3+
in config
4+
// { sources = config.sources # [ "examples/basic/**/*.purs" ]
5+
, dependencies =
6+
config.dependencies
7+
# [ "arrays"
8+
, "exceptions"
9+
, "foldable-traversable"
10+
, "integers"
11+
, "react-basic"
12+
, "react-basic-dom"
13+
, "web-html"
14+
]
15+
}

examples/basic/src/Basic.purs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1-
module Basic where
1+
module Example.Basic where
22

33
import Prelude
44
import Data.Array ((!!), drop, mapWithIndex, take)
55
import Data.Foldable (traverse_)
66
import Data.Int as Int
7-
import Data.Maybe (fromMaybe, maybe)
7+
import Data.Maybe (fromMaybe, maybe, Maybe(..))
88
import Effect (Effect)
9+
import Effect.Class.Console as Console
10+
import Effect.Exception (throw)
11+
import React.Basic.DOM (render)
912
import React.Basic.DOM as R
1013
import React.Basic.DOM.Events (targetChecked)
1114
import React.Basic.Events as Events
1215
import React.Basic.Hooks (Component, component, fragment, mkReducer, useReducer, (/\))
1316
import React.Basic.Hooks as React
1417
import React.Basic.ReactDND (dndProvider, mergeTargets, useDrag, useDrop)
1518
import React.Basic.ReactDND.Backends.HTML5Backend (html5Backend)
19+
import Web.DOM.NonElementParentNode (getElementById)
20+
import Web.HTML (window)
21+
import Web.HTML.HTMLDocument (toNonElementParentNode)
22+
import Web.HTML.Window (document)
1623

1724
data Action
1825
= Move { from :: Int, to :: Int }
1926
| SetDone String Boolean
2027

21-
type Todo
22-
= { id :: String, text :: String, done :: Boolean }
28+
type Todo = { id :: String, text :: String, done :: Boolean }
2329

2430
mkTodoExample :: Component Unit
2531
mkTodoExample = do
@@ -34,7 +40,7 @@ mkTodoExample = do
3440
, R.p_ [ R.text "Drag to reorder the list:" ]
3541
, R.section_
3642
$ state.todos
37-
# mapWithIndex \index t -> todo { index, todo: t, dispatch }
43+
# mapWithIndex \index t -> todo { index, todo: t, dispatch }
3844
]
3945
where
4046
initialState =
@@ -53,20 +59,20 @@ mkTodoExample = do
5359
SetDone id done ->
5460
state
5561
{ todos =
56-
state.todos
57-
<#> \t ->
62+
state.todos
63+
<#> \t ->
5864
if t.id == id then
5965
t { done = done }
6066
else
6167
t
6268
}
6369

64-
mkTodo ::
65-
Component
66-
{ index :: Int
67-
, todo :: Todo
68-
, dispatch :: Action -> Effect Unit
69-
}
70+
mkTodo
71+
:: Component
72+
{ index :: Int
73+
, todo :: Todo
74+
, dispatch :: Action -> Effect Unit
75+
}
7076
mkTodo = do
7177
let
7278
todoDND = "todo-dnd"
@@ -119,3 +125,15 @@ moveItem fromIndex toIndex items =
119125
take toIndex items'
120126
<> maybe [] pure item
121127
<> drop toIndex items'
128+
129+
main :: Effect Unit
130+
main = do
131+
maybeRoot <- window
132+
>>= document
133+
>>= toNonElementParentNode
134+
>>> getElementById "container"
135+
case maybeRoot of
136+
Nothing -> throw "Root element not found."
137+
Just container -> do
138+
todoExample <- mkTodoExample
139+
render (todoExample unit) container

0 commit comments

Comments
 (0)