From dc6b35d16089cd6e2b1e028ee1bbf5117c44fe54 Mon Sep 17 00:00:00 2001 From: duong Date: Sat, 9 Nov 2024 22:28:22 +0000 Subject: [PATCH] Add option to use existing Media Profile as template for new profile --- .../media_profile_controller.ex | 20 ++++++++++++++++--- .../media_profiles/media_profile_html.ex | 1 + .../actions_dropdown.html.heex | 5 +++++ .../media_profile_form.html.heex | 1 + .../media_profile_html/new.html.heex | 2 +- 5 files changed, 25 insertions(+), 4 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..9778998 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/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" />