2022-07-25 20:12:11 -04:00
|
|
|
defmodule MemexWeb.PipelineLive.Index do
|
|
|
|
use MemexWeb, :live_view
|
2022-11-24 14:31:16 -05:00
|
|
|
alias Memex.{Pipelines, Pipelines.Pipeline}
|
2022-07-25 20:12:11 -04:00
|
|
|
|
|
|
|
@impl true
|
2022-11-24 14:31:16 -05:00
|
|
|
def mount(%{"search" => search}, _session, socket) do
|
|
|
|
{:ok, socket |> assign(search: search) |> display_pipelines()}
|
|
|
|
end
|
|
|
|
|
2022-07-25 20:12:11 -04:00
|
|
|
def mount(_params, _session, socket) do
|
2022-11-24 14:31:16 -05:00
|
|
|
{:ok, socket |> assign(search: nil) |> display_pipelines()}
|
2022-07-25 20:12:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2022-11-24 14:31:16 -05:00
|
|
|
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
|
|
|
|
{:noreply, apply_action(socket, live_action, params)}
|
2022-07-25 20:12:11 -04:00
|
|
|
end
|
|
|
|
|
2022-11-24 14:31:16 -05:00
|
|
|
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
|
|
|
|
%{title: title} = pipeline = Pipelines.get_pipeline!(id, current_user)
|
|
|
|
|
2022-07-25 20:12:11 -04:00
|
|
|
socket
|
2022-11-24 14:31:16 -05:00
|
|
|
|> assign(page_title: gettext("edit %{title}", title: title))
|
|
|
|
|> assign(pipeline: pipeline)
|
2022-07-25 20:12:11 -04:00
|
|
|
end
|
|
|
|
|
2022-11-24 14:31:16 -05:00
|
|
|
defp apply_action(%{assigns: %{current_user: %{id: current_user_id}}} = socket, :new, _params) do
|
2022-07-25 20:12:11 -04:00
|
|
|
socket
|
2022-11-24 14:31:16 -05:00
|
|
|
|> assign(page_title: gettext("new pipeline"))
|
|
|
|
|> assign(pipeline: %Pipeline{user_id: current_user_id})
|
2022-07-25 20:12:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
defp apply_action(socket, :index, _params) do
|
|
|
|
socket
|
2022-11-24 14:31:16 -05:00
|
|
|
|> assign(page_title: gettext("pipelines"))
|
|
|
|
|> assign(search: nil)
|
|
|
|
|> assign(pipeline: nil)
|
|
|
|
|> display_pipelines()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp apply_action(socket, :search, %{"search" => search}) do
|
|
|
|
socket
|
|
|
|
|> assign(page_title: gettext("pipelines"))
|
|
|
|
|> assign(search: search)
|
|
|
|
|> assign(pipeline: nil)
|
|
|
|
|> display_pipelines()
|
2022-07-25 20:12:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2022-11-24 14:31:16 -05:00
|
|
|
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
|
|
|
|
pipeline = Pipelines.get_pipeline!(id, current_user)
|
|
|
|
{:ok, %{title: title}} = Pipelines.delete_pipeline(pipeline, current_user)
|
|
|
|
|
|
|
|
socket =
|
|
|
|
socket
|
|
|
|
|> assign(pipelines: Pipelines.list_pipelines(current_user))
|
|
|
|
|> put_flash(:info, gettext("%{title} deleted", title: title))
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_event("search", %{"search" => %{"search_term" => ""}}, socket) do
|
|
|
|
{:noreply, socket |> push_patch(to: Routes.pipeline_index_path(Endpoint, :index))}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event("search", %{"search" => %{"search_term" => search_term}}, socket) do
|
|
|
|
{:noreply,
|
|
|
|
socket |> push_patch(to: Routes.pipeline_index_path(Endpoint, :search, search_term))}
|
|
|
|
end
|
2022-07-25 20:12:11 -04:00
|
|
|
|
2022-11-24 14:31:16 -05:00
|
|
|
defp display_pipelines(%{assigns: %{current_user: current_user, search: search}} = socket)
|
|
|
|
when not (current_user |> is_nil()) do
|
|
|
|
socket |> assign(pipelines: Pipelines.list_pipelines(search, current_user))
|
2022-07-25 20:12:11 -04:00
|
|
|
end
|
|
|
|
|
2022-11-24 14:31:16 -05:00
|
|
|
defp display_pipelines(%{assigns: %{search: search}} = socket) do
|
|
|
|
socket |> assign(pipelines: Pipelines.list_public_pipelines(search))
|
2022-07-25 20:12:11 -04:00
|
|
|
end
|
|
|
|
end
|