defmodule MemexWeb.StepLive.FormComponent do use MemexWeb, :live_component alias Memex.Pipelines.Steps @impl true def update(%{step: step, current_user: current_user, pipeline: _pipeline} = assigns, socket) do changeset = Steps.change_step(step, current_user) {:ok, socket |> assign(assigns) |> assign(:changeset, changeset)} end @impl true def handle_event( "validate", %{"step" => step_params}, %{assigns: %{step: step, current_user: current_user}} = socket ) do changeset = step |> Steps.change_step(step_params, current_user) |> Map.put(:action, :validate) {:noreply, assign(socket, :changeset, changeset)} end def handle_event("save", %{"step" => step_params}, %{assigns: %{action: action}} = socket) do save_step(socket, action, step_params) end defp save_step( %{assigns: %{step: step, return_to: return_to, current_user: current_user}} = socket, :edit_step, step_params ) do case Steps.update_step(step, step_params, current_user) do {:ok, %{title: title}} -> {:noreply, socket |> put_flash(:info, gettext("%{title} saved", title: title)) |> push_navigate(to: return_to)} {:error, %Ecto.Changeset{} = changeset} -> {:noreply, assign(socket, :changeset, changeset)} end end defp save_step( %{ assigns: %{ step: %{position: position}, return_to: return_to, current_user: current_user, pipeline: pipeline } } = socket, :add_step, step_params ) do case Steps.create_step(step_params, position, pipeline, current_user) do {:ok, %{title: title}} -> {:noreply, socket |> put_flash(:info, gettext("%{title} created", title: title)) |> push_navigate(to: return_to)} {:error, %Ecto.Changeset{} = changeset} -> {:noreply, assign(socket, changeset: changeset)} end end end