carlgleisner
Flags for liveviews in live_session?
Hi,
Are there any preferred ways of checking for feature flags for live views in a live_session?
I would assume that one use attach_hook/4 and then add that hook to the relevant live_session.
I know Plausible.io uses FunWithFlags making it a nice reference, but they do this for controller actions and components, not entire live views.
I threw (!) together the code below to test the concept.
It gets more complicated if one has a tab for a path to several other live views of course, which I would believe is a fairly common use case.
defmodule MyAppWeb.CheckFlags do
use MyAppWeb, :verified_routes
import Phoenix.LiveView
import MyAppWeb.Gettext
def on_mount(:default, _params, _session, socket) do
{:cont,
socket
|> attach_hook(:flags, :handle_params, &check_flags/3)}
end
defp check_flags(_params, _session, socket) do
continue? =
case socket.view do
MyAppWeb.ExcitingExperimentalFeatureLive ->
FunWithFlags.enabled?(:exciting_experimental_feature, for: socket.assigns.current_user)
_ ->
true
end
if continue? do
{:cont, socket}
else
socket =
socket
|> Phoenix.LiveView.put_flash(
:error,
gettext("This feature has not been enabled for your account.")
)
|> Phoenix.LiveView.redirect(to: ~p"/")
{:halt, socket}
end
end
end
Popular in Questions
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
lets say i have a sample like
a = 20; b = 10;
if (a > b) do
{:ok, "a"}
end
if (a < b) do
{:ok, b}
end
if (a == b) do
{:ok, "equa...
New
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Hi!
Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
Other popular topics
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









