Update onboarding flow to use settings model instead of session data
This commit is contained in:
parent
970f12792a
commit
11a95443ed
9 changed files with 83 additions and 75 deletions
|
|
@ -45,6 +45,7 @@ defmodule PinchflatWeb do
|
||||||
import Plug.Conn
|
import Plug.Conn
|
||||||
import PinchflatWeb.Gettext
|
import PinchflatWeb.Gettext
|
||||||
|
|
||||||
|
alias Pinchflat.Settings
|
||||||
alias PinchflatWeb.Layouts
|
alias PinchflatWeb.Layouts
|
||||||
|
|
||||||
unquote(verified_routes())
|
unquote(verified_routes())
|
||||||
|
|
@ -56,6 +57,8 @@ defmodule PinchflatWeb do
|
||||||
use Phoenix.LiveView,
|
use Phoenix.LiveView,
|
||||||
layout: {PinchflatWeb.Layouts, :app}
|
layout: {PinchflatWeb.Layouts, :app}
|
||||||
|
|
||||||
|
alias Pinchflat.Settings
|
||||||
|
|
||||||
unquote(html_helpers())
|
unquote(html_helpers())
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -64,6 +67,8 @@ defmodule PinchflatWeb do
|
||||||
quote do
|
quote do
|
||||||
use Phoenix.LiveComponent
|
use Phoenix.LiveComponent
|
||||||
|
|
||||||
|
alias Pinchflat.Settings
|
||||||
|
|
||||||
unquote(html_helpers())
|
unquote(html_helpers())
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -76,6 +81,8 @@ defmodule PinchflatWeb do
|
||||||
import Phoenix.Controller,
|
import Phoenix.Controller,
|
||||||
only: [get_csrf_token: 0, view_module: 1, view_template: 1]
|
only: [get_csrf_token: 0, view_module: 1, view_template: 1]
|
||||||
|
|
||||||
|
alias Pinchflat.Settings
|
||||||
|
|
||||||
# Include general helpers for rendering HTML
|
# Include general helpers for rendering HTML
|
||||||
unquote(html_helpers())
|
unquote(html_helpers())
|
||||||
end
|
end
|
||||||
|
|
@ -93,6 +100,7 @@ defmodule PinchflatWeb do
|
||||||
import PinchflatWeb.CustomComponents.TableComponents
|
import PinchflatWeb.CustomComponents.TableComponents
|
||||||
import PinchflatWeb.CustomComponents.ButtonComponents
|
import PinchflatWeb.CustomComponents.ButtonComponents
|
||||||
|
|
||||||
|
alias Pinchflat.Settings
|
||||||
alias Pinchflat.Utils.StringUtils
|
alias Pinchflat.Utils.StringUtils
|
||||||
|
|
||||||
# Shortcut for generating JS commands
|
# Shortcut for generating JS commands
|
||||||
|
|
|
||||||
|
|
@ -13,29 +13,21 @@ defmodule PinchflatWeb.MediaProfiles.MediaProfileController do
|
||||||
def new(conn, _params) do
|
def new(conn, _params) do
|
||||||
changeset = Profiles.change_media_profile(%MediaProfile{})
|
changeset = Profiles.change_media_profile(%MediaProfile{})
|
||||||
|
|
||||||
if get_session(conn, :onboarding) do
|
render(conn, :new, changeset: changeset, layout: get_onboarding_layout())
|
||||||
render(conn, :new, changeset: changeset, layout: {Layouts, :onboarding})
|
|
||||||
else
|
|
||||||
render(conn, :new, changeset: changeset)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def create(conn, %{"media_profile" => media_profile_params}) do
|
def create(conn, %{"media_profile" => media_profile_params}) do
|
||||||
case Profiles.create_media_profile(media_profile_params) do
|
case Profiles.create_media_profile(media_profile_params) do
|
||||||
{:ok, media_profile} ->
|
{:ok, media_profile} ->
|
||||||
redirect_location =
|
redirect_location =
|
||||||
if get_session(conn, :onboarding), do: ~p"/?onboarding=1", else: ~p"/media_profiles/#{media_profile}"
|
if Settings.get!(:onboarding), do: ~p"/?onboarding=1", else: ~p"/media_profiles/#{media_profile}"
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> put_flash(:info, "Media profile created successfully.")
|
|> put_flash(:info, "Media profile created successfully.")
|
||||||
|> redirect(to: redirect_location)
|
|> redirect(to: redirect_location)
|
||||||
|
|
||||||
{:error, %Ecto.Changeset{} = changeset} ->
|
{:error, %Ecto.Changeset{} = changeset} ->
|
||||||
if get_session(conn, :onboarding) do
|
render(conn, :new, changeset: changeset, layout: get_onboarding_layout())
|
||||||
render(conn, :new, changeset: changeset, layout: {Layouts, :onboarding})
|
|
||||||
else
|
|
||||||
render(conn, :new, changeset: changeset)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -85,4 +77,12 @@ defmodule PinchflatWeb.MediaProfiles.MediaProfileController do
|
||||||
|> put_flash(:info, flash_message)
|
|> put_flash(:info, flash_message)
|
||||||
|> redirect(to: ~p"/media_profiles")
|
|> redirect(to: ~p"/media_profiles")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp get_onboarding_layout do
|
||||||
|
if Settings.get!(:onboarding) do
|
||||||
|
{Layouts, :onboarding}
|
||||||
|
else
|
||||||
|
{Layouts, :app}
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
<div class="mb-6 flex gap-3 flex-row items-center">
|
<div class="mb-6 flex gap-3 flex-row items-center">
|
||||||
<.link :if={!Plug.Conn.get_session(@conn, :onboarding)} navigate={~p"/media_profiles"}>
|
<.link :if={!Settings.get!(:onboarding)} navigate={~p"/media_profiles"}>
|
||||||
<.icon name="hero-arrow-left" class="w-10 h-10 hover:dark:text-white" />
|
<.icon name="hero-arrow-left" class="w-10 h-10 hover:dark:text-white" />
|
||||||
</.link>
|
</.link>
|
||||||
<h2 class="text-title-md2 font-bold text-black dark:text-white ml-4">New Media Profile</h2>
|
<h2 class="text-title-md2 font-bold text-black dark:text-white ml-4">New Media Profile</h2>
|
||||||
|
|
|
||||||
|
|
@ -20,12 +20,12 @@ defmodule PinchflatWeb.Pages.PageController do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp render_home_page(conn) do
|
defp render_home_page(conn) do
|
||||||
|
Settings.set!(:onboarding, false)
|
||||||
media_profile_count = Repo.aggregate(MediaProfile, :count, :id)
|
media_profile_count = Repo.aggregate(MediaProfile, :count, :id)
|
||||||
source_count = Repo.aggregate(Source, :count, :id)
|
source_count = Repo.aggregate(Source, :count, :id)
|
||||||
media_item_count = Repo.aggregate(MediaItem, :count, :id)
|
media_item_count = Repo.aggregate(MediaItem, :count, :id)
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> put_session(:onboarding, false)
|
|
||||||
|> render(:home,
|
|> render(:home,
|
||||||
media_profile_count: media_profile_count,
|
media_profile_count: media_profile_count,
|
||||||
source_count: source_count,
|
source_count: source_count,
|
||||||
|
|
@ -34,8 +34,9 @@ defmodule PinchflatWeb.Pages.PageController do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp render_onboarding_page(conn, media_profiles_exist, sources_exist) do
|
defp render_onboarding_page(conn, media_profiles_exist, sources_exist) do
|
||||||
|
Settings.set!(:onboarding, true)
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> put_session(:onboarding, true)
|
|
||||||
|> render(:onboarding_checklist,
|
|> render(:onboarding_checklist,
|
||||||
media_profiles_exist: media_profiles_exist,
|
media_profiles_exist: media_profiles_exist,
|
||||||
sources_exist: sources_exist,
|
sources_exist: sources_exist,
|
||||||
|
|
|
||||||
|
|
@ -16,37 +16,29 @@ defmodule PinchflatWeb.Sources.SourceController do
|
||||||
def new(conn, _params) do
|
def new(conn, _params) do
|
||||||
changeset = Sources.change_source(%Source{})
|
changeset = Sources.change_source(%Source{})
|
||||||
|
|
||||||
if get_session(conn, :onboarding) do
|
render(conn, :new,
|
||||||
render(conn, :new,
|
changeset: changeset,
|
||||||
changeset: changeset,
|
media_profiles: media_profiles(),
|
||||||
media_profiles: media_profiles(),
|
layout: get_onboarding_layout()
|
||||||
layout: {Layouts, :onboarding}
|
)
|
||||||
)
|
|
||||||
else
|
|
||||||
render(conn, :new, changeset: changeset, media_profiles: media_profiles())
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def create(conn, %{"source" => source_params}) do
|
def create(conn, %{"source" => source_params}) do
|
||||||
case Sources.create_source(source_params) do
|
case Sources.create_source(source_params) do
|
||||||
{:ok, source} ->
|
{:ok, source} ->
|
||||||
redirect_location =
|
redirect_location =
|
||||||
if get_session(conn, :onboarding), do: ~p"/?onboarding=1", else: ~p"/sources/#{source}"
|
if Settings.get!(:onboarding), do: ~p"/?onboarding=1", else: ~p"/sources/#{source}"
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> put_flash(:info, "Source created successfully.")
|
|> put_flash(:info, "Source created successfully.")
|
||||||
|> redirect(to: redirect_location)
|
|> redirect(to: redirect_location)
|
||||||
|
|
||||||
{:error, %Ecto.Changeset{} = changeset} ->
|
{:error, %Ecto.Changeset{} = changeset} ->
|
||||||
if get_session(conn, :onboarding) do
|
render(conn, :new,
|
||||||
render(conn, :new,
|
changeset: changeset,
|
||||||
changeset: changeset,
|
media_profiles: media_profiles(),
|
||||||
media_profiles: media_profiles(),
|
layout: get_onboarding_layout()
|
||||||
layout: {Layouts, :onboarding}
|
)
|
||||||
)
|
|
||||||
else
|
|
||||||
render(conn, :new, changeset: changeset, media_profiles: media_profiles())
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -107,4 +99,12 @@ defmodule PinchflatWeb.Sources.SourceController do
|
||||||
defp media_profiles do
|
defp media_profiles do
|
||||||
Profiles.list_media_profiles()
|
Profiles.list_media_profiles()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp get_onboarding_layout do
|
||||||
|
if Settings.get!(:onboarding) do
|
||||||
|
{Layouts, :onboarding}
|
||||||
|
else
|
||||||
|
{Layouts, :app}
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
<div class="mb-6 flex gap-3 flex-row items-center">
|
<div class="mb-6 flex gap-3 flex-row items-center">
|
||||||
<.link :if={!Plug.Conn.get_session(@conn, :onboarding)} navigate={~p"/sources"}>
|
<.link :if={!Settings.get!(:onboarding)} navigate={~p"/sources"}>
|
||||||
<.icon name="hero-arrow-left" class="w-10 h-10 hover:dark:text-white" />
|
<.icon name="hero-arrow-left" class="w-10 h-10 hover:dark:text-white" />
|
||||||
</.link>
|
</.link>
|
||||||
<h2 class="text-title-md2 font-bold text-black dark:text-white ml-4">New Source</h2>
|
<h2 class="text-title-md2 font-bold text-black dark:text-white ml-4">New Source</h2>
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ defmodule PinchflatWeb.MediaProfileControllerTest do
|
||||||
import Pinchflat.ProfilesFixtures
|
import Pinchflat.ProfilesFixtures
|
||||||
|
|
||||||
alias Pinchflat.Repo
|
alias Pinchflat.Repo
|
||||||
|
alias Pinchflat.Settings
|
||||||
|
|
||||||
@create_attrs %{name: "some name", output_path_template: "some output_path_template"}
|
@create_attrs %{name: "some name", output_path_template: "some output_path_template"}
|
||||||
@update_attrs %{
|
@update_attrs %{
|
||||||
|
|
@ -14,6 +15,12 @@ defmodule PinchflatWeb.MediaProfileControllerTest do
|
||||||
}
|
}
|
||||||
@invalid_attrs %{name: nil, output_path_template: nil}
|
@invalid_attrs %{name: nil, output_path_template: nil}
|
||||||
|
|
||||||
|
setup do
|
||||||
|
Settings.set!(:onboarding, false)
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
describe "index" do
|
describe "index" do
|
||||||
test "lists all media_profiles", %{conn: conn} do
|
test "lists all media_profiles", %{conn: conn} do
|
||||||
conn = get(conn, ~p"/media_profiles")
|
conn = get(conn, ~p"/media_profiles")
|
||||||
|
|
@ -27,13 +34,11 @@ defmodule PinchflatWeb.MediaProfileControllerTest do
|
||||||
assert html_response(conn, 200) =~ "New Media Profile"
|
assert html_response(conn, 200) =~ "New Media Profile"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "renders correct layout when onboarding", %{session_conn: session_conn} do
|
test "renders correct layout when onboarding", %{conn: conn} do
|
||||||
session_conn =
|
Settings.set!(:onboarding, true)
|
||||||
session_conn
|
conn = get(conn, ~p"/media_profiles/new")
|
||||||
|> put_session(:onboarding, true)
|
|
||||||
|> get(~p"/media_profiles/new")
|
|
||||||
|
|
||||||
refute html_response(session_conn, 200) =~ "MENU"
|
refute html_response(conn, 200) =~ "MENU"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -53,22 +58,18 @@ defmodule PinchflatWeb.MediaProfileControllerTest do
|
||||||
assert html_response(conn, 200) =~ "New Media Profile"
|
assert html_response(conn, 200) =~ "New Media Profile"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "redirects to onboarding when onboarding", %{session_conn: session_conn} do
|
test "redirects to onboarding when onboarding", %{conn: conn} do
|
||||||
session_conn =
|
Settings.set!(:onboarding, true)
|
||||||
session_conn
|
conn = post(conn, ~p"/media_profiles", media_profile: @create_attrs)
|
||||||
|> put_session(:onboarding, true)
|
|
||||||
|> post(~p"/media_profiles", media_profile: @create_attrs)
|
|
||||||
|
|
||||||
assert redirected_to(session_conn) == ~p"/?onboarding=1"
|
assert redirected_to(conn) == ~p"/?onboarding=1"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "renders correct layout on error when onboarding", %{session_conn: session_conn} do
|
test "renders correct layout on error when onboarding", %{conn: conn} do
|
||||||
session_conn =
|
Settings.set!(:onboarding, true)
|
||||||
session_conn
|
conn = post(conn, ~p"/media_profiles", media_profile: @invalid_attrs)
|
||||||
|> put_session(:onboarding, true)
|
|
||||||
|> post(~p"/media_profiles", media_profile: @invalid_attrs)
|
|
||||||
|
|
||||||
refute html_response(session_conn, 200) =~ "MENU"
|
refute html_response(conn, 200) =~ "MENU"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,12 @@ defmodule PinchflatWeb.PageControllerTest do
|
||||||
import Pinchflat.ProfilesFixtures
|
import Pinchflat.ProfilesFixtures
|
||||||
import Pinchflat.SourcesFixtures
|
import Pinchflat.SourcesFixtures
|
||||||
|
|
||||||
|
alias Pinchflat.Settings
|
||||||
|
|
||||||
describe "GET / when testing onboarding" do
|
describe "GET / when testing onboarding" do
|
||||||
test "sets the onboarding session to true when onboarding", %{conn: conn} do
|
test "sets the onboarding session to true when onboarding", %{conn: conn} do
|
||||||
conn = get(conn, ~p"/")
|
_conn = get(conn, ~p"/")
|
||||||
assert get_session(conn, :onboarding)
|
assert Settings.get!(:onboarding)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "displays the onboarding page when no media profiles exist", %{conn: conn} do
|
test "displays the onboarding page when no media profiles exist", %{conn: conn} do
|
||||||
|
|
@ -32,13 +34,13 @@ defmodule PinchflatWeb.PageControllerTest do
|
||||||
|
|
||||||
test "sets the onboarding session to false when not onboarding", %{conn: conn} do
|
test "sets the onboarding session to false when not onboarding", %{conn: conn} do
|
||||||
conn = get(conn, ~p"/")
|
conn = get(conn, ~p"/")
|
||||||
assert get_session(conn, :onboarding)
|
assert Settings.get!(:onboarding)
|
||||||
|
|
||||||
_ = media_profile_fixture()
|
_ = media_profile_fixture()
|
||||||
_ = source_fixture()
|
_ = source_fixture()
|
||||||
|
|
||||||
conn = get(conn, ~p"/")
|
_conn = get(conn, ~p"/")
|
||||||
refute get_session(conn, :onboarding)
|
refute Settings.get!(:onboarding)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "displays the home page when not onboarding", %{conn: conn} do
|
test "displays the home page when not onboarding", %{conn: conn} do
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,11 @@ defmodule PinchflatWeb.SourceControllerTest do
|
||||||
import Pinchflat.ProfilesFixtures
|
import Pinchflat.ProfilesFixtures
|
||||||
|
|
||||||
alias Pinchflat.Repo
|
alias Pinchflat.Repo
|
||||||
|
alias Pinchflat.Settings
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
media_profile = media_profile_fixture()
|
media_profile = media_profile_fixture()
|
||||||
|
Settings.set!(:onboarding, false)
|
||||||
|
|
||||||
{
|
{
|
||||||
:ok,
|
:ok,
|
||||||
|
|
@ -42,13 +44,11 @@ defmodule PinchflatWeb.SourceControllerTest do
|
||||||
assert html_response(conn, 200) =~ "New Source"
|
assert html_response(conn, 200) =~ "New Source"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "renders correct layout when onboarding", %{session_conn: session_conn} do
|
test "renders correct layout when onboarding", %{conn: conn} do
|
||||||
session_conn =
|
Settings.set!(:onboarding, true)
|
||||||
session_conn
|
conn = get(conn, ~p"/sources/new")
|
||||||
|> put_session(:onboarding, true)
|
|
||||||
|> get(~p"/sources/new")
|
|
||||||
|
|
||||||
refute html_response(session_conn, 200) =~ "MENU"
|
refute html_response(conn, 200) =~ "MENU"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -69,24 +69,20 @@ defmodule PinchflatWeb.SourceControllerTest do
|
||||||
assert html_response(conn, 200) =~ "New Source"
|
assert html_response(conn, 200) =~ "New Source"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "redirects to onboarding when onboarding", %{session_conn: session_conn, create_attrs: create_attrs} do
|
test "redirects to onboarding when onboarding", %{conn: conn, create_attrs: create_attrs} do
|
||||||
expect(YtDlpRunnerMock, :run, 1, &runner_function_mock/3)
|
expect(YtDlpRunnerMock, :run, 1, &runner_function_mock/3)
|
||||||
|
|
||||||
session_conn =
|
Settings.set!(:onboarding, true)
|
||||||
session_conn
|
conn = post(conn, ~p"/sources", source: create_attrs)
|
||||||
|> put_session(:onboarding, true)
|
|
||||||
|> post(~p"/sources", source: create_attrs)
|
|
||||||
|
|
||||||
assert redirected_to(session_conn) == ~p"/?onboarding=1"
|
assert redirected_to(conn) == ~p"/?onboarding=1"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "renders correct layout on error when onboarding", %{session_conn: session_conn, invalid_attrs: invalid_attrs} do
|
test "renders correct layout on error when onboarding", %{conn: conn, invalid_attrs: invalid_attrs} do
|
||||||
session_conn =
|
Settings.set!(:onboarding, true)
|
||||||
session_conn
|
conn = post(conn, ~p"/sources", source: invalid_attrs)
|
||||||
|> put_session(:onboarding, true)
|
|
||||||
|> post(~p"/sources", source: invalid_attrs)
|
|
||||||
|
|
||||||
refute html_response(session_conn, 200) =~ "MENU"
|
refute html_response(conn, 200) =~ "MENU"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue