infinitered / thesis-phoenix

A lightweight, bolt-on, intuitive content editing system for Elixir/Phoenix websites. Star this repo and follow along with our progress!
Other
645 stars 62 forks source link

LiveView support? #172

Open mayel opened 3 years ago

mayel commented 3 years ago

Wondering if there are any plans to support using Thesis in LiveViews and LiveComponents? Thanks :)

mavuio commented 3 years ago

i'm using it in a LiveView-Context in my project, but you have to go with a controller to inject the necessary data to the live-view-session, a bit hacky, but it works:

in the controller:

defmodule ThermostatController do
  ...
  import Phoenix.LiveView.Controller

  def show(conn, %{"id" => thermostat_id}) do
   for_thesis=
          %{ assigns:
              Map.take(
                conn.assigns,
                ~w(thesis_content thesis_dynamic_page thesis_editable thesis_page)a
              )
              |> Map.put(:thesis_editable, MyProject.ThesisAuth.page_is_editable?(conn))
          }
          |> Map.merge(Map.take(conn, ~w(request_path)a))

    live_render(conn, ThermostatLive, session: %{
      "thermostat_id" => id,
      "current_user_id" => get_session(conn, :user_id),
      "for_thesis" => for_thesis,
    })
  end
end

in the live-view-mount:

  def mount(_params, session, socket) do
   ...
   {:ok, assign( socket, :thesis_conn, session["for_thesis"] )}
  end

in the .leex template:

<div phx-update="ignore">
  <%= Thesis.View.thesis_editor(@thesis_conn) %>
</div>

<%= Thesis.View.content(@thesis_conn, "home-title", :text) do %>
      My Text
<% end %>
mayel commented 3 years ago

Thanks @werkzeugh ! I got something similar to work, but indeed would be better to get proper integration specifically for LV.