defmodule PhoenixKit.Modules.Shop.Web.ImportShow do @moduledoc """ LiveView for displaying import details. Shows: - Import metadata (filename, user, dates) - Statistics summary (imported/updated/skipped/errors) - List of imported products with links to edit - Error details (if any) """ use PhoenixKitWeb, :live_view alias PhoenixKit.Modules.Shop alias PhoenixKit.Modules.Shop.Translations alias PhoenixKit.Utils.Routes @impl true def mount(%{"uuid" => uuid}, _session, socket) do case Shop.get_import_log(uuid, preload: [:user]) do nil -> {:ok, socket |> put_flash(:error, "Import not found") |> push_navigate(to: Routes.path("/admin/shop/imports"))} import_log -> products = load_products(import_log.product_uuids || []) socket = socket |> assign(:page_title, "Import: #{import_log.filename}") |> assign(:import, import_log) |> assign(:products, products) {:ok, socket} end end @impl true def handle_params(_params, uri, socket) do {:noreply, assign(socket, :url_path, URI.parse(uri).path)} end defp load_products([]), do: [] defp load_products(product_uuids) do Shop.list_products_by_ids(product_uuids) end defp format_datetime(nil), do: "-" defp format_datetime(datetime) do Calendar.strftime(datetime, "%b %d, %Y %H:%M") end defp get_localized(nil), do: "-" defp get_localized(value) when is_binary(value), do: value defp get_localized(value) when is_map(value) do lang = Translations.default_language() Map.get(value, lang) || Map.get(value, "en") || Map.values(value) |> List.first() || "-" end defp format_price(nil), do: "-" defp format_price(%Decimal{} = price), do: Decimal.to_string(price) defp format_price(price) when is_number(price), do: to_string(price) @impl true def render(assigns) do ~H"""
<.admin_page_header back={Routes.path("/admin/shop/imports")} title="Import Details"> <:actions> "badge-warning" "processing" -> "badge-info" "completed" -> "badge-success" "failed" -> "badge-error" _ -> "badge-ghost" end ]}> {@import.status} <%!-- Metadata card --%>

<.icon name="hero-document-text" class="w-5 h-5" /> Import Information

Filename
{@import.filename}
User
{if @import.user, do: @import.user.email, else: "-"}
Started At
{format_datetime(@import.started_at)}
Completed At
{format_datetime(@import.completed_at)}
<%!-- Statistics --%>
<.icon name="hero-plus-circle" class="w-8 h-8" />
Imported
{@import.imported_count}
new products
<.icon name="hero-arrow-path" class="w-8 h-8" />
Updated
{@import.updated_count}
existing products
<.icon name="hero-minus-circle" class="w-8 h-8" />
Skipped
{@import.skipped_count}
filtered out
<.icon name="hero-exclamation-circle" class="w-8 h-8" />
Errors
{@import.error_count}
failed rows
<%!-- Products list --%> <%= if @products != [] do %>

<.icon name="hero-cube" class="w-5 h-5" /> Imported Products {length(@products)}

<%= for product <- @products do %> <% end %>
Title Slug Price
{get_localized(product.title)} {get_localized(product.slug) || "-"} {format_price(product.price)}
<.link navigate={Routes.path("/admin/shop/products/#{product.uuid}")} class="btn btn-xs btn-outline btn-info tooltip tooltip-bottom" data-tip={gettext("View")} > <.icon name="hero-eye" class="w-4 h-4 hidden sm:inline" /> {gettext("View")} <.link navigate={Routes.path("/admin/shop/products/#{product.uuid}/edit")} class="btn btn-xs btn-outline btn-info tooltip tooltip-bottom" data-tip={gettext("Edit")} > <.icon name="hero-pencil" class="w-4 h-4 hidden sm:inline" /> {gettext("Edit")}
<% else %>
<.icon name="hero-information-circle" class="w-6 h-6" /> No products tracked for this import. Product tracking was added in a later version.
<% end %> <%!-- Errors section (if any) --%> <%= if @import.error_count > 0 and @import.error_details != [] do %>

<.icon name="hero-exclamation-triangle" class="w-5 h-5" /> Errors {@import.error_count}

<%= for error <- Enum.take(@import.error_details, 50) do %> <% end %>
Handle Error Time
{error["handle"]} {error["error"]} {error["timestamp"]}
<%= if length(@import.error_details) > 50 do %>

Showing first 50 of {length(@import.error_details)} errors

<% end %>
<% end %>
""" end end