Trying to get up to speed with Ash, and it’s really not that steep of a curve on the context and schema side of the equation. But the views and how to use ash-phoenix, is not that easy to understand and very little examples around.
I made a simple live view for a mock project im working on, a live view for a Participant in a planner like solution.
Currently I have troubles finding out how to adapt the form_component to be ash phoenix compatible.
I tried to search on google, this forum and the docs with no luck.
Maybe you y’all can help me
this is the form_componnet Im trying to convert:
defmodule AshActivityPlannerWeb.ParticipantLive.FormComponent do
use AshActivityPlannerWeb, :live_component
alias AshActivityPlanner.Planner.Participant
@impl true
def render(assigns) do
~H"""
<div>
<.header>
<%= @title %>
<:subtitle>Use this form to manage participant records in your database.</:subtitle>
</.header>
<.simple_form
for={@form}
id="participant-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save"
>
<.input field={@form[:name]} type="text" label="Name" />
<.input field={@form[:email]} type="text" label="Email" />
<.input field={@form[:phone]} type="text" label="Phone" />
<.input field={@form[:description]} type="text" label="Description" />
<:actions>
<.button phx-disable-with="Saving...">Save Participant</.button>
</:actions>
</.simple_form>
</div>
"""
end
@impl true
def update(%{participant: participant} = assigns, socket) do
changeset = Participant.change_participant(participant)
{:ok,
socket
|> assign(assigns)
|> assign_form(changeset)}
end
@impl true
def handle_event("validate", %{"participant" => participant_params}, socket) do
changeset =
socket.assigns.participant
|> Participant.change_participant(participant_params)
|> Map.put(:action, :validate)
{:noreply, assign_form(socket, changeset)}
end
def handle_event("save", %{"participant" => participant_params}, socket) do
save_participant(socket, socket.assigns.action, participant_params)
end
defp save_participant(socket, :edit, participant_params) do
case Participant.update_participant(socket.assigns.participant, participant_params) do
{:ok, participant} ->
notify_parent({:saved, participant})
{:noreply,
socket
|> put_flash(:info, "Participant updated successfully")
|> push_patch(to: socket.assigns.patch)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign_form(socket, changeset)}
end
end
defp save_participant(socket, :new, participant_params) do
case Participant.create_participant(participant_params) do
{:ok, participant} ->
notify_parent({:saved, participant})
{:noreply,
socket
|> put_flash(:info, "Participant created successfully")
|> push_patch(to: socket.assigns.patch)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign_form(socket, changeset)}
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
AshActivityPlanner.Planner.Participant
is a normal ash resource with the following code interface:
code_interface do
define_for AshActivityPlanner.Planner
define :create
define :read
define :by_id, get_by: [:id], action: :read
define :by_name, get_by: [:name], action: :read
define :by_email, get_by: [:email], action: :read
define :update
define :destroy
end