carlgleisner

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

Where Next?

Popular in Questions Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
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
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
nsuchy
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
greenz1
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
9mm
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
svb
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 Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
minhajuddin
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
New
bsollish-terakeet
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
siddhant3030
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
sergio
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

We're in Beta

About us Mission Statement