From 31a47cdd0ef330a64e6f2e10db8e0fc255389a8e Mon Sep 17 00:00:00 2001
From: Kieran Eglin
Date: Mon, 11 Mar 2024 14:59:31 -0700
Subject: [PATCH] Restructured the way onboarding changes settings
---
.../controllers/pages/page_controller.ex | 30 ++++++++-----------
.../page_html/onboarding_checklist.html.heex | 2 +-
.../controllers/page_controller_test.exs | 30 ++++---------------
3 files changed, 19 insertions(+), 43 deletions(-)
diff --git a/lib/pinchflat_web/controllers/pages/page_controller.ex b/lib/pinchflat_web/controllers/pages/page_controller.ex
index 3f9ccf8..50d1a46 100644
--- a/lib/pinchflat_web/controllers/pages/page_controller.ex
+++ b/lib/pinchflat_web/controllers/pages/page_controller.ex
@@ -8,38 +8,34 @@ defmodule PinchflatWeb.Pages.PageController do
alias Pinchflat.Profiles.MediaProfile
def home(conn, params) do
- force_onboarding = params["onboarding"]
- media_profiles_exist = Repo.exists?(MediaProfile)
- sources_exist = Repo.exists?(Source)
+ done_onboarding = params["onboarding"] == "0"
+ force_onboarding = params["onboarding"] == "1"
- if !force_onboarding && media_profiles_exist && sources_exist do
- render_home_page(conn)
+ if done_onboarding, do: Settings.set!(:onboarding, false)
+
+ if force_onboarding || Settings.get!(:onboarding) do
+ render_onboarding_page(conn)
else
- render_onboarding_page(conn, media_profiles_exist, sources_exist)
+ render_home_page(conn)
end
end
defp render_home_page(conn) do
- Settings.set!(:onboarding, false)
- media_profile_count = Repo.aggregate(MediaProfile, :count, :id)
- source_count = Repo.aggregate(Source, :count, :id)
- media_item_count = Repo.aggregate(MediaItem, :count, :id)
-
conn
|> render(:home,
- media_profile_count: media_profile_count,
- source_count: source_count,
- media_item_count: media_item_count
+ media_profile_count: Repo.aggregate(MediaProfile, :count, :id),
+ source_count: Repo.aggregate(Source, :count, :id),
+ media_item_count: Repo.aggregate(MediaItem, :count, :id)
)
end
- defp render_onboarding_page(conn, media_profiles_exist, sources_exist) do
+ defp render_onboarding_page(conn) do
Settings.set!(:onboarding, true)
conn
|> render(:onboarding_checklist,
- media_profiles_exist: media_profiles_exist,
- sources_exist: sources_exist,
+ media_profiles_exist: Repo.exists?(MediaProfile),
+ sources_exist: Repo.exists?(Source),
layout: {Layouts, :onboarding}
)
end
diff --git a/lib/pinchflat_web/controllers/pages/page_html/onboarding_checklist.html.heex b/lib/pinchflat_web/controllers/pages/page_html/onboarding_checklist.html.heex
index e336ce5..d4682be 100644
--- a/lib/pinchflat_web/controllers/pages/page_html/onboarding_checklist.html.heex
+++ b/lib/pinchflat_web/controllers/pages/page_html/onboarding_checklist.html.heex
@@ -39,7 +39,7 @@
Feel free to add more Media Profiles or Sources in the meantime!
- <.link href={~p"/"}>
+ <.link href={~p"/?onboarding=0"}>
<.button color="bg-primary" rounding="rounded-full" disabled={not @sources_exist}>
Let's Go 🚀
diff --git a/test/pinchflat_web/controllers/page_controller_test.exs b/test/pinchflat_web/controllers/page_controller_test.exs
index ab1b536..3037764 100644
--- a/test/pinchflat_web/controllers/page_controller_test.exs
+++ b/test/pinchflat_web/controllers/page_controller_test.exs
@@ -1,51 +1,31 @@
defmodule PinchflatWeb.PageControllerTest do
use PinchflatWeb.ConnCase
- import Pinchflat.ProfilesFixtures
- import Pinchflat.SourcesFixtures
-
alias Pinchflat.Settings
describe "GET / when testing onboarding" do
- test "sets the onboarding session to true when onboarding", %{conn: conn} do
+ test "sets the onboarding setting to true when onboarding", %{conn: conn} do
_conn = get(conn, ~p"/")
assert Settings.get!(:onboarding)
end
- test "displays the onboarding page when no media profiles exist", %{conn: conn} do
- conn = get(conn, ~p"/")
- assert html_response(conn, 200) =~ "Welcome to Pinchflat"
- end
-
- test "displays the onboarding page when no sources exist", %{conn: conn} do
- _ = media_profile_fixture()
-
- conn = get(conn, ~p"/")
- assert html_response(conn, 200) =~ "Welcome to Pinchflat"
- end
-
test "displays the onboarding page when onboarding is forced", %{conn: conn} do
- _ = media_profile_fixture()
- _ = source_fixture()
+ Settings.set!(:onboarding, false)
conn = get(conn, ~p"/?onboarding=1")
assert html_response(conn, 200) =~ "Welcome to Pinchflat"
end
- test "sets the onboarding session to false when not onboarding", %{conn: conn} do
+ test "sets the onboarding setting to false if you pass the corrent query param", %{conn: conn} do
conn = get(conn, ~p"/")
assert Settings.get!(:onboarding)
- _ = media_profile_fixture()
- _ = source_fixture()
-
- _conn = get(conn, ~p"/")
+ _conn = get(conn, ~p"/?onboarding=0")
refute Settings.get!(:onboarding)
end
test "displays the home page when not onboarding", %{conn: conn} do
- _ = media_profile_fixture()
- _ = source_fixture()
+ Settings.set!(:onboarding, false)
conn = get(conn, ~p"/")
assert html_response(conn, 200) =~ "MENU"