defmodule PhoenixKitWeb.Live.Modules.Blogging.Settings do @moduledoc """ Admin configuration for site blogs. """ use PhoenixKitWeb, :live_view use Gettext, backend: PhoenixKitWeb.Gettext alias PhoenixKit.Modules.Languages alias PhoenixKit.Settings alias PhoenixKit.Utils.Routes alias PhoenixKitWeb.Live.Modules.Blogging alias PhoenixKitWeb.Live.Modules.Blogging.PubSub, as: BloggingPubSub def mount(_params, _session, socket) do blogs = Blogging.list_blogs() languages_enabled = Languages.enabled?() socket = socket |> assign(:project_title, Settings.get_setting("project_title", "PhoenixKit")) |> assign(:page_title, gettext("Manage Blogging")) |> assign( :current_path, Routes.path("/admin/settings/blogging", locale: socket.assigns.current_locale_base) ) |> assign(:module_enabled, Blogging.enabled?()) |> assign(:blogs, blogs) |> assign(:languages_enabled, languages_enabled) {:ok, socket} end def handle_params(_params, _uri, socket), do: {:noreply, socket} def handle_event("remove_blog", %{"slug" => slug}, socket) do case Blogging.trash_blog(slug) do {:ok, trashed_name} -> # Broadcast blog deleted for live dashboard updates BloggingPubSub.broadcast_blog_deleted(slug) {:noreply, socket |> assign(:blogs, Blogging.list_blogs()) |> put_flash( :info, gettext("Blog moved to trash as: %{name}", name: trashed_name) )} {:error, :not_found} -> # Blog directory doesn't exist, just remove from config case Blogging.remove_blog(slug) do {:ok, _} -> # Broadcast blog deleted for live dashboard updates BloggingPubSub.broadcast_blog_deleted(slug) {:noreply, socket |> assign(:blogs, Blogging.list_blogs()) |> put_flash(:info, gettext("Blog removed from configuration"))} {:error, _reason} -> {:noreply, put_flash(socket, :error, gettext("Failed to remove blog"))} end {:error, _reason} -> {:noreply, put_flash(socket, :error, gettext("Failed to move blog to trash"))} end end end