git.iko.soy/purestack-dev/todomvc
on a4c41d9 help sign in
3 files changed+16−29
since splitsnapshots
unifiedsplit
flake.nix+1−1 src/Frontend.purs+13−15 src/Plum.purs+2−13
flake.nix+1−1
@@ -39,7 +39,7 @@
3939 project-m36-rest.packages.${system}.default
4040 }/bin/project-m36-rest";
4141 proxy.command =
42− "${pkgs.reproxy}/bin/reproxy -l=localhost:3001 --static.enabled --static.rule='*,^/api/(.*),http://localhost:3000/$$1' --assets.location=. --assets.spa --logger.stdout";
42+ "${pkgs.reproxy}/bin/reproxy -l=localhost:3001 --static.enabled --static.rule='*,^/api/(.*),http://localhost:3000/$1' --assets.location=. --assets.spa";
4343 };
4444 });
4545 in {
src/Frontend.purs+13−15
@@ -7,36 +7,30 @@ import Prim
77
88import Data.Array ((:))
99import Data.Foldable (for_)
10+import Data.Maybe (Maybe(..))
1011import Data.URL as URL
1112import Effect (Effect)
1213import Plum as Plum
1314import PureStack.Client (client)
15+import Unsafe.Coerce (unsafeCoerce)
1416
15−type Model = { newTodo :: String, todos :: Array TodoItem }
17+type Model = { newTodo :: String, todos :: Array String }
1618
1719data Msg
1820 = UpdateNewTodo String
19− | SetTodos (Array TodoItem)
2021 | AddNewTodo
2122 | TodoAdded
2223
2324main :: Effect Unit
2425main = do
25− origin <- Plum.currentOrigin
26− let api = getApi (origin `URL.addSegment` "api")
2726 Plum.run @Model @Msg "plum"
28− { init: \runMsg -> do
29− runMsg $ SetTodos <$> api.getTodos
30− pure { newTodo: "", todos: [] }
27+ { init: \_runMsg -> pure { newTodo: "", todos: [] }
3128 , update: \runMsg msg model -> case msg of
3229 UpdateNewTodo newTodo -> pure model { newTodo = newTodo }
3330 AddNewTodo -> do
3431 runMsg $ api.createTodo (TodoItem { text: model.newTodo, done: false }) $> TodoAdded
35− pure model
36− TodoAdded -> do
37− runMsg $ SetTodos <$> api.getTodos
38− pure model
39− SetTodos todos -> pure model { todos = todos }
32+ pure model { newTodo = "", todos = model.newTodo : model.todos }
33+ TodoAdded -> pure model
4034 , view: \model -> column do
4135 font "sans-serif"
4236
@@ -52,9 +46,13 @@ main = do
5246
5347 onKey "Enter" AddNewTodo
5448
55− for_ model.todos $ \(TodoItem todo) -> do
56− text todo.text mempty
49+ for_ model.todos $ \todo -> do
50+ text todo mempty
5751 }
5852
59−getApi = client @API
53+url = case URL.fromString "http://localhost" of
54+ Just x -> x
55+ Nothing -> unsafeCoerce unit
56+
57+api = client @API url
6058
src/Plum.purs+2−13
@@ -1,16 +1,13 @@
1−module Plum (Plum, run, currentOrigin) where
1+module Plum (Plum, run) where
22
33import Prelude
44
55import Data.Array as Array
66import Data.Maybe (Maybe(..))
7−import Data.URL (URL)
8−import Data.URL as URL
97import Effect (Effect)
108import Effect.Aff (Aff, launchAff_)
119import Effect.Class (liftEffect)
1210import Effect.Console (log)
13−import Effect.Exception (throw)
1411import Effect.Ref as Ref
1512import Foreign.Object as Object
1613import Literals.Undefined (undefined)
@@ -20,10 +17,8 @@ import Unsafe.Coerce (unsafeCoerce)
2017import Untagged.Union (asOneOf)
2118import Web.DOM.NonElementParentNode (getElementById) as Web
2219import Web.HTML (window) as Web
23−import Web.HTML.Event.EventTypes (invalid)
2420import Web.HTML.HTMLDocument (toNonElementParentNode) as Web
25−import Web.HTML.Location as Location
26−import Web.HTML.Window (document, location) as Web
21+import Web.HTML.Window (document) as Web
2722
2823type Plum msg model =
2924 { init :: (Aff msg -> Effect Unit) -> Effect model
@@ -113,9 +108,3 @@ run id plum = do
113108outerStyle :: String
114109outerStyle = "html,body{height:100%;padding:0;margin:0;}"
115110
116−currentOrigin :: Effect URL
117−currentOrigin = do
118− s <- Web.window >>= Web.location >>= Location.origin
119− case URL.fromString s of
120− Nothing -> throw "window.location.origin returned an invalid url"
121− Just url -> pure url