diff --git a/lib/arrow_web/controllers/stop_controller.ex b/lib/arrow_web/controllers/stop_controller.ex index e2ebc821..891be041 100644 --- a/lib/arrow_web/controllers/stop_controller.ex +++ b/lib/arrow_web/controllers/stop_controller.ex @@ -29,4 +29,25 @@ defmodule ArrowWeb.StopController do |> redirect(to: ~p"/stops/new") end end + + @spec update(Conn.t(), Conn.params()) :: Conn.t() + def update(conn, %{"id" => id, "stop" => stop_params}) do + stop = Stops.get_stop!(id) + + case Stops.update_stop(stop, stop_params) do + {:ok, _stop} -> + conn + |> put_flash(:info, "Stop updated successfully.") + |> redirect(to: ~p"/stops") + + {:error, %Ecto.Changeset{} = changeset} -> + conn + |> put_flash( + :errors, + {"Error updating stop, please try again", + ErrorHelpers.changeset_error_messages(changeset)} + ) + |> redirect(to: ~p"/stops/#{id}/edit") + end + end end diff --git a/lib/arrow_web/live/stop_live/stop_live.ex b/lib/arrow_web/live/stop_live/stop_live.ex index 35a7f0a8..664c0b29 100644 --- a/lib/arrow_web/live/stop_live/stop_live.ex +++ b/lib/arrow_web/live/stop_live/stop_live.ex @@ -70,7 +70,7 @@ defmodule ArrowWeb.StopViewLive do socket |> assign(:form, form) |> assign(:form_action, "edit") - |> assign(:http_action, nil) + |> assign(:http_action, ~p"/stops/#{id}") |> assign(:stop, stop) |> assign(:title, "edit shuttle stop") |> assign(:stop_map_props, stop) @@ -99,6 +99,16 @@ defmodule ArrowWeb.StopViewLive do {:ok, socket} end + def handle_changeset(socket, changeset) do + case Ecto.Changeset.apply_action(changeset, :validate) do + {:ok, _} -> + {:noreply, assign(socket, form: to_form(changeset), trigger_submit: true)} + + {:error, applied_changeset} -> + {:noreply, assign(socket, form: to_form(applied_changeset), trigger_submit: false)} + end + end + def handle_event("validate", %{"stop" => stop_params}, socket) do form = Stops.change_stop(%Stop{}, stop_params) |> to_form(action: :validate) @@ -109,28 +119,14 @@ defmodule ArrowWeb.StopViewLive do def handle_event("edit", %{"stop" => stop_params}, socket) do stop = Stops.get_stop!(socket.assigns.stop.id) + changeset = Stops.change_stop(stop, stop_params) - case Stops.update_stop(stop, stop_params) do - {:ok, _stop} -> - {:noreply, - socket - |> put_flash(:info, "Stop edited successfully.") - |> redirect(to: ~p"/stops/")} - - {:error, %Ecto.Changeset{} = changeset} -> - {:noreply, assign(socket, form: to_form(changeset))} - end + handle_changeset(socket, changeset) end def handle_event("create", %{"stop" => stop_params}, socket) do changeset = Stops.change_stop(%Stop{}, stop_params) - case Ecto.Changeset.apply_action(changeset, :validate) do - {:ok, _} -> - {:noreply, assign(socket, form: to_form(changeset), trigger_submit: true)} - - {:error, applied_changeset} -> - {:noreply, assign(socket, form: to_form(applied_changeset), trigger_submit: false)} - end + handle_changeset(socket, changeset) end end diff --git a/lib/arrow_web/router.ex b/lib/arrow_web/router.ex index 31ab89d0..0121d7d9 100644 --- a/lib/arrow_web/router.ex +++ b/lib/arrow_web/router.ex @@ -52,6 +52,7 @@ defmodule ArrowWeb.Router do live("/stops/new", StopViewLive, :new) live("/stops/:id/edit", StopViewLive, :edit) get("/stops", StopController, :index) + post("/stops/:id", StopController, :update) post("/stops", StopController, :create) resources("/shapes", ShapeController, only: [:delete, :index, :show]) get("/shapes_upload", ShapeController, :new) diff --git a/test/arrow_web/live/stop_live/stop_live_test.exs b/test/arrow_web/live/stop_live/stop_live_test.exs index da7de093..3bb5773c 100644 --- a/test/arrow_web/live/stop_live/stop_live_test.exs +++ b/test/arrow_web/live/stop_live/stop_live_test.exs @@ -113,20 +113,27 @@ defmodule ArrowWeb.StopLiveTest do test "redirects when data is valid", %{conn: conn, stop: stop} do {:ok, edit_live, _html} = live(conn, ~p"/stops/#{stop}/edit") - assert edit_live - |> form("#stop-form", stop: @update_attrs) - |> render_submit() + form = + edit_live + |> form("#stop-form", stop: @update_attrs) + + assert render_submit(form) =~ ~r/phx-trigger-action/ - assert_redirect(edit_live, ~p"/stops/") + conn = follow_trigger_action(form, conn) + assert conn.method == "POST" + params = Enum.map(@update_attrs, fn {k, v} -> {"#{k}", v} end) |> Enum.into(%{}) + assert conn.params == %{"stop" => params, "id" => "#{stop.id}"} end @tag :authenticated_admin test "renders errors when data is invalid", %{conn: conn, stop: stop} do {:ok, edit_live, _html} = live(conn, ~p"/stops/#{stop}/edit") - assert edit_live - |> form("#stop-form", stop: @invalid_attrs) - |> render_submit() + form = + edit_live + |> form("#stop-form", stop: @invalid_attrs) + + refute render_submit(form) =~ ~r/phx-trigger-action/ html = render(edit_live) assert html =~ "edit shuttle stop"