Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/dotcom_web/hooks/assign_route.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule DotcomWeb.Hooks.AssignRoute do
@moduledoc """
Assign the route, before both disconnected and connected mounts.
"""
import Phoenix.Component, only: [assign: 3]

@routes_repo Application.compile_env!(:dotcom, :repo_modules)[:routes]

@doc """
Uses the route ID in the params to look up the corresponding
route, and assigns that route to the socket as `:route`.

If the route doesn't exist, then raises a
`DotcomWeb.NotFoundError` in order to get
`DotcomWeb.Router.handle_errors/2` to render the 404 page.
"""
def on_mount(:default, %{"route_id" => route_id}, _session, socket) do
case @routes_repo.get(route_id) do
nil -> raise DotcomWeb.NotFoundError
route -> {:cont, assign(socket, :route, route)}
end
end
end
5 changes: 5 additions & 0 deletions lib/dotcom_web/hooks/breadcrumbs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule DotcomWeb.Hooks.Breadcrumbs do
@moduledoc """
Assign the breadcrumbs, before both disconnected and connected mounts.
"""
alias DotcomWeb.Schedule.RouteBreadcrumbs

use Dotcom.Gettext.Sigils
use DotcomWeb, :verified_routes
Expand All @@ -17,6 +18,10 @@ defmodule DotcomWeb.Hooks.Breadcrumbs do
{:cont, assign(socket, :breadcrumbs, [build(~t"Trip Planner")])}
end

def on_mount(:schedule_page, _params, _session, %{assigns: %{route: route}} = socket) do
{:cont, assign(socket, :breadcrumbs, RouteBreadcrumbs.breadcrumbs(route))}
end

# catch-all case
def on_mount(:default, _params, _session, socket), do: {:cont, socket}
end
8 changes: 5 additions & 3 deletions lib/dotcom_web/live/line_diagram_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ defmodule DotcomWeb.LineDiagramLive do

use DotcomWeb, :live_view
@route_patterns_repo Application.compile_env!(:dotcom, :repo_modules)[:route_patterns]
@routes_repo Application.compile_env!(:dotcom, :repo_modules)[:routes]
@alerts_repo Application.compile_env!(:dotcom, :repo_modules)[:alerts]
@date_time_module Application.compile_env!(:dotcom, :date_time_module)

Expand All @@ -22,9 +21,12 @@ defmodule DotcomWeb.LineDiagramLive do

import DotcomWeb.Views.Helpers.AlertHelpers, only: [alert_badge: 1]

on_mount DotcomWeb.Hooks.AssignRoute
on_mount {DotcomWeb.Hooks.Breadcrumbs, :schedule_page}

def mount(params, _session, socket) do
route_id = params |> Map.get("route_id")
route = @routes_repo.get(route_id)
route = socket.assigns.route
route_id = route.id
route_patterns = @route_patterns_repo.by_route_id(route_id)

direction_id =
Expand Down
Loading