defmodule ArcanaWeb.CollectionsLive do @moduledoc """ LiveView for managing collections in Arcana. """ use Phoenix.LiveView import ArcanaWeb.DashboardComponents alias Arcana.Collection @impl true def mount(_params, session, socket) do repo = get_repo_from_session(session) {:ok, socket |> assign(repo: repo) |> assign(editing_collection: nil, confirm_delete_collection: nil) |> assign(stats: nil, collections: [])} end @impl true def handle_params(_params, _uri, socket) do {:noreply, load_data(socket)} end defp load_data(socket) do repo = socket.assigns.repo socket |> assign(stats: load_stats(repo)) |> assign(collections: load_collections(repo)) end @impl true def handle_event("create_collection", %{"collection" => params}, socket) do repo = socket.assigns.repo name = params["name"] || "" description = params["description"] case Collection.get_or_create(name, repo, description) do {:ok, _collection} -> {:noreply, load_data(socket)} {:error, changeset} -> {:noreply, put_flash(socket, :error, "Failed to create collection: #{inspect(changeset.errors)}")} end end def handle_event("edit_collection", %{"id" => id}, socket) do repo = socket.assigns.repo collection = repo.get(Collection, id) {:noreply, assign(socket, editing_collection: collection)} end def handle_event("cancel_edit_collection", _params, socket) do {:noreply, assign(socket, editing_collection: nil)} end def handle_event("update_collection", %{"id" => id, "collection" => params}, socket) do repo = socket.assigns.repo collection = repo.get!(Collection, id) changeset = Collection.changeset(collection, %{ name: params["name"] || collection.name, description: params["description"] }) case repo.update(changeset) do {:ok, _updated} -> {:noreply, socket |> assign(editing_collection: nil) |> load_data()} {:error, changeset} -> {:noreply, put_flash(socket, :error, "Failed to update: #{inspect(changeset.errors)}")} end end def handle_event("confirm_delete_collection", %{"id" => id}, socket) do {:noreply, assign(socket, confirm_delete_collection: id)} end def handle_event("cancel_delete_collection", _params, socket) do {:noreply, assign(socket, confirm_delete_collection: nil)} end def handle_event("delete_collection", %{"id" => id}, socket) do repo = socket.assigns.repo case repo.get(Collection, id) do nil -> {:noreply, assign(socket, confirm_delete_collection: nil)} collection -> repo.delete!(collection) {:noreply, socket |> assign(confirm_delete_collection: nil) |> load_data()} end end @impl true def render(assigns) do ~H""" <.dashboard_layout stats={@stats} current_tab={:collections}>
Organize documents into collections for scoped searches and better organization.
| Name | Description | Documents | Actions | ||||
|---|---|---|---|---|---|---|---|
| <% else %> | <%= collection.name %> |
<%= collection.description || "-" %> | <%= collection.document_count %> <%= if collection.document_count == 1, do: "document", else: "documents" %> |
<%= if @confirm_delete_collection == collection.id do %>
Delete?
<% else %>
|
<% end %>
|||