Skip to content

Commit

Permalink
[Sentry APP-5B] Ensure form errors can be rendered on /settings (#2278)
Browse files Browse the repository at this point in the history
* Ensure form errors can be rendered on /settings

* Changelog
  • Loading branch information
ukutaht authored Sep 28, 2022
1 parent d104abb commit 65f0ce3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ All notable changes to this project will be documented in this file.
- Timezone offset labels now update with time changes
- Render 404 if shared link auth cannot be verified [plausible/analytics#2225](https://github.com/plausible/analytics/pull/2225)
- Restore compatibility with older format of shared links [plausible/analytics#2225](https://github.com/plausible/analytics/pull/2225)
- Ensure settings page can be rendered after a form error [plausible/analytics#2278](https://github.com/plausible/analytics/pull/2278)

### Changed
- Cache the tracking script for 24 hours
Expand Down
36 changes: 18 additions & 18 deletions lib/plausible_web/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -437,20 +437,8 @@ defmodule PlausibleWeb.AuthController do
end

def user_settings(conn, _params) do
user = conn.assigns[:current_user]
changeset = Auth.User.changeset(user)

{usage_pageviews, usage_custom_events} = Plausible.Billing.usage_breakdown(user)

render(conn, "user_settings.html",
user: user |> Repo.preload(:api_keys),
changeset: changeset,
subscription: user.subscription,
invoices: Plausible.Billing.paddle_api().get_invoices(user.subscription),
theme: user.theme || "system",
usage_pageviews: usage_pageviews,
usage_custom_events: usage_custom_events
)
changeset = Auth.User.changeset(conn.assigns[:current_user])
render_settings(conn, changeset)
end

def save_settings(conn, %{"user" => user_params}) do
Expand All @@ -463,13 +451,25 @@ defmodule PlausibleWeb.AuthController do
|> redirect(to: Routes.auth_path(conn, :user_settings))

{:error, changeset} ->
render(conn, "user_settings.html",
changeset: changeset,
subscription: conn.assigns[:current_user].subscription
)
render_settings(conn, changeset)
end
end

defp render_settings(conn, changeset) do
user = conn.assigns[:current_user]
{usage_pageviews, usage_custom_events} = Plausible.Billing.usage_breakdown(user)

render(conn, "user_settings.html",
user: user |> Repo.preload(:api_keys),
changeset: changeset,
subscription: user.subscription,
invoices: Plausible.Billing.paddle_api().get_invoices(user.subscription),
theme: user.theme || "system",
usage_pageviews: usage_pageviews,
usage_custom_events: usage_custom_events
)
end

def new_api_key(conn, _params) do
key = :crypto.strong_rand_bytes(64) |> Base.url_encode64() |> binary_part(0, 64)
changeset = Auth.ApiKey.changeset(%Auth.ApiKey{}, %{key: key})
Expand Down
6 changes: 6 additions & 0 deletions test/plausible_web/controllers/auth_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,12 @@ defmodule PlausibleWeb.AuthControllerTest do

assert redirected_to(conn, 302) == "/settings"
end

test "renders form with error if form validations fail", %{conn: conn, user: user} do
conn = put(conn, "/settings", %{"user" => %{"name" => ""}})

assert html_response(conn, 200) =~ "can't be blank"
end
end

describe "DELETE /me" do
Expand Down

0 comments on commit 65f0ce3

Please sign in to comment.