-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: migrate shuttle page to LiveView
- Loading branch information
Showing
7 changed files
with
188 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
80 changes: 0 additions & 80 deletions
80
lib/arrow_web/controllers/shuttle_html/shuttle_form.html.heex
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
7 changes: 4 additions & 3 deletions
7
...eb/controllers/shuttle_html/new.html.heex → .../shuttle_live/shuttle_view_live.html.heex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.