My space on the web https://inhji.de
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

55 lines
1.5 KiB

defmodule MirageWeb.LinkLive.FormComponent do
use MirageWeb, :live_component
alias Mirage.Links
@impl true
def update(%{link: link} = assigns, socket) do
changeset = Links.change_link(link)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)}
end
@impl true
def handle_event("validate", %{"link" => link_params}, socket) do
changeset =
socket.assigns.link
|> Links.change_link(link_params)
|> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)}
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} ->
{:noreply,
socket
|> put_flash(:info, "Note link updated successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end
defp save_link(socket, :new, link_params) do
case Links.create_link(link_params) do
{:ok, _link} ->
{:noreply,
socket
|> put_flash(:info, "Note link created successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
end
end