Skip to content

Add a minimal working example to the documentation #84

Open
@KaneRoot

Description

@KaneRoot

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugSomething that should function correctly isn't.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions