ezcontainer_example/lib/ezcontainer_railway_web/live/container_create_live.ex

66 lines
2.3 KiB
Elixir
Raw 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">
<article>
<%= if @target == "container" do %>
<.form for={@form} phx-submit="create_container">
<label class="label">
<span class="label-text">Container Name</span>
<span class="label-text-alt mr-20">Tag</span>
</label>
2023-10-25 04:48:36 -04:00
2023-10-25 23:19:22 -04:00
<div class="grid grid-rows-1 grid-cols-4 gap-4">
<input class="input input-bordered inline col-span-3" name="name" />
<input class="input input-bordered inline" name="tag" />
</div>
<div class="text-center mt-6">
<button phx-submit class="btn btn-primary">CREATE</button>
</div>
<input type="hidden" name="target" value="container" />
</.form>
<% end %>
<%= if @target == "repo" do %>
<.form for={@form} phx-submit="create_container">
<label class="label">
<span class="label-text">Github Repo</span>
<span class="label-text-alt mr-20">Branch</span>
</label>
<div class="grid grid-rows-1 grid-cols-4 gap-4">
<input class="input input-bordered inline col-span-3" name="name" />
<input class="input input-bordered inline" name="tag" />
</div>
<div class="text-center mt-6">
<button phx-submit class="btn btn-primary">CREATE</button>
</div>
<input type="hidden" name="target" value="repo" />
</.form>
<% end %>
</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-25 23:19:22 -04:00
{:ok, socket |> assign(form: form, target: "container", session: session)}
end
def handle_event("create_container", data, socket) do
{:ok, resp} = Railway.create_service(
data["name"],
"image",
socket.assigns.session["railway_token"]
)
{:noreply, socket |> push_redirect('/containers')}
2023-10-25 04:48:36 -04:00
end
end