2022-07-25 20:12:11 -04:00
|
|
|
defmodule MemexWeb.PipelineLive.Show do
|
|
|
|
use MemexWeb, :live_view
|
2022-11-26 21:26:21 -05:00
|
|
|
alias Memex.{Accounts.User, Pipelines}
|
|
|
|
alias Memex.Pipelines.{Pipeline, Steps, Steps.Step}
|
2022-07-25 20:12:11 -04:00
|
|
|
|
|
|
|
@impl true
|
|
|
|
def mount(_params, _session, socket) do
|
|
|
|
{:ok, socket}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2022-11-24 14:31:16 -05:00
|
|
|
def handle_params(
|
2022-11-26 21:26:21 -05:00
|
|
|
%{"slug" => slug} = params,
|
|
|
|
_url,
|
|
|
|
%{assigns: %{current_user: current_user, live_action: live_action}} = socket
|
2022-11-24 14:31:16 -05:00
|
|
|
) do
|
2022-11-26 14:51:18 -05:00
|
|
|
pipeline =
|
|
|
|
case Pipelines.get_pipeline_by_slug(slug, current_user) do
|
|
|
|
nil -> raise MemexWeb.NotFoundError, gettext("%{slug} could not be found", slug: slug)
|
|
|
|
pipeline -> pipeline
|
|
|
|
end
|
2022-11-24 17:44:01 -05:00
|
|
|
|
|
|
|
socket =
|
|
|
|
socket
|
|
|
|
|> assign(:page_title, page_title(live_action, pipeline))
|
|
|
|
|> assign(:pipeline, pipeline)
|
2022-11-26 21:26:21 -05:00
|
|
|
|> assign(:steps, pipeline |> Steps.list_steps(current_user))
|
|
|
|
|> apply_action(live_action, params)
|
2022-11-24 17:44:01 -05:00
|
|
|
|
|
|
|
{:noreply, socket}
|
2022-07-25 20:12:11 -04:00
|
|
|
end
|
|
|
|
|
2022-11-26 21:26:21 -05:00
|
|
|
defp apply_action(socket, live_action, _params) when live_action in [:show, :edit] do
|
|
|
|
socket
|
|
|
|
end
|
|
|
|
|
|
|
|
defp apply_action(
|
|
|
|
%{
|
|
|
|
assigns: %{
|
|
|
|
steps: steps,
|
|
|
|
pipeline: %{id: pipeline_id},
|
|
|
|
current_user: %{id: current_user_id}
|
|
|
|
}
|
|
|
|
} = socket,
|
|
|
|
:add_step,
|
|
|
|
_params
|
|
|
|
) do
|
|
|
|
socket
|
|
|
|
|> assign(
|
|
|
|
step: %Step{
|
|
|
|
position: steps |> Enum.count(),
|
|
|
|
pipeline_id: pipeline_id,
|
|
|
|
user_id: current_user_id
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp apply_action(
|
|
|
|
%{assigns: %{current_user: current_user}} = socket,
|
|
|
|
:edit_step,
|
|
|
|
%{"step_id" => step_id}
|
|
|
|
) do
|
|
|
|
socket |> assign(step: step_id |> Steps.get_step!(current_user))
|
|
|
|
end
|
|
|
|
|
2022-11-24 14:31:16 -05:00
|
|
|
@impl true
|
|
|
|
def handle_event(
|
|
|
|
"delete",
|
|
|
|
_params,
|
|
|
|
%{assigns: %{pipeline: pipeline, current_user: current_user}} = socket
|
|
|
|
) do
|
2022-11-26 14:51:18 -05:00
|
|
|
{:ok, %{slug: slug}} = Pipelines.delete_pipeline(pipeline, current_user)
|
2022-11-24 14:31:16 -05:00
|
|
|
|
|
|
|
socket =
|
|
|
|
socket
|
2022-11-26 14:51:18 -05:00
|
|
|
|> put_flash(:info, gettext("%{slug} deleted", slug: slug))
|
2022-11-24 14:31:16 -05:00
|
|
|
|> push_navigate(to: Routes.pipeline_index_path(Endpoint, :index))
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
2022-11-26 21:26:21 -05:00
|
|
|
def handle_event(
|
|
|
|
"delete_step",
|
|
|
|
%{"step-id" => step_id},
|
|
|
|
%{assigns: %{pipeline: %{slug: pipeline_slug}, current_user: current_user}} = socket
|
|
|
|
) do
|
|
|
|
{:ok, %{title: title}} =
|
|
|
|
step_id
|
|
|
|
|> Steps.get_step!(current_user)
|
|
|
|
|> Steps.delete_step(current_user)
|
|
|
|
|
|
|
|
socket =
|
|
|
|
socket
|
|
|
|
|> put_flash(:info, gettext("%{title} deleted", title: title))
|
|
|
|
|> push_patch(to: Routes.pipeline_show_path(Endpoint, :show, pipeline_slug))
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event(
|
|
|
|
"reorder_step",
|
|
|
|
%{"step-id" => step_id, "direction" => direction},
|
|
|
|
%{assigns: %{pipeline: %{slug: pipeline_slug}, current_user: current_user}} = socket
|
|
|
|
) do
|
|
|
|
direction = if direction == "up", do: :up, else: :down
|
|
|
|
|
|
|
|
{:ok, _step} =
|
|
|
|
step_id
|
|
|
|
|> Steps.get_step!(current_user)
|
|
|
|
|> Steps.reorder_step(direction, current_user)
|
|
|
|
|
|
|
|
socket =
|
|
|
|
socket
|
|
|
|
|> push_patch(to: Routes.pipeline_show_path(Endpoint, :show, pipeline_slug))
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
2022-11-26 14:51:18 -05:00
|
|
|
defp page_title(:show, %{slug: slug}), do: slug
|
2022-11-26 21:26:21 -05:00
|
|
|
|
|
|
|
defp page_title(live_action, %{slug: slug}) when live_action in [:edit, :edit_step],
|
|
|
|
do: gettext("edit %{slug}", slug: slug)
|
|
|
|
|
|
|
|
defp page_title(:add_step, %{slug: slug}), do: gettext("add step to %{slug}", slug: slug)
|
2022-11-24 17:15:10 -05:00
|
|
|
|
|
|
|
@spec is_owner_or_admin?(Pipeline.t(), User.t()) :: boolean()
|
|
|
|
defp is_owner_or_admin?(%{user_id: user_id}, %{id: user_id}), do: true
|
|
|
|
defp is_owner_or_admin?(_context, %{role: :admin}), do: true
|
|
|
|
defp is_owner_or_admin?(_context, _other_user), do: false
|
|
|
|
|
|
|
|
@spec is_owner?(Pipeline.t(), User.t()) :: boolean()
|
|
|
|
defp is_owner?(%{user_id: user_id}, %{id: user_id}), do: true
|
|
|
|
defp is_owner?(_context, _other_user), do: false
|
2022-07-25 20:12:11 -04:00
|
|
|
end
|