defmodule ZoeyscomputerWeb.LinkLive.FormComponent do use ZoeyscomputerWeb, :live_component alias Zoeyscomputer.Links @impl true def render(assigns) do ~H"""
<.header> <%= @title %> <:subtitle>Use this form to manage link records in your database. <.simple_form for={@form} id="link-form" phx-target={@myself} phx-change="validate" phx-submit="save" > <.input field={@form[:url]} type="text" label="Url" /> <:actions> <.button phx-disable-with="Saving...">Save Link
""" end @impl true def update(%{link: link} = assigns, socket) do {:ok, socket |> assign(assigns) |> assign_new(:form, fn -> to_form(Links.change_link(link)) end)} end @impl true def handle_event("validate", %{"link" => link_params}, socket) do changeset = Links.change_link(socket.assigns.link, link_params) {:noreply, assign(socket, form: to_form(changeset, action: :validate))} end def handle_event("save", %{"link" => link_params}, socket) do save_link(socket, socket.assigns.action, link_params) end defp save_link(socket, :edit, link_params) do case Links.update_link(socket.assigns.link, link_params) do {:ok, link} -> notify_parent({:saved, link}) {:noreply, socket |> put_flash(:info, "Link updated successfully") |> push_patch(to: socket.assigns.patch)} {:error, %Ecto.Changeset{} = changeset} -> {:noreply, assign(socket, form: to_form(changeset))} end end defp save_link(socket, :new, link_params) do case Links.create_link(link_params) do {:ok, link} -> notify_parent({:saved, link}) {:noreply, socket |> put_flash(:info, "Link created successfully") |> push_patch(to: socket.assigns.patch)} {:error, %Ecto.Changeset{} = changeset} -> {:noreply, assign(socket, form: to_form(changeset))} end end defp notify_parent(msg), do: send(self(), {__MODULE__, msg}) end