Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Spike Team Settings section (#4941)

hq17b05cb2c9e3a
diff --git a/lib/plausible/teams.ex b/lib/plausible/teams.ex
index 50a398c7bb..43ec2950af 100644
--- a/lib/plausible/teams.ex
+++ b/lib/plausible/teams.ex
@@ -12,6 +12,10 @@ defmodule Plausible.Teams do
@accept_traffic_until_free ~D[2135-01-01]
+ def enabled?(team) do
+ not is_nil(team) and FunWithFlags.enabled?(:teams, for: team)
+ end
+
@spec get!(pos_integer()) :: Teams.Team.t()
def get!(team_id) do
Repo.get!(Teams.Team, team_id)
diff --git a/lib/plausible/teams/team.ex b/lib/plausible/teams/team.ex
index f9f732bd92..ebbf0bc185 100644
--- a/lib/plausible/teams/team.ex
+++ b/lib/plausible/teams/team.ex
@@ -3,6 +3,12 @@ defmodule Plausible.Teams.Team do
Team schema
"""
+ defimpl FunWithFlags.Actor, for: __MODULE__ do
+ def id(%{id: id}) do
+ "team:#{id}"
+ end
+ end
+
use Ecto.Schema
use Plausible
@@ -46,6 +52,12 @@ defmodule Plausible.Teams.Team do
|> maybe_bump_accept_traffic_until()
end
+ def name_changeset(team, attrs \\ %{}) do
+ team
+ |> cast(attrs, [:name])
+ |> validate_required(:name)
+ end
+
def start_trial(team, today \\ Date.utc_today()) do
trial_expiry = trial_expiry(today)
diff --git a/lib/plausible_web/controllers/settings_controller.ex b/lib/plausible_web/controllers/settings_controller.ex
index 374673a339..b47c912319 100644
--- a/lib/plausible_web/controllers/settings_controller.ex
+++ b/lib/plausible_web/controllers/settings_controller.ex
@@ -12,6 +12,38 @@ defmodule PlausibleWeb.SettingsController do
redirect(conn, to: Routes.settings_path(conn, :preferences))
end
+ def team_general(conn, _params) do
+ render_team_general(conn)
+ end
+
+ def update_team_name(conn, %{"team" => params}) do
+ changeset = Plausible.Teams.Team.name_changeset(conn.assigns.my_team, params)
+
+ case Repo.update(changeset) do
+ {:ok, _user} ->
+ conn
+ |> put_flash(:success, "Team name changed")
+ |> redirect(to: Routes.settings_path(conn, :team_general) <> "#update-name")
+
+ {:error, changeset} ->
+ render_team_general(conn, team_name_changeset: changeset)
+ end
+ end
+
+ defp render_team_general(conn, opts \\ []) do
+ name_changeset =
+ Keyword.get(
+ opts,
+ :team_name_changeset,
+ Plausible.Teams.Team.name_changeset(conn.assigns.my_team)
+ )
+
+ render(conn, :team_general,
+ team_name_changeset: name_changeset,
+ layout: {PlausibleWeb.LayoutView, :settings}
+ )
+ end
+
def preferences(conn, _params) do
render_preferences(conn)
end
diff --git a/lib/plausible_web/live/components/form.ex b/lib/plausible_web/live/components/form.ex
index 2fec26aec1..7373bbde0c 100644
--- a/lib/plausible_web/live/components/form.ex
+++ b/lib/plausible_web/live/components/form.ex
@@ -353,4 +353,46 @@ defmodule PlausibleWeb.Live.Components.Form do
String.replace(acc, "%{#{key}}", fn _ -> to_string(value) end)
end)
end
+
+ attr :conn, Plug.Conn, required: true
+ attr :name, :string, required: true
+ attr :options, :list, required: true
+ attr :value, :any, default: nil
+ attr :href_base, :string, default: "/"
+ attr :selected_fn, :any, required: true
+
+ def mobile_nav_dropdown(%{options: options} = assigns) do
+ options =
+ Enum.reduce(options, Map.new(), fn
+ {section, opts}, acc when is_list(opts) ->
+ Map.put(acc, section, for(o <- opts, do: {o.key, o.value}))
+
+ {key, value}, _acc when is_binary(key) and is_binary(value) ->
+ options
+ end)
+
+ assigns = assign(assigns, :options, options)
+
+ ~H"""
+ <.form for={@conn} class="lg:hidden">
+ <.input
+ value={
+ @options
+ |> Enum.flat_map(fn
+ {_section, opts} when is_list(opts) -> opts
+ {k, v} when is_binary(k) and is_binary(v) -> [{k, v}]
+ end)
+ |> Enum.find_value(fn {_k, v} ->
+ apply(@selected_fn, [v]) && v
+ end)
+ }
+ name={@name}
+ type="select"
+ options={@options}
+ onchange={"if (event.target.value) { location.href = '#{@href_base}' + event.target.value }"}
+ class="dark:bg-gray-800 mt-1 block w-full pl-3 pr-10 py-2 text-base border-gray-300 dark:border-gray-500 outline-none focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 rounded-md dark:text-gray-100"
+ />
+ </.form>
+ """
+ end
end
diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex
index a03955599f..cdd96a6f45 100644
--- a/lib/plausible_web/router.ex
+++ b/lib/plausible_web/router.ex
@@ -362,6 +362,9 @@ defmodule PlausibleWeb.Router do
delete "/api-keys/:id", SettingsController, :delete_api_key
get "/danger-zone", SettingsController, :danger_zone
+
+ get "/team/general", SettingsController, :team_general
+ post "/team/general/name", SettingsController, :update_team_name
end
scope "/", PlausibleWeb do
diff --git a/lib/plausible_web/templates/layout/settings.html.heex b/lib/plausible_web/templates/layout/settings.html.heex
index 9862f0a683..d16bf2031b 100644
--- a/lib/plausible_web/templates/layout/settings.html.heex
+++ b/lib/plausible_web/templates/layout/settings.html.heex
@@ -1,20 +1,5 @@
<%= render_layout "app.html", assigns do %>
- <% options = [
- %{key: "Preferences", value: "preferences", icon: :cog_6_tooth},
- %{key: "Security", value: "security", icon: :lock_closed},
- %{key: "Subscription", value: "billing/subscription", icon: :circle_stack},
- %{key: "Invoices", value: "billing/invoices", icon: :banknotes},
- %{key: "API Keys", value: "api-keys", icon: :key},
- %{key: "Danger Zone", value: "danger-zone", icon: :exclamation_triangle}
- ]
-
- options =
- if Plausible.ee?() do
- options
- else
- Enum.reject(options, fn option -> String.contains?(option.value, "billing") end)
- end %>
-
+ <% options = account_settings_sidebar(@conn) %>
<div class="container pt-6">
<.styled_link class="text-indigo-600 font-bold text-sm" href="/sites">
← Back to Sites
@@ -26,24 +11,21 @@
</div>
<div class="lg:grid lg:grid-cols-12 lg:gap-x-5 lg:mt-4">
<div class="py-4 g:py-0 lg:col-span-3">
- <div class="mb-4">
+ <div class="mb-4 hidden lg:block">
<h3 class="uppercase text-sm text-indigo-600 font-semibold">Account Settings</h3>
- <p class="text-xs dark:text-gray-400"><%= @current_user.email %></p>
+ <p class="text-xs dark:text-gray-400 truncate"><%= @current_user.email %></p>
</div>
- <.form for={@conn} class="lg:hidden">
- <.input
- value={Enum.find_value(options, &(is_current_tab(@conn, &1.value) && &1.value))}
- name="setting"
- type="select"
- options={Enum.map(options, fn opt -> {opt.key, opt.value} end)}
- onchange="location.href = '/settings/' + event.target.value"
- class="dark:bg-gray-800 mt-1 block w-full pl-3 pr-10 py-2 text-base border-gray-300 dark:border-gray-500 outline-none focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 rounded-md dark:text-gray-100"
- />
- </.form>
+ <.mobile_nav_dropdown
+ name="settings"
+ options={options}
+ selected_fn={&is_current_tab(@conn, &1)}
+ conn={@conn}
+ href_base="/settings/"
+ />
<div class="hidden lg:block">
- <%= for %{key: key, value: value, icon: icon} <- options do %>
+ <%= for %{key: key, value: value, icon: icon} <- options["Account Settings"] do %>
<%= render("_settings_tab.html",
icon: icon,
this_tab: value,
@@ -53,6 +35,25 @@
) %>
<% end %>
</div>
+
+ <div :if={Plausible.Teams.enabled?(@my_team)}>
+ <div class="mb-4 mt-4 hidden lg:block">
+ <h3 class="uppercase text-sm text-indigo-600 font-semibold">Team Settings</h3>
+ <p class="text-xs dark:text-gray-400 truncate"><%= @my_team.name %></p>
+ </div>
+
+ <div class="hidden lg:block">
+ <%= for %{key: key, value: value, icon: icon} <- options["Team Settings"] do %>
+ <%= render("_settings_tab.html",
+ icon: icon,
+ this_tab: value,
+ text: key,
+ conn: @conn,
+ submenu?: false
+ ) %>
+ <% end %>
+ </div>
+ </div>
</div>
<div class="space-y-6 lg:col-span-9 lg:mt-4">
diff --git a/lib/plausible_web/templates/layout/site_settings.html.heex b/lib/plausible_web/templates/layout/site_settings.html.heex
index 53248e2a99..f0b7983cbd 100644
--- a/lib/plausible_web/templates/layout/site_settings.html.heex
+++ b/lib/plausible_web/templates/layout/site_settings.html.heex
@@ -13,23 +13,17 @@
</div>
<div class="lg:grid lg:grid-cols-12 lg:gap-x-5 lg:mt-4">
<div class="py-4 g:py-0 lg:col-span-3">
- <.form
- for={@conn}
- action={"/sites/#{URI.encode_www_form(@site.domain)}"}
- class="lg:hidden"
- >
- <% options = flat_settings_options(@conn) %>
- <.input
- type="select"
- name="tab"
- options={options}
- class="dark:bg-gray-800 mt-1 block w-full pl-3 pr-10 py-2 text-base border-gray-300 dark:border-gray-500 outline-none focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 rounded-md dark:text-gray-100"
- onchange={"location.href = '/" <> URI.encode_www_form(@site.domain) <> "/settings/' + event.target.value"}
- value={Enum.find_value(options, &(is_current_tab(@conn, elem(&1, 1)) && elem(&1, 1)))}
- />
- </.form>
+ <% options = flat_site_settings_options(@conn) %>
+ <.mobile_nav_dropdown
+ conn={@conn}
+ options={options}
+ name="tab"
+ href_base={"/#{URI.encode_www_form(@site.domain)}/settings/"}
+ selected_fn={&is_current_tab(@conn, &1)}
+ />
+
<div class="hidden lg:block">
- <%= for %{key: key, value: value, icon: icon} <- settings_tabs(@conn) do %>
+ <%= for %{key: key, value: value, icon: icon} <- site_settings_sidebar(@conn) do %>
<%= if is_binary(value) do %>
<%= render("_site_settings_tab.html",
icon: icon,
diff --git a/lib/plausible_web/templates/settings/team_general.html.heex b/lib/plausible_web/templates/settings/team_general.html.heex
new file mode 100644
index 0000000000..bbd038d66b
--- /dev/null
+++ b/lib/plausible_web/templates/settings/team_general.html.heex
@@ -0,0 +1,22 @@
+<.settings_tiles>
+ <.tile>
+ <:title>
+ <a id="update-name">Team Name</a>
+ </:title>
+ <:subtitle>
+ Change the name of your team
+ </:subtitle>
+ <.form
+ :let={f}
+ action={Routes.settings_path(@conn, :update_team_name)}
+ for={@team_name_changeset}
+ method="post"
+ >
+ <.input type="text" field={f[:name]} label="Name" width="w-1/2" />
+
+ <.button type="submit">
+ Change Name
+ </.button>
+ </.form>
+ </.tile>
+</.settings_tiles>
diff --git a/lib/plausible_web/views/layout_view.ex b/lib/plausible_web/views/layout_view.ex
index 33435af9dd..1ae46cec8f 100644
--- a/lib/plausible_web/views/layout_view.ex
+++ b/lib/plausible_web/views/layout_view.ex
@@ -48,7 +48,7 @@ defmodule PlausibleWeb.LayoutView do
end
end
- def settings_tabs(conn) do
+ def site_settings_sidebar(conn) do
[
%{key: "General", value: "general", icon: :rocket_launch},
%{key: "People", value: "people", icon: :users},
@@ -78,9 +78,9 @@ defmodule PlausibleWeb.LayoutView do
|> Enum.reject(&is_nil/1)
end
- def flat_settings_options(conn) do
+ def flat_site_settings_options(conn) do
conn
- |> settings_tabs()
+ |> site_settings_sidebar()
|> Enum.map(fn
%{value: value, key: key} when is_binary(value) ->
{key, value}
@@ -93,6 +93,27 @@ defmodule PlausibleWeb.LayoutView do
|> List.flatten()
end
+ def account_settings_sidebar(conn) do
+ options = %{
+ "Account Settings" => [
+ %{key: "Preferences", value: "preferences", icon: :cog_6_tooth},
+ %{key: "Security", value: "security", icon: :lock_closed},
+ %{key: "Subscription", value: "billing/subscription", icon: :circle_stack},
+ %{key: "Invoices", value: "billing/invoices", icon: :banknotes},
+ %{key: "API Keys", value: "api-keys", icon: :key},
+ %{key: "Danger Zone", value: "danger-zone", icon: :exclamation_triangle}
+ ]
+ }
+
+ if Plausible.Teams.enabled?(conn.assigns[:my_team]) do
+ Map.put(options, "Team Settings", [
+ %{key: "General", value: "team/general", icon: :adjustments_horizontal}
+ ])
+ else
+ options
+ end
+ end
+
def trial_notification(team) do
case Plausible.Teams.trial_days_left(team) do
days when days > 1 ->
@@ -126,6 +147,16 @@ defmodule PlausibleWeb.LayoutView do
end
def is_current_tab(conn, tab) do
- String.ends_with?(Enum.join(conn.path_info, "/"), tab)
+ full_path = Path.join(conn.path_info)
+
+ one_up =
+ conn.path_info
+ |> Enum.drop(-1)
+ |> Path.join()
+
+ case conn.method do
+ :get -> String.ends_with?(full_path, tab)
+ _ -> String.ends_with?(full_path, tab) or String.ends_with?(one_up, tab)
+ end
end
end
diff --git a/test/plausible_web/controllers/settings_controller_test.exs b/test/plausible_web/controllers/settings_controller_test.exs
index ce2620d01c..0f592fffee 100644
--- a/test/plausible_web/controllers/settings_controller_test.exs
+++ b/test/plausible_web/controllers/settings_controller_test.exs
@@ -1103,6 +1103,58 @@ defmodule PlausibleWeb.SettingsControllerTest do
end
end
+ describe "Team Settings" do
+ setup [:create_user, :log_in]
+
+ test "does not render team settings, when no team assigned", %{conn: conn} do
+ conn = get(conn, Routes.settings_path(conn, :preferences))
+ html = html_response(conn, 200)
+ refute html =~ "Team Settings"
+ end
+
+ test "renders team settings, when team assigned", %{conn: conn, user: user} do
+ {:ok, team} = Plausible.Teams.get_or_create(user)
+ conn = get(conn, Routes.settings_path(conn, :preferences))
+ html = html_response(conn, 200)
+ assert html =~ "Team Settings"
+ assert html =~ team.name
+ end
+
+ test "GET /settings/team/general", %{conn: conn, user: user} do
+ {:ok, team} = Plausible.Teams.get_or_create(user)
+ conn = get(conn, Routes.settings_path(conn, :team_general))
+ html = html_response(conn, 200)
+ assert html =~ "Team Name"
+ assert html =~ "Change the name of your team"
+ assert text_of_attr(html, "input#team_name", "value") == team.name
+ end
+
+ test "POST /settings/team/general/name", %{conn: conn, user: user} do
+ {:ok, team} = Plausible.Teams.get_or_create(user)
+
+ conn =
+ post(conn, Routes.settings_path(conn, :update_team_name), %{
+ "team" => %{"name" => "New Name"}
+ })
+
+ assert redirected_to(conn, 302) ==
+ Routes.settings_path(conn, :team_general) <> "#update-name"
+
+ assert Repo.reload!(team).name == "New Name"
+ end
+
+ test "POST /settings/team/general/name - changeset error", %{conn: conn, user: user} do
+ {:ok, _team} = Plausible.Teams.get_or_create(user)
+
+ conn =
+ post(conn, Routes.settings_path(conn, :update_team_name), %{
+ "team" => %{"name" => ""}
+ })
+
+ assert text(html_response(conn, 200)) =~ "can't be blank"
+ end
+ end
+
defp configure_enterprise_plan(user, attrs \\ []) do
subscribe_to_enterprise_plan(
user,
diff --git a/test/test_helper.exs b/test/test_helper.exs
index 3870088140..fef0ce84c6 100644
--- a/test/test_helper.exs
+++ b/test/test_helper.exs
@@ -8,6 +8,7 @@ Application.ensure_all_started(:double)
FunWithFlags.enable(:channels)
FunWithFlags.enable(:scroll_depth)
+FunWithFlags.enable(:teams)
Ecto.Adapters.SQL.Sandbox.mode(Plausible.Repo, :manual)