defmodule PrymnWeb.AppIndexLive do
use PrymnWeb, :live_view
alias Prymn.Apps
@impl true
def mount(_, _, socket) do
apps = Apps.list_apps()
for %Apps.App{} = app <- apps do
Phoenix.PubSub.subscribe(Prymn.PubSub, "app:#{app.id}")
end
{:ok, assign(socket, :apps, apps)}
end
@impl true
def render(assigns) do
~H"""
<%= cond do %>
<% assigns.live_action == :new -> %>
<.back navigate={~p"/apps"}>Go back
<.live_component id={:new} module={PrymnWeb.CreateApp} app_type={assigns[:app_type]} />
<% assigns.apps == [] -> %>
<.onboarding />
<% true -> %>
<.header>
Live Apps
<:subtitle>
All of your apps accross all projects.
<:actions>
Create app
App: <%= app.name %>
Server: <%= app.server.name %>
<% end %>
"""
end
@impl true
def handle_info(msg, socket) do
dbg(msg, label: "Incoming message from pubsub")
{:noreply, socket}
end
@impl true
def handle_params(%{"app_type" => app_type}, _, socket) do
{:noreply, assign(socket, app_type: app_type)}
end
def handle_params(_, _, socket) do
{:noreply,
socket
|> assign(:page_title, (socket.assigns.live_action == :new && "New App") || "Apps")}
end
defp onboarding(assigns) do
~H"""
You have no Apps.
Create your first App here!
Create a new App
"""
end
end