Hello all,
Long time lurker, I have what is a hopefully “small” problem for the great minds of this community.
We have a parent organisation with users (admins) and multiple child entities, each with their associated users and resources.
Users of the children entities browse resources via a set of urls and associated views e.g
/users
/posts
/users/$username/posts
*(I am using made up urls for simplicity)
and for the parent org, we provide access to the same resources via urls prefixed with /publishers/$entity_id/ + /.../
e.g
/publishers/$entity_id/users
/publishers/$entity_id/posts
/publishers/$entity_id/users/$username/posts
So far so good.
We initially did something like this in our .heex
templates, it is “clean enough”, it works
<.link :if={@is_admin} href={~p"/publishers/#{@entity}/users"} class="..">Users</.link>
<.link :if={not(@is_admin)} href={~p"/users"} class="..">Users</.link>
However, we are finding that this duplication everywhere isn’t very satisfying.
Another approach:
<.link href={@is_admin && ~p"/publishers/#{@entity}/users" || ~p"/users"} class="..">Users</.link>
This is also fine but gets unwieldy when my urls get relatively long.
Yet another approach (conditionally prefixing publishers/#{@entity}
to the route ~p"/users/#{@user}/posts"
<.link href={~p"/#{@is_admin && "publishers/#{@entity}/users/$username" || "/users/$username"}/posts"} class="..">Users</.link>
gives compile warnings saying 'no route path for Webapp.Router for "..."'
which is a perfectly valid warning.
And we can’t concatenate url ~p"/publishers/#{@entity}" + ~p"/users/#{@user}/posts"
.
My question:
Is there an obvious right way to do this that we are missing? Or have you been faced with something similar since the release of VerifiedRoutes and how did you handle it?
Thanks in advance!