1- module Basic where
1+ module Example. Basic where
22
33import Prelude
44import Data.Array ((!!), drop , mapWithIndex , take )
55import Data.Foldable (traverse_ )
66import Data.Int as Int
7- import Data.Maybe (fromMaybe , maybe )
7+ import Data.Maybe (fromMaybe , maybe , Maybe (..) )
88import Effect (Effect )
9+ import Effect.Class.Console as Console
10+ import Effect.Exception (throw )
11+ import React.Basic.DOM (render )
912import React.Basic.DOM as R
1013import React.Basic.DOM.Events (targetChecked )
1114import React.Basic.Events as Events
1215import React.Basic.Hooks (Component , component , fragment , mkReducer , useReducer , (/\))
1316import React.Basic.Hooks as React
1417import React.Basic.ReactDND (dndProvider , mergeTargets , useDrag , useDrop )
1518import 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
1724data 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
2430mkTodoExample :: Component Unit
2531mkTodoExample = 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+ }
7076mkTodo = 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