cannery/lib/cannery_web/live/live_helpers.ex

39 lines
1.0 KiB
Elixir
Raw Normal View History

2021-09-02 23:31:14 -04:00
defmodule CanneryWeb.LiveHelpers do
2022-01-22 21:40:29 -05:00
@moduledoc """
Contains common helper functions for liveviews
"""
2021-09-02 23:31:14 -04:00
import Phoenix.LiveView.Helpers
import Phoenix.LiveView, only: [assign_new: 3]
2022-01-22 21:40:29 -05:00
alias Cannery.Accounts
2022-02-11 22:47:33 -05:00
alias CanneryWeb.Components.Modal
2021-09-02 23:31:14 -04:00
@doc """
2022-02-11 22:47:33 -05:00
Renders a component inside the `Modal` component.
2021-09-02 23:31:14 -04:00
The rendered modal receives a `:return_to` option to properly update
the URL when the modal is closed.
## Examples
<%= live_modal CanneryWeb.TagLive.FormComponent,
id: @tag.id || :new,
action: @live_action,
tag: @tag,
return_to: Routes.tag_index_path(@socket, :index) %>
"""
def live_modal(component, opts) do
path = Keyword.fetch!(opts, :return_to)
2022-02-11 22:47:33 -05:00
live_component(Modal, id: :modal, return_to: path, component: component, opts: opts)
2021-09-02 23:31:14 -04:00
end
def assign_defaults(socket, %{"user_token" => user_token} = _session) do
socket
2022-02-09 23:21:42 -05:00
|> assign_new(:current_user, fn -> Accounts.get_user_by_session_token(user_token) end)
2021-09-02 23:31:14 -04:00
end
2022-01-22 17:21:10 -05:00
2021-09-02 23:31:14 -04:00
def assign_defaults(socket, _session) do
socket
end
end