Skip to content

Commit

Permalink
chore: create/update stop within live session
Browse files Browse the repository at this point in the history
  • Loading branch information
Whoops committed Nov 1, 2024
1 parent d0ca145 commit 42121d3
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 27 deletions.
18 changes: 18 additions & 0 deletions lib/arrow/stops.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ defmodule Arrow.Stops do
"""
def get_stop!(id), do: Repo.get!(Stop, id)

@doc """
Gets a single stop by stop_id.
Returns nil if no stop exists with the given stop_id.
## Examples
iex> get_stop_by_stop_id("123")
%Stop{}
iex> get_stop_by_stop_id("456")
nil
"""
def get_stop_by_stop_id(stop_id) do
Repo.get_by(Stop, stop_id: stop_id)
end

@doc """
Creates a stop.
Expand Down
35 changes: 20 additions & 15 deletions lib/arrow_web/live/stop_live/stop_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,6 @@ 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(socket.assigns.stop, stop_params) |> to_form(action: :validate)

Expand All @@ -138,14 +128,29 @@ 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)

handle_changeset(socket, changeset)
case Arrow.Stops.update_stop(stop, stop_params) do
{:ok, _stop} ->
{:noreply,
socket
|> put_flash(:info, "Stop edited successfully")
|> redirect(to: ~p"/stops")}

{:error, changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
end
end

def handle_event("create", %{"stop" => stop_params}, socket) do
changeset = Stops.change_stop(%Stop{}, stop_params)

handle_changeset(socket, changeset)
case Arrow.Stops.create_stop(stop_params) do
{:ok, _stop} ->
{:noreply,
socket
|> put_flash(:info, "Stop created successfully")
|> redirect(to: ~p"/stops")}

{:error, changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
end
end
end
9 changes: 9 additions & 0 deletions test/arrow/stops_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,14 @@ defmodule Arrow.StopsTest do
stop = stop_fixture()
assert %Ecto.Changeset{} = Stops.change_stop(stop)
end

test "get_stop_by_stop_id/1 returns stop when found" do
stop = stop_fixture()
assert Stops.get_stop_by_stop_id(stop.stop_id) == stop
end

test "get_stop_by_stop_id/1 returns nil when stop not found" do
assert Stops.get_stop_by_stop_id("nonexistent") == nil
end
end
end
27 changes: 15 additions & 12 deletions test/arrow_web/live/stop_live/stop_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ defmodule ArrowWeb.StopLiveTest do
import Phoenix.LiveViewTest
import Arrow.StopsFixtures

alias Arrow.Shuttles.Stop

@create_attrs %{
stop_id: "some stop_id",
stop_name: "some stop_name",
Expand Down Expand Up @@ -64,19 +66,20 @@ defmodule ArrowWeb.StopLiveTest do

describe "create stop" do
@tag :authenticated_admin
test "redirects to index when data is valid", %{conn: conn} do
test "creates stop and redirects to index when data is valid", %{conn: conn} do
{:ok, new_live, _html} = live(conn, ~p"/stops/new")

form =
new_live
|> form("#stop-form", stop: @create_attrs)

assert render_submit(form) =~ ~r/phx-trigger-action/
render_submit(form)

assert_redirected(new_live, "/stops")

conn = follow_trigger_action(form, conn)
assert conn.method == "POST"
params = Enum.map(@create_attrs, fn {k, v} -> {"#{k}", v} end) |> Enum.into(%{})
assert conn.params == %{"stop" => params}
stop_id = @create_attrs[:stop_id]

assert %Stop{stop_id: ^stop_id} = Arrow.Stops.get_stop_by_stop_id(stop_id)
end

@tag :authenticated_admin
Expand Down Expand Up @@ -110,19 +113,19 @@ defmodule ArrowWeb.StopLiveTest do
setup [:create_stop]

@tag :authenticated_admin
test "redirects when data is valid", %{conn: conn, stop: stop} do
test "updates and redirects when data is valid", %{conn: conn, stop: stop} do
{:ok, edit_live, _html} = live(conn, ~p"/stops/#{stop}/edit")

form =
edit_live
|> form("#stop-form", stop: @update_attrs)
|> render_submit()

assert_redirected(edit_live, ~p"/stops")

assert render_submit(form) =~ ~r/phx-trigger-action/
stop_id = @update_attrs[:stop_id]

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}
assert %Stop{stop_id: ^stop_id} = Arrow.Stops.get_stop_by_stop_id(stop_id)
end

@tag :authenticated_admin
Expand Down

0 comments on commit 42121d3

Please sign in to comment.