Skip to content

Best practices, simple use case #34

Description

@gaku-sei

Hello,

So far I really enjoy using Concur for some personal projects!

But I'm often wondering what the "Concur way" of doing things should be. There are many examples, using Elm architecture, Signal, StateT, simple Widget, etc... and to be honest I'm a bit confused on which one is the best for which case.

For instance I was implementing a simple "fetch" button, which ended up looking like the following:

data State
  = Init
  | Loading
  | Error
  | Success (Array User)

fetchUsersSignal :: State -> Signal HTML State
fetchUsersSignal state =
  step state
    $ case state of
        Init -> return =<< fetchButton
        Loading -> do
          resp <-
            (liftAff $ Ajax.get ResponseFormat.string "https://jsonplaceholder.typicode.com/users")
              <|> D.button' [ D.text "Loading..." ]
          return $ maybe Error Success $ (hush <<< runExcept <<< decodeJSON <<< _.body) =<< hush resp
        Error -> return =<< fetchButton <|> D.div' [ D.text "An error occured, please try again" ]
        Success users -> do
          let
            names = (_.name <<< unwrap) <$> users
          liftEffect $ log $ show names
          void $ D.div' [ D.text "It worked!", D.div' $ (D.div' <<< pure <<< D.text) <$> names ]
          return state
  where
  fetchButton = D.button [ P.onClick $> Loading ] [ D.text "Fetch users" ]

  return = pure <<< fetchUsersSignal

As you can see it's using Signal, and some sort of "Elm-ish" architecture. I find this approach quite easy to read, but about the performances, and in general the downsides I could encounter with this approach? Would Widget be better? Should I put the logic in a separate case/of like in https://github.com/purescript-concur/purescript-concur-react/blob/master/examples/src/Test/TheElmArchitecture.purs?

In general, do you think there could be a page in the documentation listing some good/best practices, with some common use cases?

Thank you

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions