Skip to content

Commit

Permalink
Use UseElmish for Login page
Browse files Browse the repository at this point in the history
  • Loading branch information
Larocceau committed Mar 1, 2024
1 parent 53c2274 commit 046cc03
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
24 changes: 4 additions & 20 deletions src/Client/Index.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ open Shared

type PageTab =
| Home
| Login of Login.Model
| Login
| Wishlist of WishList.Model
| NotFound

Expand All @@ -22,7 +22,6 @@ type User =
type Model = { Page: PageTab; User: User }

type Msg =
| LoginPageMsg of Login.Msg
| WishlistMsg of WishList.Msg
| UrlChanged of string list
| OnSessionChange
Expand All @@ -47,15 +46,8 @@ let initFromUrl model url =
let model = { Page = Home; User = model.User }
model, Cmd.none
| [ "login" ] ->
let loginModel, loginMsg = Login.init ()

let model = {
Page = Login loginModel
User = model.User
}

let cmd = loginMsg |> Cmd.map LoginPageMsg
model, cmd
let model = { Page = Login; User = model.User }
model, Cmd.none
| [ "wishlist" ] ->
match model.User with
| User user ->
Expand All @@ -79,14 +71,6 @@ let init () =

let update msg model =
match model.Page, msg with
| Login loginModel, LoginPageMsg loginMsg ->
let user =
match loginMsg with
| Login.LoggedIn user -> User user
| _ -> model.User

let newModel, cmd = Login.update guestApi loginMsg loginModel
{ Page = Login newModel; User = user }, cmd |> Cmd.map LoginPageMsg
| Wishlist wishlistModel, WishlistMsg wishlistMsg ->
let token =
match model.User with
Expand Down Expand Up @@ -161,7 +145,7 @@ let view model dispatch =
prop.children [
match model.Page with
| Home -> Home.View guestApi
| Login loginModel -> Login.view loginModel (LoginPageMsg >> dispatch)
| Login -> Login.View guestApi
| Wishlist wishlistModel -> WishList.view wishlistModel (WishlistMsg >> dispatch)
| NotFound -> Html.div [ prop.text "Not Found" ]
]
Expand Down
6 changes: 5 additions & 1 deletion src/Client/pages/Login.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ open System
open Elmish
open Feliz.DaisyUI
open Feliz.Router
open Feliz.UseElmish
open FsToolkit.ErrorHandling
open Shared
open SAFE
Expand Down Expand Up @@ -68,7 +69,10 @@ let update (guestApi: IGuestApi) msg model =

open Feliz

let view model dispatch =
[<ReactComponent>]
let View guestApi =
let model, dispatch = React.useElmish (init, (update guestApi), [||])

Html.div [
prop.className "grid justify-center"
prop.children [
Expand Down

0 comments on commit 046cc03

Please sign in to comment.