From 4c8c0461be1fec2dd3010e44679dff4d71003286 Mon Sep 17 00:00:00 2001 From: Duong Nguyen <45255870+loop-index@users.noreply.github.com> Date: Wed, 20 Nov 2024 13:25:53 -0500 Subject: [PATCH] [Enhancement] Add option to use existing Media Profile as template for new profile (#466) * Add option to use existing Media Profile as template for new profile * Forgot to commit the edit form too * Reset deletion mark on Source controller * Add test for new preload profile feature * mix check --- .../media_profile_controller.ex | 20 ++++++++++++++++--- .../media_profiles/media_profile_html.ex | 1 + .../actions_dropdown.html.heex | 5 +++++ .../media_profile_html/edit.html.heex | 2 +- .../media_profile_form.html.heex | 1 + .../media_profile_html/new.html.heex | 2 +- .../controllers/sources/source_controller.ex | 3 ++- .../media_profile_controller_test.exs | 9 +++++++++ 8 files changed, 37 insertions(+), 6 deletions(-) diff --git a/lib/pinchflat_web/controllers/media_profiles/media_profile_controller.ex b/lib/pinchflat_web/controllers/media_profiles/media_profile_controller.ex index 34b7bf8..b0b1a3e 100644 --- a/lib/pinchflat_web/controllers/media_profiles/media_profile_controller.ex +++ b/lib/pinchflat_web/controllers/media_profiles/media_profile_controller.ex @@ -17,10 +17,24 @@ defmodule PinchflatWeb.MediaProfiles.MediaProfileController do render(conn, :index, media_profiles: media_profiles) end - def new(conn, _params) do - changeset = Profiles.change_media_profile(%MediaProfile{}) + def new(conn, params) do + # Preload an existing media profile for faster creation + cs_struct = + case to_string(params["template_id"]) do + "" -> %MediaProfile{} + template_id -> Repo.get(MediaProfile, template_id) || %MediaProfile{} + end - render(conn, :new, changeset: changeset, layout: get_onboarding_layout()) + render(conn, :new, + layout: get_onboarding_layout(), + changeset: + Profiles.change_media_profile(%MediaProfile{ + cs_struct + | id: nil, + name: nil, + marked_for_deletion_at: nil + }) + ) end def create(conn, %{"media_profile" => media_profile_params}) do diff --git a/lib/pinchflat_web/controllers/media_profiles/media_profile_html.ex b/lib/pinchflat_web/controllers/media_profiles/media_profile_html.ex index 13e4ca1..fbf3b76 100644 --- a/lib/pinchflat_web/controllers/media_profiles/media_profile_html.ex +++ b/lib/pinchflat_web/controllers/media_profiles/media_profile_html.ex @@ -10,6 +10,7 @@ defmodule PinchflatWeb.MediaProfiles.MediaProfileHTML do """ attr :changeset, Ecto.Changeset, required: true attr :action, :string, required: true + attr :method, :string, required: true def media_profile_form(assigns) diff --git a/lib/pinchflat_web/controllers/media_profiles/media_profile_html/actions_dropdown.html.heex b/lib/pinchflat_web/controllers/media_profiles/media_profile_html/actions_dropdown.html.heex index da058fa..e4f42cf 100644 --- a/lib/pinchflat_web/controllers/media_profiles/media_profile_html/actions_dropdown.html.heex +++ b/lib/pinchflat_web/controllers/media_profiles/media_profile_html/actions_dropdown.html.heex @@ -11,6 +11,11 @@ <.icon name="hero-check" class="ml-2 h-4 w-4" /> + <:option> + <.link href={~p"/media_profiles/new?template_id=#{@media_profile}"} method="get"> + Use as Template + + <:option>
diff --git a/lib/pinchflat_web/controllers/media_profiles/media_profile_html/edit.html.heex b/lib/pinchflat_web/controllers/media_profiles/media_profile_html/edit.html.heex index 27459f0..a56fea8 100644 --- a/lib/pinchflat_web/controllers/media_profiles/media_profile_html/edit.html.heex +++ b/lib/pinchflat_web/controllers/media_profiles/media_profile_html/edit.html.heex @@ -10,7 +10,7 @@
- <.media_profile_form changeset={@changeset} action={~p"/media_profiles/#{@media_profile}"} /> + <.media_profile_form changeset={@changeset} action={~p"/media_profiles/#{@media_profile}"} method="patch" />
diff --git a/lib/pinchflat_web/controllers/media_profiles/media_profile_html/media_profile_form.html.heex b/lib/pinchflat_web/controllers/media_profiles/media_profile_html/media_profile_form.html.heex index 778d674..18e2ed1 100644 --- a/lib/pinchflat_web/controllers/media_profiles/media_profile_html/media_profile_form.html.heex +++ b/lib/pinchflat_web/controllers/media_profiles/media_profile_html/media_profile_form.html.heex @@ -2,6 +2,7 @@ :let={f} for={@changeset} action={@action} + method={@method} x-data="{ advancedMode: !!JSON.parse(localStorage.getItem('advancedMode')) }" x-init="$watch('advancedMode', value => localStorage.setItem('advancedMode', JSON.stringify(value)))" > diff --git a/lib/pinchflat_web/controllers/media_profiles/media_profile_html/new.html.heex b/lib/pinchflat_web/controllers/media_profiles/media_profile_html/new.html.heex index 0fe77fe..e301d73 100644 --- a/lib/pinchflat_web/controllers/media_profiles/media_profile_html/new.html.heex +++ b/lib/pinchflat_web/controllers/media_profiles/media_profile_html/new.html.heex @@ -8,7 +8,7 @@
- <.media_profile_form changeset={@changeset} action={~p"/media_profiles"} /> + <.media_profile_form changeset={@changeset} action={~p"/media_profiles"} method="post" />
diff --git a/lib/pinchflat_web/controllers/sources/source_controller.ex b/lib/pinchflat_web/controllers/sources/source_controller.ex index 9c48cec..f8b63b5 100644 --- a/lib/pinchflat_web/controllers/sources/source_controller.ex +++ b/lib/pinchflat_web/controllers/sources/source_controller.ex @@ -67,7 +67,8 @@ defmodule PinchflatWeb.Sources.SourceController do collection_name: nil, collection_id: nil, collection_type: nil, - original_url: nil + original_url: nil, + marked_for_deletion_at: nil }) ) end diff --git a/test/pinchflat_web/controllers/media_profile_controller_test.exs b/test/pinchflat_web/controllers/media_profile_controller_test.exs index 8c908de..2c3dcfc 100644 --- a/test/pinchflat_web/controllers/media_profile_controller_test.exs +++ b/test/pinchflat_web/controllers/media_profile_controller_test.exs @@ -79,6 +79,15 @@ defmodule PinchflatWeb.MediaProfileControllerTest do refute html_response(conn, 200) =~ "MENU" end + + test "preloads some attributes when using a template", %{conn: conn} do + profile = media_profile_fixture(name: "My first profile", download_subs: true, sub_langs: "de") + + conn = get(conn, ~p"/media_profiles/new", %{"template_id" => profile.id}) + assert html_response(conn, 200) =~ "New Media Profile" + assert html_response(conn, 200) =~ profile.sub_langs + refute html_response(conn, 200) =~ profile.name + end end describe "edit media_profile" do