ezcontainer_example/lib/ezcontainer_railway_web/live/container_create_live.ex

84 lines
3.3 KiB
Elixir
Raw Permalink Normal View History

2023-10-25 04:48:36 -04:00
defmodule EzcontainerRailwayWeb.ContainerCreateLive do
2023-10-25 23:19:22 -04:00
alias EzcontainerRailway.Railway
2023-10-25 04:48:36 -04:00
alias Phoenix.Component
use Phoenix.LiveView
2023-10-25 23:19:22 -04:00
use EzcontainerRailwayWeb, :live_view
2023-10-25 04:48:36 -04:00
def render(assigns) do
~H"""
2023-10-25 23:19:22 -04:00
<div class="flex space-around w-full">
2023-10-27 01:48:53 -04:00
<article class="m-auto">
<%= if @target == "image" do %>
2023-10-25 23:19:22 -04:00
<.form for={@form} phx-submit="create_container">
<div class="grid grid-rows-1 grid-cols-4 gap-4">
2023-10-27 01:48:53 -04:00
<label class="label flex space-between">
<span class="label-text">Container Name</span>
</label>
2023-10-25 23:19:22 -04:00
<input class="input input-bordered inline col-span-3" name="name" />
2023-10-27 01:48:53 -04:00
<label class="label flex space-between">
<span class="label-text-alt">Tag</span>
</label>
2023-10-25 23:19:22 -04:00
<input class="input input-bordered inline" name="tag" />
</div>
<div class="text-center mt-6">
2023-10-27 01:48:53 -04:00
<button phx-submit class={"btn btn-primary #{if @loading, do: "loading-dots"}"}>CREATE</button>
2023-10-25 23:19:22 -04:00
</div>
<input type="hidden" name="target" value="container" />
</.form>
<% end %>
<%= if @target == "repo" do %>
<.form for={@form} phx-submit="create_container">
<div class="grid grid-rows-1 grid-cols-4 gap-4">
2023-10-27 01:48:53 -04:00
<label class="label flex space-between">
<span class="label-text">Repo</span>
</label>
2023-10-25 23:19:22 -04:00
<input class="input input-bordered inline col-span-3" name="name" />
2023-10-27 01:48:53 -04:00
<label class="label flex space-between">
<span class="label-text-alt">Branch</span>
</label>
2023-10-25 23:19:22 -04:00
<input class="input input-bordered inline" name="tag" />
</div>
<div class="text-center mt-6">
2023-10-27 01:48:53 -04:00
<button phx-submit class={"btn btn-primary #{if @loading, do: "loading-spinner"}"}>CREATE</button>
2023-10-25 23:19:22 -04:00
</div>
<input type="hidden" name="target" value="repo" />
</.form>
<% end %>
2023-10-27 01:48:53 -04:00
<div class="flex w-full mt-5 space-around">
<button class="m-auto btn btn-secondary" phx-click="switch_target">Switch to <%= if @target == "repo", do: "Image", else: "Repo" %></button>
</div>
2023-10-25 23:19:22 -04:00
</article>
</div>
2023-10-25 04:48:36 -04:00
"""
end
2023-10-25 23:19:22 -04:00
def mount(_params, session, socket) do
form = Component.to_form(%{"name" => "", "tag" => ""})
2023-10-25 04:48:36 -04:00
2023-10-27 01:48:53 -04:00
{:ok, socket |> assign(form: form, target: "image", session: session, loading: false)}
2023-10-25 23:19:22 -04:00
end
2023-10-27 01:48:53 -04:00
def handle_info({:create, data}, socket) do
2023-10-25 23:19:22 -04:00
{:ok, resp} = Railway.create_service(
data["name"],
"image",
socket.assigns.session["railway_token"]
)
2023-10-26 00:24:51 -04:00
{:noreply, socket |> put_flash(:info, "container deplying") |> push_redirect(to: "/containers")}
2023-10-25 04:48:36 -04:00
end
2023-10-27 01:48:53 -04:00
def handle_event("create_container", data, socket) do
send(self(), {:create, data})
{:noreply, socket |> assign(loading: true)}
end
def handle_event("switch_target", _, socket) do
IO.inspect socket.assigns
case socket.assigns.target do
"repo" -> {:noreply, socket |> assign(target: "image")}
"image" -> {:noreply, socket |> assign(target: "repo")}
end
end
2023-10-25 04:48:36 -04:00
end