Skip to content

Commit

Permalink
wip: migrate shuttle page to LiveView
Browse files Browse the repository at this point in the history
  • Loading branch information
lemald committed Nov 1, 2024
1 parent f59c344 commit 73986a8
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 179 deletions.
73 changes: 0 additions & 73 deletions lib/arrow_web/controllers/shuttle_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,87 +2,14 @@ defmodule ArrowWeb.ShuttleController do
use ArrowWeb, :controller

alias Arrow.Shuttles
alias Arrow.Shuttles.Shuttle

def index(conn, _params) do
shuttles = Shuttles.list_shuttles()
render(conn, :index, shuttles: shuttles)
end

def new(conn, _params) do
changeset =
Shuttles.change_shuttle(%Shuttle{
status: :draft,
routes: [%Shuttles.Route{direction_id: :"0"}, %Shuttles.Route{direction_id: :"1"}]
})

gtfs_disruptable_routes = Shuttles.list_disruptable_routes()
shapes = Shuttles.list_shapes()

render(conn, :new,
changeset: changeset,
gtfs_disruptable_routes: gtfs_disruptable_routes,
shapes: shapes
)
end

def create(conn, %{"shuttle" => shuttle_params}) do
case Shuttles.create_shuttle(shuttle_params) do
{:ok, shuttle} ->
conn
|> put_flash(:info, "Shuttle created successfully.")
|> redirect(to: ~p"/shuttles/#{shuttle}")

{:error, %Ecto.Changeset{} = changeset} ->
gtfs_disruptable_routes = Shuttles.list_disruptable_routes()
shapes = Shuttles.list_shapes()

render(conn, :new,
changeset: changeset,
gtfs_disruptable_routes: gtfs_disruptable_routes,
shapes: shapes
)
end
end

def show(conn, %{"id" => id}) do
shuttle = Shuttles.get_shuttle!(id)
render(conn, :show, shuttle: shuttle)
end

def edit(conn, %{"id" => id}) do
shuttle = Shuttles.get_shuttle!(id)
changeset = Shuttles.change_shuttle(shuttle)
gtfs_disruptable_routes = Shuttles.list_disruptable_routes()
shapes = Shuttles.list_shapes()

render(conn, :edit,
shuttle: shuttle,
changeset: changeset,
gtfs_disruptable_routes: gtfs_disruptable_routes,
shapes: shapes
)
end

def update(conn, %{"id" => id, "shuttle" => shuttle_params}) do
shuttle = Shuttles.get_shuttle!(id)

case Shuttles.update_shuttle(shuttle, shuttle_params) do
{:ok, shuttle} ->
conn
|> put_flash(:info, "Shuttle updated successfully.")
|> redirect(to: ~p"/shuttles/#{shuttle}")

{:error, %Ecto.Changeset{} = changeset} ->
gtfs_disruptable_routes = Shuttles.list_disruptable_routes()
shapes = Shuttles.list_shapes()

render(conn, :edit,
shuttle: shuttle,
changeset: changeset,
gtfs_disruptable_routes: gtfs_disruptable_routes,
shapes: shapes
)
end
end
end
10 changes: 0 additions & 10 deletions lib/arrow_web/controllers/shuttle_html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,4 @@ defmodule ArrowWeb.ShuttleView do
use ArrowWeb, :html

embed_templates "shuttle_html/*"

@doc """
Renders a shuttle form.
"""
attr :changeset, Ecto.Changeset, required: true
attr :action, :string, required: true
attr :gtfs_disruptable_routes, :list, required: true
attr :shapes, :list, required: true

def shuttle_form(assigns)
end
12 changes: 0 additions & 12 deletions lib/arrow_web/controllers/shuttle_html/edit.html.heex

This file was deleted.

80 changes: 0 additions & 80 deletions lib/arrow_web/controllers/shuttle_html/shuttle_form.html.heex

This file was deleted.

180 changes: 180 additions & 0 deletions lib/arrow_web/live/shuttle_live/shuttle_live.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
defmodule ArrowWeb.ShuttleViewLive do
use ArrowWeb, :live_view
import Phoenix.HTML.Form
alias Arrow.Shuttles
alias Arrow.Shuttles.Shuttle

embed_templates "shuttle_live/*"

@doc """
Renders a shuttle route form
"""
attr :form, :any, required: true
attr :action, :string, required: true
attr :http_action, :string
attr :gtfs_disruptable_routes, :list, required: true
attr :shapes, :list, required: true

def shuttle_form(assigns) do
~H"""
<.simple_form :let={f} for={@form} as={:shuttle_route} action={@http_action} phx-submit={@action}>
<div class="form-group">
<a
target="_blank"
rel="noreferrer noopener"
href="https://www.notion.so/native/mbta-downtown-crossing/Conventions-for-shuttle-bus-information-fc5a788409b24eb088dbfe3a43abf67e?pvs=4&deepLinkOpenNewTab=true#2b8886089b25403991f1ed69144d8fd8"
>
View Shuttle Definition Conventions
</a>
</div>
<.error :if={@form.action}>
Oops, something went wrong! Please check the errors below.
</.error>
<div class="row">
<div class="col">
<.input field={f[:shuttle_name]} type="text" label="Shuttle Name" />
</div>
<div class="col">
<.input
field={f[:disrupted_route_id]}
type="select"
label="Disrupted Route"
prompt="Choose a route"
options={Enum.map(@gtfs_disruptable_routes, &{&1.long_name, &1.id})}
/>
</div>
<div class="col">
<.input
field={f[:status]}
type="select"
label="Status"
prompt="Choose a value"
options={Ecto.Enum.values(Arrow.Shuttles.Shuttle, :status)}
/>
</div>
</div>
<hr />
<h2>define route</h2>
<.inputs_for :let={f_route} field={f[:routes]} as={:routes}>
<h4>direction <%= input_value(f_route, :direction_id) %></h4>
<div class="row">
<div class="hidden">
<.input field={f_route[:direction_id]} type="text" label="Direction id" />
</div>
<div class="col">
<.input field={f_route[:direction_desc]} type="text" label="Direction desc" />
</div>
<div class="col offset-md-1">
<.input
field={f_route[:shape_id]}
type="select"
label="Shape"
prompt="Choose a shape"
options={Enum.map(@shapes, &{&1.name, &1.id})}
/>
</div>
</div>
<div class="row">
<div class="col">
<.input field={f_route[:destination]} type="text" label="Destination" />
</div>
<div class="col-1 align-self-end italic">
<div class="form-group">
via
</div>
</div>
<div class="col">
<.input field={f_route[:waypoint]} type="text" label="Waypoint" />
</div>
</div>
<div class="row">
<div class="col">
<.input field={f_route[:suffix]} type="text" label="Suffix" />
</div>
</div>
</.inputs_for>
<:actions>
<.button>Save Shuttle</.button>
</:actions>
</.simple_form>
"""
end

def mount(%{"id" => id} = _params, session, socket) do
logout_url = session["logout_url"]
shuttle = Shuttles.get_shuttle!(id)
changeset = Shuttles.change_shuttle(shuttle)
gtfs_disruptable_routes = Shuttles.list_disruptable_routes()
shapes = Shuttles.list_shapes()
form = to_form(changeset)

socket =
socket
|> assign(:form, form)
|> assign(:form_action, "edit")
|> assign(:http_action, ~p"/shuttles/#{id}")
|> assign(:shuttle_route, shuttle)
|> assign(:title, "edit shuttle")
|> assign(:gtfs_disruptable_routes, gtfs_disruptable_routes)
|> assign(:shapes, shapes)
|> assign(:logout_url, logout_url)

{:ok, socket}
end

def mount(%{} = _params, session, socket) do
logout_url = session["logout_url"]

changeset =
Shuttles.change_shuttle(%Shuttle{
status: :draft,
routes: [%Shuttles.Route{direction_id: :"0"}, %Shuttles.Route{direction_id: :"1"}]
})

gtfs_disruptable_routes = Shuttles.list_disruptable_routes()
shapes = Shuttles.list_shapes()
form = to_form(changeset)

socket =
socket
|> assign(:form, form)
|> assign(:form_action, "create")
|> assign(:http_action, ~p"/shuttles")
|> assign(:title, "create new replacement service shuttle")
|> assign(:gtfs_disruptable_routes, gtfs_disruptable_routes)
|> assign(:shapes, shapes)
|> assign(:logout_url, logout_url)

{: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)

{:noreply, assign(socket, form: form, shuttle_route: shuttle_route_params)}
end

def handle_event("edit", %{"shuttle_route" => shuttle_route_params}, socket) do
shuttle = Shuttles.get_shuttle!(socket.assigns.shuttle_route.id)
changeset = Shuttles.change_shuttle(shuttle, shuttle_route_params)

handle_changeset(socket, changeset)
end

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

handle_changeset(socket, changeset)
end
end
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<.header>
new shuttle
<%= @title %>
</.header>

<.shuttle_form
changeset={@changeset}
action={~p"/shuttles"}
form={@form}
action={@form_action}
http_action={@http_action}
gtfs_disruptable_routes={@gtfs_disruptable_routes}
shapes={@shapes}
/>
Expand Down
Loading

0 comments on commit 73986a8

Please sign in to comment.