diff --git a/CHANGELOG.md b/CHANGELOG.md index b80c851d1..7b514729f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file. - Site switching keybinds (1-9 for respective sites) plausible/analytics#735 - Glob (wildcard) based pageview goals plausible/analytics#750 - Support for embedding shared links in an iframe plausible/analytics#812 +- Add name/label to shared links plausible/analytics#910 ### Fixed - Capitalized date/time selection keybinds not working plausible/analytics#709 diff --git a/lib/plausible/site/shared_link.ex b/lib/plausible/site/shared_link.ex index ff5d9ca7c..a63c3f59b 100644 --- a/lib/plausible/site/shared_link.ex +++ b/lib/plausible/site/shared_link.ex @@ -4,6 +4,7 @@ defmodule Plausible.Site.SharedLink do schema "shared_links" do belongs_to :site, Plausible.Site + field :name, :string field :slug, :string field :password_hash, :string field :password, :string, virtual: true @@ -13,8 +14,8 @@ defmodule Plausible.Site.SharedLink do def changeset(link, attrs \\ %{}) do link - |> cast(attrs, [:slug, :password]) - |> validate_required([:slug]) + |> cast(attrs, [:slug, :password, :name]) + |> validate_required([:slug, :name]) |> unique_constraint(:slug) |> hash_password() end diff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.ex index 5bcc55202..8470d53d8 100644 --- a/lib/plausible_web/controllers/site_controller.ex +++ b/lib/plausible_web/controllers/site_controller.ex @@ -541,6 +541,40 @@ defmodule PlausibleWeb.SiteController do end end + def edit_shared_link(conn, %{"website" => website, "slug" => slug}) do + site = Sites.get_for_user!(conn.assigns[:current_user].id, website) + shared_link = Repo.get_by(Plausible.Site.SharedLink, slug: slug) + changeset = Plausible.Site.SharedLink.changeset(shared_link, %{}) + + conn + |> assign(:skip_plausible_tracking, true) + |> render("edit_shared_link.html", + site: site, + changeset: changeset, + layout: {PlausibleWeb.LayoutView, "focus.html"} + ) + end + + def update_shared_link(conn, %{"website" => website, "slug" => slug, "shared_link" => params}) do + site = Sites.get_for_user!(conn.assigns[:current_user].id, website) + shared_link = Repo.get_by(Plausible.Site.SharedLink, slug: slug) + changeset = Plausible.Site.SharedLink.changeset(shared_link, params) + + case Repo.update(changeset) do + {:ok, _created} -> + redirect(conn, to: "/#{URI.encode_www_form(site.domain)}/settings/visibility") + + {:error, changeset} -> + conn + |> assign(:skip_plausible_tracking, true) + |> render("edit_shared_link.html", + site: site, + changeset: changeset, + layout: {PlausibleWeb.LayoutView, "focus.html"} + ) + end + end + def delete_shared_link(conn, %{"website" => website, "slug" => slug}) do site = Sites.get_for_user!(conn.assigns[:current_user].id, website) diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex index 8dc0d8232..c49cd01a8 100644 --- a/lib/plausible_web/router.ex +++ b/lib/plausible_web/router.ex @@ -179,6 +179,8 @@ defmodule PlausibleWeb.Router do get "/sites/:website/shared-links/new", SiteController, :new_shared_link post "/sites/:website/shared-links", SiteController, :create_shared_link + get "/sites/:website/shared-links/:slug/edit", SiteController, :edit_shared_link + put "/sites/:website/shared-links/:slug", SiteController, :update_shared_link delete "/sites/:website/shared-links/:slug", SiteController, :delete_shared_link get "/sites/:website/custom-domains/new", SiteController, :new_custom_domain diff --git a/lib/plausible_web/templates/site/edit_shared_link.html.eex b/lib/plausible_web/templates/site/edit_shared_link.html.eex new file mode 100644 index 000000000..00b98d0b3 --- /dev/null +++ b/lib/plausible_web/templates/site/edit_shared_link.html.eex @@ -0,0 +1,12 @@ +<%= form_for @changeset, "/sites/#{URI.encode_www_form(@site.domain)}/shared-links/#{@changeset.data.slug}", [class: "max-w-md w-full mx-auto bg-white dark:bg-gray-800 shadow-md rounded px-8 pt-6 pb-8 mb-4 mt-8"], fn f -> %> +

Edit shared link

+
+ <%= label f, :name, "Name", class: "block text-sm font-medium text-gray-700 dark:text-gray-100" %> +
+ <%= text_input f, :name, class: "shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 rounded-md", required: "required" %> + <%= error_tag f, :name %> +
+
+ + <%= submit "Edit shared link", class: "button mt-4 w-full" %> +<% end %> diff --git a/lib/plausible_web/templates/site/new_shared_link.html.eex b/lib/plausible_web/templates/site/new_shared_link.html.eex index 2681b1bd4..00ae88c94 100644 --- a/lib/plausible_web/templates/site/new_shared_link.html.eex +++ b/lib/plausible_web/templates/site/new_shared_link.html.eex @@ -1,13 +1,23 @@ <%= form_for @changeset, "/sites/#{URI.encode_www_form(@site.domain)}/shared-links", [class: "max-w-md w-full mx-auto bg-white dark:bg-gray-800 shadow-md rounded px-8 pt-6 pb-8 mb-4 mt-8"], fn f -> %>

New shared link

- Add a password or leave it blank so anyone with the link can see the stats. - Once the link is created, we cannot reveal the password. Please make sure you save it in a secure place.
-
- <%= label f, :password, "Password (optional)", class: "block text-sm font-bold dark:text-gray-100" %> - <%= password_input f, :password, class: "transition mt-3 bg-gray-100 dark:bg-gray-900 outline-none appearance-none border border-transparent rounded w-full p-2 text-gray-700 dark:text-gray-300 leading-normal focus:outline-none focus:bg-white dark:focus:bg-gray-800 focus:border-gray-300 dark:focus:border-gray-500" %> - <%= error_tag f, :password %> +
+ <%= label f, :name, "Name", class: "block text-sm font-medium text-gray-700 dark:text-gray-100" %> +
+ <%= text_input f, :name, class: "shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 rounded-md", required: "required" %> + <%= error_tag f, :name %> +
+
+
+ <%= label f, :password, "Password (optional)", class: "block text-sm font-medium text-gray-700 dark:text-gray-100" %> +
+ <%= password_input f, :password, class: "shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 rounded-md" %> + <%= error_tag f, :password %> +

+ Password protection is optional. Please make sure you save it in a secure place. Once the link is created, we cannot reveal the password. +

+
<%= submit "Create shared link", class: "button mt-4 w-full" %> diff --git a/lib/plausible_web/templates/site/settings_visibility.html.eex b/lib/plausible_web/templates/site/settings_visibility.html.eex index 2c111810d..ed13c9a5c 100644 --- a/lib/plausible_web/templates/site/settings_visibility.html.eex +++ b/lib/plausible_web/templates/site/settings_visibility.html.eex @@ -33,16 +33,30 @@ <% end %> -
+
<%= for link <- @shared_links do %> -
- - - <%= button(to: "/sites/#{URI.encode_www_form(@site.domain)}/shared-links/#{link.slug}", method: :delete, class: "py-2 px-4 bg-gray-200 dark:bg-gray-850 text-red-600 dark:text-red-500 rounded-l-none hover:bg-gray-300 dark:hover:bg-gray-825", data: [confirm: "Are you sure you want to delete this shared link? The stats will not be accessible with this link anymore."]) do %> - - <% end %> +
+ +
+ + + <%= link(to: "/sites/#{URI.encode_www_form(@site.domain)}/shared-links/#{link.slug}/edit", class: "px-4 py-2 inline-flex items-center text-indigo-800 bg-gray-200 border-r border-gray-300 rounded-none dark:bg-gray-850 dark:text-indigo-500 dark:border-gray-500 hover:bg-gray-300 dark:hover:bg-gray-825") do %> + + <% end %> + <%= button(to: "/sites/#{URI.encode_www_form(@site.domain)}/shared-links/#{link.slug}", method: :delete, class: "py-2 px-4 inline-flex items-center bg-gray-200 dark:bg-gray-850 text-red-600 dark:text-red-500 rounded-l-none hover:bg-gray-300 dark:hover:bg-gray-825", data: [confirm: "Are you sure you want to delete this shared link? The stats will not be accessible with this link anymore."]) do %> + + <% end %> +
<% end %> diff --git a/priv/repo/migrations/20210406073254_add_name_to_shared_links.exs b/priv/repo/migrations/20210406073254_add_name_to_shared_links.exs new file mode 100644 index 000000000..108c28f0f --- /dev/null +++ b/priv/repo/migrations/20210406073254_add_name_to_shared_links.exs @@ -0,0 +1,15 @@ +defmodule Plausible.Repo.Migrations.AddNameToSharedLinks do + use Ecto.Migration + + def change do + alter table(:shared_links) do + add :name, :string + end + + execute "UPDATE shared_links SET name=slug" + + alter table(:shared_links) do + modify :name, :string, null: false + end + end +end diff --git a/test/plausible_web/controllers/site_controller_test.exs b/test/plausible_web/controllers/site_controller_test.exs index 88bbf82ac..81ce569cf 100644 --- a/test/plausible_web/controllers/site_controller_test.exs +++ b/test/plausible_web/controllers/site_controller_test.exs @@ -493,23 +493,54 @@ defmodule PlausibleWeb.SiteControllerTest do setup [:create_user, :log_in, :create_site] test "creates shared link without password", %{conn: conn, site: site} do - post(conn, "/sites/#{site.domain}/shared-links", %{"shared_link" => %{}}) + post(conn, "/sites/#{site.domain}/shared-links", %{ + "shared_link" => %{"name" => "Link name"} + }) link = Repo.one(Plausible.Site.SharedLink) refute is_nil(link.slug) assert is_nil(link.password_hash) + assert link.name == "Link name" end test "creates shared link with password", %{conn: conn, site: site} do post(conn, "/sites/#{site.domain}/shared-links", %{ - "shared_link" => %{"password" => "password"} + "shared_link" => %{"password" => "password", "name" => "New name"} }) link = Repo.one(Plausible.Site.SharedLink) refute is_nil(link.slug) refute is_nil(link.password_hash) + assert link.name == "New name" + end + end + + describe "GET /sites/:website/shared-links/edit" do + setup [:create_user, :log_in, :create_site] + + test "shows form to edit shared link", %{conn: conn, site: site} do + link = insert(:shared_link, site: site) + conn = get(conn, "/sites/#{site.domain}/shared-links/#{link.slug}/edit") + + assert html_response(conn, 200) =~ "Edit shared link" + end + end + + describe "PUT /sites/:website/shared-links/:slug" do + setup [:create_user, :log_in, :create_site] + + test "can update link name", %{conn: conn, site: site} do + link = insert(:shared_link, site: site) + + put(conn, "/sites/#{site.domain}/shared-links/#{link.slug}", %{ + "shared_link" => %{"name" => "Updated link name"} + }) + + link = Repo.one(Plausible.Site.SharedLink) + + assert link.name == "Updated link name" end end diff --git a/test/support/factory.ex b/test/support/factory.ex index 78e36018d..35f33739b 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -147,6 +147,7 @@ defmodule Plausible.Factory do def shared_link_factory do %Plausible.Site.SharedLink{ + name: "Link name", slug: Nanoid.generate() } end