Skip to content

Commit

Permalink
Client: Elmish
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacabraham committed Dec 20, 2023
1 parent 0e47e2f commit 1e39a36
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 20 deletions.
20 changes: 15 additions & 5 deletions client/Program.fs
@@ -1,7 +1,17 @@
open Feliz
open Browser
open Feliz.JSX.React
open Elmish
open Elmish.React

let private root = document.getElementById "root" |> ReactDOM.createRoot
#if DEBUG
open Elmish.Debug
open Elmish.HMR
#endif

[ App.MyComponent() |> toReact ] |> React.strictMode |> root.render
Program.mkProgram Ui.init Ui.update Ui.view
#if DEBUG
|> Program.withConsoleTrace
#endif
|> Program.withReactSynchronous "root"
#if DEBUG
|> Program.withDebugger
#endif
|> Program.run
44 changes: 29 additions & 15 deletions client/Ui.fs
@@ -1,28 +1,42 @@
module App
module Ui

open Browser.Dom
open Feliz
open Fable.Core
open Fable.Remoting.Client
open Elmish
open Feliz.JSX.React

let private loadData () =
let api =
Remoting.createApi ()
|> Remoting.withRouteBuilder Route.builder
|> Remoting.buildProxy<IApi>
type Model = string option

async {
let! data = api.Greet()
console.log data
window.alert $"Data received: '{data}'"
}
|> Async.StartImmediate
type Message =
| GetData
| GotData of string

let api =
Remoting.createApi ()
|> Remoting.withRouteBuilder Route.builder
|> Remoting.buildProxy<IApi>

let init () = None, Cmd.none

let update msg model =
match msg with
| GetData -> None, Cmd.OfAsync.perform api.Greet () GotData
| GotData data -> Some data, Cmd.none

[<JSX.Component>]
let MyComponent () =
let MyComponent model dispatch =
match model with
| Some model -> window.alert model
| None -> ()

JSX.jsx
$"""
<button id="btnFetch" onClick={fun _ -> loadData ()}>
<button onClick={fun _ -> dispatch GetData}>
Fetch Data!
</button>
"""
"""
|> toReact

let view model dispatch = MyComponent model dispatch
3 changes: 3 additions & 0 deletions client/client.fsproj
Expand Up @@ -10,6 +10,9 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Fable.Core" Version="4.2.0" />
<PackageReference Include="Fable.Elmish.Debugger" Version="4.0.0" />
<PackageReference Include="Fable.Elmish.HMR" Version="7.0.0" />
<PackageReference Include="Fable.Elmish.React" Version="4.0.0" />
<PackageReference Include="Fable.Remoting.Client" Version="7.29.0" />
<PackageReference Include="Fable.SimpleHttp" Version="3.6.0" />
<PackageReference Include="Feliz" Version="2.7.0" />
Expand Down
4 changes: 4 additions & 0 deletions client/index.html
Expand Up @@ -7,6 +7,10 @@

<body>
<div id="root"></div>
<!-- Polyfill for remotedev -->
<script type="text/javascript">
var global = global || window;
</script>
<script type="module" src="/output/Program.jsx"></script>
</body>

Expand Down

0 comments on commit 1e39a36

Please sign in to comment.