Skip to content

Commit

Permalink
feat: add update endpoint for stop_controller, use in stop_live
Browse files Browse the repository at this point in the history
  • Loading branch information
meagharty committed Sep 16, 2024
1 parent 0b425fd commit 66280a2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 25 deletions.
21 changes: 21 additions & 0 deletions lib/arrow_web/controllers/stop_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 14 additions & 18 deletions lib/arrow_web/live/stop_live/stop_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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
1 change: 1 addition & 0 deletions lib/arrow_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 14 additions & 7 deletions test/arrow_web/live/stop_live/stop_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 66280a2

Please sign in to comment.