memEx/lib/memex_web/live/context_live/index.ex

80 lines
2.6 KiB
Elixir
Raw Permalink Normal View History

2022-07-25 20:11:08 -04:00
defmodule MemexWeb.ContextLive.Index do
use MemexWeb, :live_view
alias Memex.{Contexts, Contexts.Context}
2022-07-25 20:11:08 -04:00
@impl true
2022-11-24 12:44:34 -05:00
def mount(%{"search" => search}, _session, socket) do
{:ok, socket |> assign(search: search) |> display_contexts()}
end
2022-07-25 20:11:08 -04:00
def mount(_params, _session, socket) do
2022-11-24 12:44:34 -05:00
{:ok, socket |> assign(search: nil) |> display_contexts()}
2022-07-25 20:11:08 -04:00
end
@impl true
2022-11-24 12:44:34 -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:11:08 -04:00
end
2022-11-26 14:51:18 -05:00
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"slug" => slug}) do
%{slug: slug} = context = Contexts.get_context_by_slug(slug, current_user)
2022-11-24 12:44:34 -05:00
2022-07-25 20:11:08 -04:00
socket
2022-11-26 14:51:18 -05:00
|> assign(page_title: gettext("edit %{slug}", slug: slug))
2022-11-24 12:44:34 -05:00
|> assign(context: context)
2022-07-25 20:11:08 -04:00
end
2022-11-24 12:44:34 -05:00
defp apply_action(%{assigns: %{current_user: %{id: current_user_id}}} = socket, :new, _params) do
2022-07-25 20:11:08 -04:00
socket
2022-11-24 12:44:34 -05:00
|> assign(page_title: gettext("new context"))
2022-11-26 20:36:51 -05:00
|> assign(context: %Context{visibility: :private, user_id: current_user_id})
2022-07-25 20:11:08 -04:00
end
defp apply_action(socket, :index, _params) do
socket
2022-11-24 12:44:34 -05:00
|> assign(page_title: gettext("contexts"))
|> assign(search: nil)
|> assign(context: nil)
|> display_contexts()
end
defp apply_action(socket, :search, %{"search" => search}) do
socket
|> assign(page_title: gettext("contexts"))
|> assign(search: search)
|> assign(context: nil)
|> display_contexts()
2022-07-25 20:11:08 -04:00
end
@impl true
2022-11-24 12:44:34 -05:00
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
context = Contexts.get_context!(id, current_user)
2022-11-26 14:51:18 -05:00
{:ok, %{slug: slug}} = Contexts.delete_context(context, current_user)
2022-11-24 12:44:34 -05:00
socket =
socket
|> assign(contexts: Contexts.list_contexts(current_user))
2022-11-26 14:51:18 -05:00
|> put_flash(:info, gettext("%{slug} deleted", slug: slug))
2022-11-24 12:44:34 -05:00
{:noreply, socket}
end
def handle_event("search", %{"search" => %{"search_term" => ""}}, socket) do
2023-04-13 23:29:29 -04:00
{:noreply, socket |> push_patch(to: ~p"/contexts")}
2022-11-24 12:44:34 -05:00
end
def handle_event("search", %{"search" => %{"search_term" => search_term}}, socket) do
2023-04-13 23:29:29 -04:00
redirect_to = ~p"/contexts/#{search_term}"
2023-03-18 23:03:15 -04:00
{:noreply, socket |> push_patch(to: redirect_to)}
2022-11-24 12:44:34 -05:00
end
2022-07-25 20:11:08 -04:00
2022-11-24 12:44:34 -05:00
defp display_contexts(%{assigns: %{current_user: current_user, search: search}} = socket)
when not (current_user |> is_nil()) do
socket |> assign(contexts: Contexts.list_contexts(search, current_user))
2022-07-25 20:11:08 -04:00
end
2022-11-24 12:44:34 -05:00
defp display_contexts(%{assigns: %{search: search}} = socket) do
socket |> assign(contexts: Contexts.list_public_contexts(search))
2022-07-25 20:11:08 -04:00
end
end