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 @@