-
-
Save Dzoukr/a37cbfe6c567cc2c4aab2c1c2dc68d56 to your computer and use it in GitHub Desktop.
Feliz – MVU with React Function Components
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module App | |
open Elmish | |
type State = | |
{ CurrentUser: string option } | |
type Msg = | |
| SignIn of string | |
| SignOut | |
let init() = { CurrentUser = None }, Cmd.none | |
let update (msg: Msg) (state: State) = | |
match msg with | |
| SignIn user -> { state with CurrentUser = Some (user) }, Cmd.none | |
| SignOut -> { state with CurrentUser = None }, Cmd.none | |
let render (state: State) (dispatch: Msg -> unit) = | |
match state.CurrentUser with | |
| Some -> Dashboard.dashboard { User = state.CurrentUser.Value; SignOut = fun () -> SignOut |> dispatch } | |
| None -> Login.login { SignIn = fun user -> SignIn(user) |> dispatch } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Dashboard | |
open Elmish | |
open Feliz.UseElmish | |
open Feliz | |
type State = | |
{ User: string } | |
type Props = | |
{ User: string | |
SignOut: unit -> unit } | |
type Msg = | SignOut | |
let private init props = | |
{ User = props.User }, Cmd.none | |
let private update props msg state = | |
match msg with | |
| SignOut -> state, Cmd.ofSub (fun _ -> props.SignOut()) | |
let private render props hook = React.functionComponent("Dashboard", fun () -> | |
let (state:State), dispatch = hook() | |
Html.div [ | |
Html.h1 (sprintf "Hallo, %s!" state.User) | |
Html.hr [] | |
Html.button [ | |
prop.text "Sign out" | |
prop.onClick (fun _ -> SignOut |> dispatch) | |
] | |
] | |
) | |
let dashboard props = | |
let hook () = React.useElmish(init props, update props, [||]) | |
(render props hook)() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Login | |
open Feliz | |
open Feliz.UseElmish | |
open Elmish | |
open System | |
type State = | |
{ UserName: string | |
Password: string } | |
type Props = | |
{ SignIn: string -> unit } | |
type Msg = | |
| SetUserName of string | |
| SetPassword of string | |
| SignIn | |
let init() = { UserName = ""; Password = "" }, Cmd.none | |
let update props msg state = | |
match msg with | |
| SetUserName userName -> { state with UserName = userName }, Cmd.none | |
| SetPassword password -> { state with Password = password }, Cmd.none | |
| SignIn -> state, Cmd.ofSub (fun _ -> props.SignIn(state.UserName)) | |
let render props hook = React.functionComponent("LoginForm", fun () -> | |
let state, dispatch = hook() | |
let signInPossible = | |
(not (String.IsNullOrWhiteSpace(state.UserName))) && | |
(not (String.IsNullOrWhiteSpace(state.Password))) | |
Html.div [ | |
Html.label [ prop.text "User name"; prop.htmlFor "user" ] | |
Html.br [] | |
Html.input [ | |
prop.id "user" | |
prop.type'.text | |
prop.onChange (SetUserName >> dispatch) | |
] | |
Html.br [] | |
Html.label [ prop.text "Password"; prop.htmlFor "password" ] | |
Html.br [] | |
Html.input [ | |
prop.id "password" | |
prop.type'.password | |
prop.onChange (SetPassword >> dispatch) | |
] | |
Html.br [] | |
Html.br [] | |
Html.button [ | |
prop.onClick (fun _ -> SignIn |> dispatch) | |
prop.text "Sign in" | |
prop.disabled (not signInPossible) | |
] | |
] | |
) | |
let login props = | |
let hook () = React.useElmish(init, update props, [||]) | |
(render props hook)() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment