Current section
Files
Jump to
Current section
Files
priv/templates/kirayedar.gen.live/form_component.ex.eex
defmodule <%= @app_module %>Web.<%= @module %>Live.FormComponent do
use <%= @app_module %>Web, :live_component
alias <%= @app_module %>.<%= @module %>
alias <%= @app_module %>.Repo
@impl true
def render(assigns) do
~H"""
<div>
<.header>
{@title}
<:subtitle>Use this form to manage <%= @singular %> records in your database.</:subtitle>
</.header>
<.simple_form
for={@form}
id="<%= @singular %>-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save"
>
<.input field={@form[:name]} type="text" label="Name" />
<.input field={@form[:slug]} type="text" label="Slug" />
<.input field={@form[:domain]} type="text" label="Domain" />
<.input
field={@form[:status]}
type="select"
label="Status"
options={["active", "suspended", "pending"]}
/>
<:actions>
<.button phx-disable-with="Saving...">Save <%= @module %></.button>
</:actions>
</.simple_form>
</div>
"""
end
@impl true
def update(%{<%= @singular %>: <%= @singular %>} = assigns, socket) do
changeset = <%= @module %>.changeset(<%= @singular %>, %{})
{:ok,
socket
|> assign(assigns)
|> assign_form(changeset)}
end
@impl true
def handle_event("validate", %{"<%= @singular %>" => <%= @singular %>_params}, socket) do
changeset =
socket.assigns.<%= @singular %>
|> <%= @module %>.changeset(<%= @singular %>_params)
|> Map.put(:action, :validate)
{:noreply, assign_form(socket, changeset)}
end
def handle_event("save", %{"<%= @singular %>" => <%= @singular %>_params}, socket) do
save_<%= @singular %>(socket, socket.assigns.action, <%= @singular %>_params)
end
defp save_<%= @singular %>(socket, :edit, <%= @singular %>_params) do
case Repo.update(<%= @module %>.changeset(socket.assigns.<%= @singular %>, <%= @singular %>_params)) do
{:ok, <%= @singular %>} ->
notify_parent({:saved, <%= @singular %>})
{:noreply,
socket
|> put_flash(:info, "<%= @module %> updated successfully")
|> push_patch(to: socket.assigns.patch)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign_form(socket, changeset)}
end
end
defp save_<%= @singular %>(socket, :new, <%= @singular %>_params) do
Kirayedar.scope_global(fn ->
case Repo.insert(<%= @module %>.changeset(%<%= @module %>{}, <%= @singular %>_params)) do
{:ok, <%= @singular %>} ->
# Create tenant schema
case Kirayedar.create(Repo, <%= @singular %>.slug) do
:ok ->
# Run migrations for new tenant
Kirayedar.Migration.migrate(Repo, <%= @singular %>.slug)
notify_parent({:saved, <%= @singular %>})
{:noreply,
socket
|> put_flash(:info, "<%= @module %> created successfully")
|> push_patch(to: socket.assigns.patch)}
{:error, _} ->
Repo.delete(<%= @singular %>)
{:noreply, put_flash(socket, :error, "Failed to create <%= @singular %> schema")}
end
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign_form(socket, changeset)}
end
end)
end
defp assign_form(socket, %Ecto.Changeset{} = changeset) do
assign(socket, :form, to_form(changeset))
end
defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
end