add steps
This commit is contained in:
74
lib/memex_web/live/step_live/form_component.ex
Normal file
74
lib/memex_web/live/step_live/form_component.ex
Normal file
@ -0,0 +1,74 @@
|
||||
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
|
34
lib/memex_web/live/step_live/form_component.html.heex
Normal file
34
lib/memex_web/live/step_live/form_component.html.heex
Normal file
@ -0,0 +1,34 @@
|
||||
<div class="h-full flex flex-col justify-start items-stretch space-y-4">
|
||||
<.form
|
||||
:let={f}
|
||||
for={@changeset}
|
||||
id="step-form"
|
||||
phx-target={@myself}
|
||||
phx-change="validate"
|
||||
phx-submit="save"
|
||||
phx-debounce="300"
|
||||
class="flex flex-col justify-start items-stretch space-y-4"
|
||||
>
|
||||
<%= text_input(f, :title,
|
||||
class: "input input-primary",
|
||||
placeholder: gettext("title")
|
||||
) %>
|
||||
<%= error_tag(f, :title) %>
|
||||
|
||||
<%= textarea(f, :content,
|
||||
id: "step-form-content",
|
||||
class: "input input-primary h-64 min-h-64",
|
||||
phx_hook: "MaintainAttrs",
|
||||
phx_update: "ignore",
|
||||
placeholder: gettext("use [[context-slug]] to link to a context")
|
||||
) %>
|
||||
<%= error_tag(f, :content) %>
|
||||
|
||||
<div class="flex justify-center items-stretch space-x-4">
|
||||
<%= submit(dgettext("actions", "save"),
|
||||
phx_disable_with: gettext("saving..."),
|
||||
class: "mx-auto btn btn-primary"
|
||||
) %>
|
||||
</div>
|
||||
</.form>
|
||||
</div>
|
Reference in New Issue
Block a user