Coder Social home page Coder Social logo

Remove duplication about safe-bookstore HOT 7 CLOSED

safe-stack avatar safe-stack commented on July 17, 2024
Remove duplication

from safe-bookstore.

Comments (7)

et1975 avatar et1975 commented on July 17, 2024

I don't know if there's a good answer for what you asking, but a couple of small observations:

  • You should never need to call urlUpdate from update
  • Delegate as much as possible into child modules (you can think of auth as viewless child)

from safe-bookstore.

forki avatar forki commented on July 17, 2024
  • regarding urlupdate - care to send one of your famous refactoring PRs?
  • yes but even if you push more into the submodules. it's still that weird pattern match - but on the other hand it's super explicit. Much better than hiding such things in DI containers. and compiler tells me if I forget. so ...

from safe-bookstore.

bentayloruk avatar bentayloruk commented on July 17, 2024

This match also looks funky to me, because when we have a message for a page, but we don't have the correct sub-model for that page (in the app model), we just drop through (i.e. ignore the message). Is this considered OK, from a logical perspective? It feels like it should be an error, but I'm assuming there must be conditions where you could get in this state legitimately.

from safe-bookstore.

bentayloruk avatar bentayloruk commented on July 17, 2024

@forki do you think it would improve things, if we promote the SubModel to the top level match? To me, this makes it clearer and makes the drop through case distinct too.

let update msg model =
    match msg, model.SubModel with
    | AppMsg.OpenLogIn, _ ->
        let m,cmd = Login.init None
        { model with
            Page = Page.Login
            SubModel = LoginModel m }, Cmd.batch [cmd; Navigation.modifyUrl (toHash Page.Login) ]

    | StorageFailure e, _ ->
        printfn "Unable to access local storage: %A" e
        model, []

    | LoginMsg msg, LoginModel m ->
        let m,cmd = Login.update msg m
        let cmd = Cmd.map LoginMsg cmd
        match m.State with
        | Login.LoginState.LoggedIn token ->
            let newUser : UserData = { UserName = m.Login.UserName; Token = token }
            let cmd =
                if model.Menu.User = Some newUser then cmd else
                Cmd.batch [cmd
                           Cmd.ofFunc (Utils.save "user") newUser (fun _ -> LoggedIn) StorageFailure ]

            { model with
                SubModel = LoginModel m
                Menu = { model.Menu with User = Some newUser }}, cmd
        | _ ->
            { model with
                SubModel = LoginModel m
                Menu = { model.Menu with User = None } }, cmd

    | LoginMsg msg, _ -> model, Cmd.none

    | PromoMsg msg, PromoModel m ->
        let m, cmd = Promo.update msg m
        let cmd = Cmd.map PromoMsg cmd
        { model with 
            SubModel = PromoModel m }, cmd

    | PromoMsg msg, _ -> model, Cmd.none
        
    | WishListMsg msg, WishListModel m ->
        let m,cmd = WishList.update msg m
        let cmd = Cmd.map WishListMsg cmd
        { model with
            SubModel = WishListModel m }, cmd
    
    | WishListMsg msg, _ -> model, Cmd.none

    | AppMsg.LoggedIn, _ ->
        let nextPage = Page.WishList
        let m,cmd = urlUpdate (Some nextPage) model
        match m.Menu.User with
        | Some user ->
            m, Cmd.batch [cmd; Navigation.modifyUrl (toHash nextPage) ]
        | None ->
            m, Cmd.ofMsg Logout

    | AppMsg.LoggedOut, _ ->
        { model with
            Page = Page.Home
            SubModel = NoSubModel
            Menu = { model.Menu with User = None } },
        Navigation.modifyUrl (toHash Page.Home)

    | AppMsg.Logout, _ ->
        model, Cmd.ofFunc Utils.delete "user" (fun _ -> LoggedOut) StorageFailure

from safe-bookstore.

forki avatar forki commented on July 17, 2024

yes. thought I already did that

from safe-bookstore.

bentayloruk avatar bentayloruk commented on July 17, 2024

The code you link to above, in master has the nested match. Want me to PR it?

from safe-bookstore.

forki avatar forki commented on July 17, 2024

from safe-bookstore.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.