2021-11-05 00:49:19 +00:00
|
|
|
defmodule LiveBeatsWeb.LayoutComponent do
|
|
|
|
@moduledoc """
|
|
|
|
Component for rendering content inside layout without full DOM patch.
|
|
|
|
"""
|
|
|
|
use LiveBeatsWeb, :live_component
|
|
|
|
|
|
|
|
def show_modal(module, attrs) do
|
|
|
|
send_update(__MODULE__, id: "layout", show: Enum.into(attrs, %{module: module}))
|
|
|
|
end
|
|
|
|
|
|
|
|
def hide_modal do
|
|
|
|
send_update(__MODULE__, id: "layout", show: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(%{id: id} = assigns, socket) do
|
|
|
|
show =
|
|
|
|
case assigns[:show] do
|
|
|
|
%{module: _module, confirm: {text, attrs}} = show ->
|
|
|
|
show
|
2022-02-04 16:41:05 +00:00
|
|
|
|> Map.put_new(:title, show[:title])
|
|
|
|
|> Map.put_new(:on_cancel, show[:on_cancel] || %JS{})
|
|
|
|
|> Map.put_new(:on_confirm, show[:on_confirm] || %JS{})
|
2021-11-23 14:34:34 +00:00
|
|
|
|> Map.put_new(:patch, nil)
|
|
|
|
|> Map.put_new(:navigate, nil)
|
2021-11-05 00:49:19 +00:00
|
|
|
|> Map.merge(%{confirm_text: text, confirm_attrs: attrs})
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
{:ok, assign(socket, id: id, show: show)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def render(assigns) do
|
|
|
|
~H"""
|
|
|
|
<div class={unless @show, do: "hidden"}>
|
|
|
|
<%= if @show do %>
|
2022-02-04 16:41:05 +00:00
|
|
|
<.modal
|
|
|
|
show
|
|
|
|
id={@id}
|
|
|
|
navigate={@show.navigate}
|
|
|
|
patch={@show.patch}
|
|
|
|
on_cancel={@show.on_cancel}
|
|
|
|
on_confirm={@show.on_confirm}
|
|
|
|
>
|
|
|
|
<:title><%= @show.title %></:title>
|
2021-11-05 00:49:19 +00:00
|
|
|
<.live_component module={@show.module} {@show} />
|
|
|
|
<:cancel>Cancel</:cancel>
|
|
|
|
<:confirm {@show.confirm_attrs}><%= @show.confirm_text %></:confirm>
|
|
|
|
</.modal>
|
|
|
|
<% end %>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
end
|