defmodule PhoenixKit.Modules.Shop.Web.Settings do @moduledoc """ E-Commerce module settings LiveView. Allows configuration of e-commerce settings including inventory tracking. """ use PhoenixKitWeb, :live_view alias PhoenixKit.Modules.Billing alias PhoenixKit.Modules.Shop alias PhoenixKit.Settings alias PhoenixKit.Utils.Routes # Test comment for live reload @impl true def mount(_params, _session, socket) do config = Shop.get_config() socket = socket |> assign(:page_title, "E-Commerce Settings") |> assign(:enabled, config.enabled) |> assign(:inventory_tracking, config.inventory_tracking) |> assign(:billing_enabled, billing_enabled?()) |> assign(:category_name_display, get_category_name_display()) |> assign(:category_icon_mode, get_category_icon_mode()) {:ok, socket} end defp get_category_name_display do Settings.get_setting_cached("shop_category_name_display", "truncate") end defp get_category_icon_mode do Settings.get_setting_cached("shop_category_icon_mode", "none") end @impl true def handle_event("toggle_enabled", _params, socket) do new_enabled = !socket.assigns.enabled if new_enabled and not billing_enabled?() do # Cannot enable Shop without Billing {:noreply, socket |> assign(:enabled, false) |> put_flash(:error, "Please enable Billing module first")} else result = if new_enabled do Shop.enable_system() else Shop.disable_system() end case result do :ok -> socket = socket |> assign(:enabled, new_enabled) |> put_flash( :info, if(new_enabled, do: "E-Commerce module enabled", else: "E-Commerce module disabled") ) {:noreply, socket} _ -> {:noreply, socket |> assign(:enabled, false) |> put_flash(:error, "Failed to update E-Commerce status")} end end end @impl true def handle_event("toggle_inventory_tracking", _params, socket) do new_value = !socket.assigns.inventory_tracking value_str = if(new_value, do: "true", else: "false") case Settings.update_setting("shop_inventory_tracking", value_str) do {:ok, _} -> {:noreply, socket |> assign(:inventory_tracking, new_value) |> put_flash( :info, if(new_value, do: "Inventory tracking enabled", else: "Inventory tracking disabled") )} {:error, _} -> {:noreply, put_flash(socket, :error, "Failed to update inventory setting")} end end @impl true def handle_event("update_category_display", %{"display" => display}, socket) do case Settings.update_setting("shop_category_name_display", display) do {:ok, _} -> {:noreply, socket |> assign(:category_name_display, display) |> put_flash(:info, "Category display setting updated")} {:error, _} -> {:noreply, put_flash(socket, :error, "Failed to update category display setting")} end end @impl true def handle_event("update_category_icon", %{"mode" => mode}, socket) do case Settings.update_setting("shop_category_icon_mode", mode) do {:ok, _} -> {:noreply, socket |> assign(:category_icon_mode, mode) |> put_flash(:info, "Category icon setting updated")} {:error, _} -> {:noreply, put_flash(socket, :error, "Failed to update category icon setting")} end end @impl true def render(assigns) do ~H"""
<%!-- Header --%>
<.link navigate={Routes.path("/admin/shop")} class="btn btn-outline btn-primary btn-sm shrink-0" > <.icon name="hero-arrow-left" class="w-4 h-4 mr-2" /> Back

E-Commerce Settings

Configure your e-commerce store

<%!-- Module Status Card --%>

Module Status

Enable or disable the E-Commerce module

{if @enabled, do: "Enabled", else: "Disabled"}
<%= if not @billing_enabled do %> <.link navigate={Routes.path("/admin/modules")} class="badge badge-warning badge-sm hover:badge-error" > Billing Required <% end %>
<%!-- Inventory Settings (toggle pattern) --%>

<.icon name="hero-archive-box" class="w-6 h-6" /> Inventory

<%!-- Info about Billing --%>
<.icon name="hero-information-circle" class="w-6 h-6" />

Currency & Tax Settings

Currency and tax configuration is managed in the <.link navigate={Routes.path("/admin/settings/billing")} class="link font-medium"> Billing module settings

<%!-- Product Options --%>

<.icon name="hero-tag" class="w-6 h-6" /> Product Options

<%!-- Import Configurations --%>

<.icon name="hero-funnel" class="w-6 h-6" /> Import Configurations

<%!-- Sidebar Display Settings --%>

<.icon name="hero-bars-3" class="w-6 h-6" /> Sidebar Display

<%!-- Category Name Display --%>

How category names should be displayed in the sidebar

<%!-- Category Icon Mode --%>

Show icons next to category names in sidebar

""" end defp billing_enabled? do Code.ensure_loaded?(Billing) and function_exported?(Billing, :enabled?, 0) and Billing.enabled?() rescue _ -> false end end