Fork me on GitHub
#reitit
<
2021-12-13
>
NoahTheDuke17:12:54

is there a built-in way to do conditional routing in the frontend? right now, I either show the lobby or the active game, depending on if there is in fact an active game happening (using reagent), but for legacy reasons, they’re both routed to /play.

NoahTheDuke17:12:37

I have

(defn lobby-or-game []
  (if (get-in @app-state [:current-game :gameid])
    gameboard
    game-lobby))

(def routes
  (rf/router
    ...
    ["/play" {:name :nav/lobby
              :view lobby-or-game}]
    ...))
where gameboard and game-lobby are the two view functions that return hiccup, but this feels awkward/dirty

valtteri19:12:12

Doesn’t seem dirty to me. An alternative could be to use a controller to dispatch an event where you can check the app state and trigger an effect to redirect the user to lobby if needed

valtteri19:12:57

Assuming lobby would be another route

NoahTheDuke19:12:32

right now, for ease of use, the lobby is at /play as well so if you’re in a game, you can’t see other games (which limits other legacy issues lol)

valtteri19:12:28

If they are the same route then you probably will need the if in view 🙂

valtteri19:12:02

Which is not that bad imo