-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathfeature.ex
More file actions
44 lines (39 loc) · 1.39 KB
/
feature.ex
File metadata and controls
44 lines (39 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
defmodule PlausibleWeb.Components.Site.Feature do
@moduledoc """
Phoenix Component for rendering a user-facing feature toggle
capable of flipping booleans in `Plausible.Site` via the `toggle_feature` controller action.
"""
use PlausibleWeb, :view
attr(:site, Plausible.Site, required: true)
attr(:feature_mod, :atom, required: true, values: Plausible.Billing.Feature.list())
attr(:conn, Plug.Conn, required: true)
attr(:class, :any, default: nil)
slot(:inner_block)
def toggle(assigns) do
assigns =
assigns
|> assign(:current_setting, assigns.feature_mod.enabled?(assigns.site))
|> assign(:disabled?, assigns.feature_mod.check_availability(assigns.site.team) !== :ok)
~H"""
<div class="mt-4">
<.form
action={target(@site, @feature_mod.toggle_field(), @conn, !@current_setting)}
method="put"
for={nil}
class={@class}
>
<.toggle_submit set_to={@current_setting} disabled?={@disabled?}>
Show {String.downcase(@feature_mod.display_name())} in the dashboard
</.toggle_submit>
</.form>
<div :if={@current_setting}>
{render_slot(@inner_block)}
</div>
</div>
"""
end
def target(site, setting, conn, set_to) when is_boolean(set_to) do
r = conn.request_path
Routes.site_path(conn, :update_feature_visibility, site.domain, setting, r: r, set: set_to)
end
end