Skip to content

Commit

Permalink
wip: move create / edit into live view
Browse files Browse the repository at this point in the history
  • Loading branch information
lemald committed Nov 1, 2024
1 parent 7c600fb commit 9125867
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions lib/arrow_web/live/shuttle_live/shuttle_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,6 @@ defmodule ArrowWeb.ShuttleViewLive do
{:ok, socket}
end

defp 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", %{"shuttle_route" => shuttle_route_params}, socket) do
form =
%Shuttle{} |> Shuttles.change_shuttle(shuttle_route_params) |> to_form(action: :validate)
Expand All @@ -169,12 +159,30 @@ defmodule ArrowWeb.ShuttleViewLive do
shuttle = Shuttles.get_shuttle!(socket.assigns.shuttle_route.id)
changeset = Shuttles.change_shuttle(shuttle, shuttle_route_params)

handle_changeset(socket, changeset)
case Arrow.Repo.update(changeset) do
{:ok, shuttle} ->
{:noreply,
socket
|> put_flash(:info, "Shuttle updated successfully")
|> redirect(to: ~p"/shuttles/#{shuttle}")}

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

def handle_event("create", %{"shuttle_route" => shuttle_route_params}, socket) do
changeset = Shuttles.change_shuttle(%Shuttle{}, shuttle_route_params)

handle_changeset(socket, changeset)
case Arrow.Repo.insert(changeset) do
{:ok, shuttle} ->
{:noreply,
socket
|> put_flash(:info, "Shuttle created successfully")
|> redirect(to: ~p"/shuttles/#{shuttle}")}

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

0 comments on commit 9125867

Please sign in to comment.