ezcontainer_example/lib/ezcontainer_railway_web/live/containers_live.ex

56 lines
1.9 KiB
Elixir
Raw Permalink Normal View History

2023-10-26 00:24:51 -04:00
defmodule EzcontainerRailwayWeb.ContainersLive do
use Phoenix.LiveView
alias EzcontainerRailway.Railway
use EzcontainerRailwayWeb, :live_view
def render(assigns) do
~H"""
<div class="grid grid-cols-4 gap-2 m-10">
2023-10-27 01:48:53 -04:00
<%= for service <- @services do %>
<div class="card bg-base-100 shadow-xl">
<div class="card-body">
<h2 class="card-title"><%= service["name"] %></h2>
<div class="card-actions justify-start">
<button phx-click="delete" phx-value-project_id={service["id"]} class={"btn btn-xs btn-secondary #{if @loading, do: "loading-dots"}"}><%= if not @loading do %> DELETE <% end %></button>
2023-10-26 00:24:51 -04:00
</div>
2023-10-27 01:48:53 -04:00
</div>
</div>
<% end %>
2023-10-26 00:24:51 -04:00
</div>
"""
end
def mount(_params, session, socket) do
{:ok, services} = Railway.get_services(session["railway_token"])
2023-10-27 01:48:53 -04:00
socket = socket |> assign(session: session, services: services, loading: false)
case length(services) do
0 -> {:ok, socket |> put_flash(:error, "No running containers") |> push_navigate(to: "/containers/create")}
_ -> {:ok, socket |> assign(session: session, services: services, loading: false)}
end
2023-10-26 00:24:51 -04:00
end
def handle_info(:refresh, socket) do
{:ok, services} = Railway.get_services(socket.assigns.session["railway_token"])
2023-10-27 01:48:53 -04:00
case length(services) do
0 -> {:noreply, socket |> put_flash(:error, "No running containers") |> push_navigate(to: "/containers/create")}
_ -> {:noreply, socket |> assign(services: services, loading: false)}
end
2023-10-26 00:24:51 -04:00
end
2023-10-27 01:48:53 -04:00
def handle_info({:delete, project_id}, socket) do
{:ok, _} = Railway.delete_container(project_id, socket.assigns.session["railway_token"])
2023-10-26 00:24:51 -04:00
send(self(), :refresh)
{:noreply, socket}
end
2023-10-27 01:48:53 -04:00
def handle_event("delete", data, socket) do
send(self(), {:delete, data["project_id"]})
{:noreply, socket |> assign(loading: true)}
end
2023-10-26 00:24:51 -04:00
end