nit: Shouldn't your route definition be type Route
instead of type alias Route
? I think your old route
function would work just fine with your new custom type definition.
But assuming that you want to do some useful pattern matching based on AuthenticatedRoute
or UnauthenticatedRoute
later on, you might want to change your Route
type a little to allow nested pattern matching. perhaps like this
type Route
= UnAuthed UnAuthenticatedRoute
| Authed AuthenticatedRoute
| NotFound
After this your route
method can be something like this I believe
route : Parser (Route -> a) a
route =
oneOf
[ map (Authed Index) top
, map (Authed New) (s "todos" </> s "new")
, map (Authed Edit) (s "todos" </> int </> s "edit")
, map (Authed Show) (s "todos" </> int)
, map (UnAuthed Login) (s "sign-in")
, map (UnAuthed Signup) (s "sign-up")
]
-- (or alternatively you can also split this into two parsers for authed routes and unauthed routes as well)
Now say you want to figure out what page to show based on the route:
processRoute : Route -> Model -> (Model, Cmd msg)
processRoute route model =
case route of
Authed authRoute ->
-- do stuff with your authRoute (e.g. pattern matching, go to login page if not authed )
UnAuthed unauthRoute ->
-- do stuff with your unauthroute
NotFound ->
-- do stuff with
I didn't try to compile anything above so there might be some errors, but hopefully this gives you some idea!