-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
53 additions
and
34 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
defmodule LiveIsolatedComponent.ViewUtils do | ||
@moduledoc """ | ||
Collection of utils for people that want to write their own | ||
mock LiveView to use with `m:LiveIsolatedComponent.live_isolated_component/2`. | ||
""" | ||
|
||
alias LiveIsolatedComponent.Hooks | ||
alias LiveIsolatedComponent.MessageNames | ||
alias LiveIsolatedComponent.StoreAgent | ||
alias LiveIsolatedComponent.Utils | ||
|
||
@doc """ | ||
Run this in your mock view `c:Phoenix.LiveView.mount/3`. | ||
## Options | ||
- `:on_mmount`, _boolean_, defaults to `true`. Can disable adding `on_mount` hooks. | ||
""" | ||
def mount(params, session, socket, opts \\ []) do | ||
socket = | ||
socket | ||
|> assign(:store_agent, session[MessageNames.store_agent_key()]) | ||
Check failure on line 21 in lib/live_isolated_component/view_utils.ex GitHub Actions / Build and test (1.14.5, 24.3, 0.19.0)
Check failure on line 21 in lib/live_isolated_component/view_utils.ex GitHub Actions / Build and test (1.14.5, 24.3, 0.20.0)
|
||
|> run_on_mount(params, session, opts) | ||
|> Utils.update_socket_from_store_agent() | ||
|
||
{:ok, socket} | ||
end | ||
|
||
defp run_on_mount(socket, params, session, opts), | ||
do: run_on_mount(socket.assigns.store_agent, params, session, socket, opts) | ||
|
||
defp run_on_mount(agent, params, session, socket, opts) do | ||
on_mount = if Keyword.get(opts, :on_mount, true), do: StoreAgent.get_on_mount(agent), else: [] | ||
|
||
on_mount | ||
|> add_lic_hooks() | ||
|> Enum.reduce(socket, &do_run_on_mount(&1, params, session, &2)) | ||
end | ||
|
||
defp do_run_on_mount({module, first}, params, session, socket) do | ||
{:cont, socket} = module.on_mount(first, params, session, socket) | ||
socket | ||
end | ||
|
||
defp do_run_on_mount(module, params, session, socket), | ||
do: do_run_on_mount({module, :default}, params, session, socket) | ||
|
||
defp add_lic_hooks(list), | ||
do: [Hooks.HandleEventSpyHook, Hooks.HandleInfoSpyHook, Hooks.AssignsUpdateSpyHook | list] | ||
end |