PhoenixDatastar.Helpers (PhoenixDatastar v0.2.0)

Copy Markdown View Source

Shared helper functions for PhoenixDatastar modules.

Summary

Functions

Extracts the base path from a request path by stripping Datastar suffixes.

Extracts the session map from a Plug.Conn.

Extracts the view module name (last part of module path).

Renders a view's template with the socket's assigns.

Functions

get_base_path(path)

@spec get_base_path(String.t()) :: String.t()

Extracts the base path from a request path by stripping Datastar suffixes.

Removes /stream suffix and /_event/:event patterns to get the view's base path.

Examples

iex> PhoenixDatastar.Helpers.get_base_path("/counter/stream")
"/counter"

iex> PhoenixDatastar.Helpers.get_base_path("/counter/_event/increment")
"/counter"

iex> PhoenixDatastar.Helpers.get_base_path("/counter")
"/counter"

get_session_map(conn)

@spec get_session_map(Plug.Conn.t()) :: map()

Extracts the session map from a Plug.Conn.

Merges the Plug session (from the session store) with all conn.assigns set by plugs in the pipeline. Assign keys are stringified so they can be pattern-matched in mount/3 the same way as regular session keys.

Returns the session data if available, otherwise an empty map.

get_view_name(view)

@spec get_view_name(module()) :: String.t()

Extracts the view module name (last part of module path).

Examples

iex> PhoenixDatastar.Helpers.get_view_name(MyApp.CounterStar)
"CounterStar"

render_html(view, socket)

@spec render_html(module(), PhoenixDatastar.Socket.t()) :: Phoenix.HTML.Safe.t()

Renders a view's template with the socket's assigns.