From 089e61a574098e58cb00d97bf9ae31c574b41d52 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Tue, 24 Sep 2024 09:27:35 +0100 Subject: [PATCH] Remove manual record creation --- .../live/record_live/form_component.ex | 15 --------------- lib/music_library_web/live/record_live/index.ex | 7 ------- .../live/record_live/index.html.heex | 12 ++---------- lib/music_library_web/router.ex | 1 - 4 files changed, 2 insertions(+), 33 deletions(-) diff --git a/lib/music_library_web/live/record_live/form_component.ex b/lib/music_library_web/live/record_live/form_component.ex index 0d6be204..71366e00 100644 --- a/lib/music_library_web/live/record_live/form_component.ex +++ b/lib/music_library_web/live/record_live/form_component.ex @@ -80,20 +80,5 @@ defmodule MusicLibraryWeb.RecordLive.FormComponent do end end - defp save_record(socket, :new, record_params) do - case Records.create_record(record_params) do - {:ok, record} -> - notify_parent({:saved, record}) - - {:noreply, - socket - |> put_flash(:info, "Record created successfully") - |> push_patch(to: socket.assigns.patch)} - - {:error, %Ecto.Changeset{} = changeset} -> - {:noreply, assign(socket, form: to_form(changeset))} - end - end - defp notify_parent(msg), do: send(self(), {__MODULE__, msg}) end diff --git a/lib/music_library_web/live/record_live/index.ex b/lib/music_library_web/live/record_live/index.ex index 7bdb63a9..fd669c1c 100644 --- a/lib/music_library_web/live/record_live/index.ex +++ b/lib/music_library_web/live/record_live/index.ex @@ -3,7 +3,6 @@ defmodule MusicLibraryWeb.RecordLive.Index do import MusicLibraryWeb.Pagination alias MusicLibrary.Records - alias MusicLibrary.Records.Record @impl true def mount(params, _session, socket) do @@ -36,12 +35,6 @@ defmodule MusicLibraryWeb.RecordLive.Index do |> assign(:record, Records.get_record!(id)) end - defp apply_action(socket, :new, _params) do - socket - |> assign(:page_title, "New Record") - |> assign(:record, %Record{}) - end - defp apply_action(socket, :index, params) do new_socket = socket diff --git a/lib/music_library_web/live/record_live/index.html.heex b/lib/music_library_web/live/record_live/index.html.heex index 1889fd0f..b2ec60b9 100644 --- a/lib/music_library_web/live/record_live/index.html.heex +++ b/lib/music_library_web/live/record_live/index.html.heex @@ -1,9 +1,6 @@ <.header> Listing Records <:actions> - <.link patch={~p"/records/new"}> - <.button>New Record - <.link patch={~p"/records/search"}> <.button>Search Record @@ -42,15 +39,10 @@ -<.modal - :if={@live_action in [:new, :edit]} - id="record-modal" - show - on_cancel={JS.patch(~p"/records")} -> +<.modal :if={@live_action == :edit} id="record-modal" show on_cancel={JS.patch(~p"/records")}> <.live_component module={MusicLibraryWeb.RecordLive.FormComponent} - id={@record.id || :new} + id={@record.id} title={@page_title} action={@live_action} record={@record} diff --git a/lib/music_library_web/router.ex b/lib/music_library_web/router.ex index eec5afce..f383231f 100644 --- a/lib/music_library_web/router.ex +++ b/lib/music_library_web/router.ex @@ -21,7 +21,6 @@ defmodule MusicLibraryWeb.Router do get "/images/:record_id", ImageController, :show live "/records", RecordLive.Index, :index - live "/records/new", RecordLive.Index, :new live "/records/search", RecordLive.Index, :search live "/records/:id/edit", RecordLive.Index, :edit