Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a minimal working example to the documentation #84

Open
KaneRoot opened this issue Jul 24, 2021 · 1 comment
Open

Add a minimal working example to the documentation #84

KaneRoot opened this issue Jul 24, 2021 · 1 comment
Labels
type: bug Something that should function correctly isn't.

Comments

@KaneRoot
Copy link

Hello everyone,

I encountered your project and from what I saw, you really help getting routing done in purescript quite easily.
I'm fairly new to Purescript, but I still understood your documentation.

The only problem I see is that there is no complete example.

Here is what I did as an example:

module Main where

import Prelude

import Effect (Effect)
import Halogen.Aff as HA
import Halogen.VDom.Driver (runUI)


import Control.Alternative ((<|>))
import Routing (match)
import Routing.Match (Match, lit, int, str, end)
import Data.Maybe -- Maybe
import Data.Foldable -- oneOf
import Data.Either (Either(..))

import Effect.Class (class MonadEffect)
import Effect.Class.Console (log)
import Halogen as H

import Routing.Hash (matches)

type DomainName = String

data Route
  = Home
  | Domain DomainName
  | Admin

-- matches everything
pageHome :: Match Route
pageHome = Home <$ lit ""

pageAdmin :: Match Route
pageAdmin = Admin <$ (lit "admin")

pageDomain :: Match Route
pageDomain = Domain <$> (lit "domain" *> str)

myRoute :: Match Route
myRoute = pageDomain <|> pageAdmin <|> pageHome

maybeMyRoute :: Match (Maybe Route)
maybeMyRoute = oneOf
  [ Just <$> myRoute
  , pure Nothing
  ]

And to test my routes, I did a small function that I can call from the purescript REPL:

testRoute :: String -> String
testRoute r = case (match maybeMyRoute r) of
  Left err -> err
  Right (Just (Admin)) -> "page admin"
  Right (Just (Domain dom)) -> "page domain: " <> dom
  Right (Just (Home)) -> "page home"
  Right (Nothing) -> "page nothing?"

The only problem I have is that I don't know how to make the "main" function to make it all work.
I'm pretty sure this could be done in a couple of lines, but I have no idea where to start.

Thanks, and have a good day!

@KaneRoot KaneRoot added the type: bug Something that should function correctly isn't. label Jul 24, 2021
@ocramz
Copy link

ocramz commented Dec 27, 2021

I've just stumbled on this and must agree.

As a Purescript beginner, but experienced Haskeller, the parser combinator approach based on Match is straightforward, but I'm completely stumped by comments such as

"Returns an effect which will remove the listener."

in the docstrings for matches etc. https://pursuit.purescript.org/packages/purescript-routing/10.0.1/docs/Routing.Hash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something that should function correctly isn't.
Development

No branches or pull requests

2 participants