feat(import): confirm column mapping in a preview before importing members
This commit is contained in:
parent
a93dd9d535
commit
68a1a9530a
8 changed files with 816 additions and 38 deletions
|
|
@ -25,7 +25,22 @@ defmodule MvWeb.ImportLive.Components do
|
|||
<div>
|
||||
<p class="text-sm mb-2">
|
||||
{gettext(
|
||||
"Use the data field name as the CSV column header in your file. Data fields must exist in Mila before importing, because unknown data field columns will be ignored. Groups and membership fees are not supported for import."
|
||||
"Use the data field name as the CSV column header in your file. Data fields must exist in Mila before importing, because unknown data field columns will be ignored."
|
||||
)}
|
||||
</p>
|
||||
<p class="text-sm mb-2">
|
||||
{gettext(
|
||||
"Groups column (recognized headers): Groups, Gruppen, Gruppe. Comma-separated group names are supported and missing groups are created automatically."
|
||||
)}
|
||||
</p>
|
||||
<p class="text-sm mb-2">
|
||||
{gettext(
|
||||
"Fee type column (recognized headers): Fee Type, fee type, fee_type, membership_fee_type, Beitragsart. Unknown fee types fall back to the default."
|
||||
)}
|
||||
</p>
|
||||
<p class="text-sm">
|
||||
{gettext(
|
||||
"Fee status columns (Membership Fee Status, Bezahlstatus, Mitgliedsbeitragsstatus) are always ignored and cannot be imported."
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
|
|
@ -100,6 +115,194 @@ defmodule MvWeb.ImportLive.Components do
|
|||
"""
|
||||
end
|
||||
|
||||
@doc """
|
||||
Renders the mapping preview shown between upload and processing.
|
||||
|
||||
Shows the column-to-role mapping, up to 3 sample rows, and notices for
|
||||
auto-created groups, unresolved fee types, empty fee-type cells, and unknown
|
||||
columns. Nothing is written until the user confirms.
|
||||
"""
|
||||
def preview(assigns) do
|
||||
state = assigns.import_state
|
||||
column_roles = column_roles(state)
|
||||
column_samples = column_samples(state.preview_rows, length(state.headers))
|
||||
|
||||
assigns =
|
||||
assigns
|
||||
|> assign(:column_roles, column_roles)
|
||||
|> assign(:column_samples, column_samples)
|
||||
|
||||
~H"""
|
||||
<section
|
||||
class="mt-4 space-y-4"
|
||||
data-testid="import-preview"
|
||||
aria-labelledby="import-preview-heading"
|
||||
>
|
||||
<h2 id="import-preview-heading" class="text-lg font-semibold">
|
||||
{gettext("Preview import")}
|
||||
</h2>
|
||||
|
||||
<div class="overflow-x-auto">
|
||||
<table class="table table-sm w-full" data-testid="preview-mapping-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{gettext("Role")}</th>
|
||||
<th>{gettext("Column")}</th>
|
||||
<th class="text-base-content/60">{gettext("Row 1")}</th>
|
||||
<th class="text-base-content/60">{gettext("Row 2")}</th>
|
||||
<th class="text-base-content/60">{gettext("Row 3")}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<%= for {{header, role}, samples} <- Enum.zip(@column_roles, @column_samples) do %>
|
||||
<tr class={role_row_class(role)} data-testid="preview-column-row">
|
||||
<td>
|
||||
<span class={"badge badge-sm #{role_badge_class(role)}"}>
|
||||
{role_label(role)}
|
||||
</span>
|
||||
</td>
|
||||
<td class="font-medium">{header}</td>
|
||||
<%= for sample <- samples do %>
|
||||
<td class="text-base-content/70 max-w-32 truncate">{sample}</td>
|
||||
<% end %>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<%= if @import_state.groups_to_create != [] do %>
|
||||
<div class="alert alert-info" role="note" data-testid="preview-groups-notice">
|
||||
<.icon name="hero-information-circle" class="size-5" aria-hidden="true" />
|
||||
<div>
|
||||
<p class="text-sm">
|
||||
{gettext("These groups will be created automatically: %{names}",
|
||||
names: Enum.join(@import_state.groups_to_create, ", ")
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<%= if @import_state.fee_type_warnings != [] do %>
|
||||
<div class="alert alert-warning" role="alert" data-testid="preview-fee-type-warning">
|
||||
<.icon name="hero-exclamation-circle" class="size-5" aria-hidden="true" />
|
||||
<div>
|
||||
<p class="text-sm">
|
||||
{gettext("Unknown fee types (members get the default): %{names}",
|
||||
names: Enum.join(@import_state.fee_type_warnings, ", ")
|
||||
)}
|
||||
</p>
|
||||
<.link
|
||||
navigate={~p"/membership_fee_settings/new_fee_type"}
|
||||
class="link link-primary text-sm"
|
||||
>
|
||||
{gettext("Create fee type")}
|
||||
</.link>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<%= if @import_state.has_empty_fee_type_cells? do %>
|
||||
<div class="alert alert-info" role="note" data-testid="preview-fee-type-info">
|
||||
<.icon name="hero-information-circle" class="size-5" aria-hidden="true" />
|
||||
<div>
|
||||
<p class="text-sm">
|
||||
{gettext("Rows with an empty fee type will get the default fee type.")}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<%= if @import_state.warnings != [] do %>
|
||||
<div class="alert alert-warning" role="alert" data-testid="preview-unknown-warning">
|
||||
<.icon name="hero-information-circle" class="size-5" aria-hidden="true" />
|
||||
<div>
|
||||
<ul class="list-disc list-inside space-y-1 text-sm">
|
||||
<%= for warning <- @import_state.warnings do %>
|
||||
<li>{warning}</li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<.link navigate={~p"/admin/datafields"} class="link link-primary text-sm">
|
||||
{gettext("Create custom field")}
|
||||
</.link>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div class="flex gap-2">
|
||||
<.button
|
||||
type="button"
|
||||
phx-click="confirm_import"
|
||||
variant="primary"
|
||||
data-testid="confirm-import-button"
|
||||
>
|
||||
{gettext("Confirm and Import")}
|
||||
</.button>
|
||||
<.button type="button" phx-click="cancel_import" data-testid="cancel-import-button">
|
||||
{gettext("Cancel")}
|
||||
</.button>
|
||||
</div>
|
||||
</section>
|
||||
"""
|
||||
end
|
||||
|
||||
# Pairs each CSV header with its resolved role for the preview mapping table.
|
||||
defp column_roles(state) do
|
||||
member_indices = MapSet.new(Map.values(state.column_map))
|
||||
custom_indices = MapSet.new(Map.values(state.custom_field_map))
|
||||
ignored_headers = MapSet.new(state.ignored)
|
||||
|
||||
state.headers
|
||||
|> Enum.with_index()
|
||||
|> Enum.map(fn {header, index} ->
|
||||
{header, role_for(index, header, state, member_indices, custom_indices, ignored_headers)}
|
||||
end)
|
||||
end
|
||||
|
||||
defp role_for(index, header, state, member_indices, custom_indices, ignored_headers) do
|
||||
cond do
|
||||
index == state.groups_column_index -> :groups
|
||||
index == state.fee_type_column_index -> :fee_type
|
||||
MapSet.member?(ignored_headers, header) -> :ignored
|
||||
MapSet.member?(member_indices, index) -> :member_field
|
||||
MapSet.member?(custom_indices, index) -> :custom_field
|
||||
true -> :unknown
|
||||
end
|
||||
end
|
||||
|
||||
defp role_label(:member_field), do: gettext("Member field")
|
||||
defp role_label(:custom_field), do: gettext("Custom field")
|
||||
defp role_label(:groups), do: gettext("Groups")
|
||||
defp role_label(:fee_type), do: gettext("Fee type")
|
||||
defp role_label(:ignored), do: gettext("Ignored (system-computed field)")
|
||||
defp role_label(:unknown), do: gettext("Unknown (ignored)")
|
||||
|
||||
defp role_badge_class(:member_field), do: "badge-primary"
|
||||
defp role_badge_class(:custom_field), do: "badge-secondary"
|
||||
defp role_badge_class(:groups), do: "badge-success"
|
||||
defp role_badge_class(:fee_type), do: "badge-warning"
|
||||
defp role_badge_class(:ignored), do: "badge-ghost"
|
||||
defp role_badge_class(:unknown), do: "badge-error"
|
||||
|
||||
defp role_row_class(:ignored), do: "opacity-50"
|
||||
defp role_row_class(:unknown), do: "opacity-50"
|
||||
defp role_row_class(_), do: nil
|
||||
|
||||
defp column_samples([], col_count), do: List.duplicate([], col_count)
|
||||
|
||||
defp column_samples(rows, col_count) do
|
||||
Enum.map(0..(col_count - 1), fn col_idx ->
|
||||
rows
|
||||
|> Enum.map(fn row -> Enum.at(row, col_idx, "") end)
|
||||
|> pad_to(3, "")
|
||||
end)
|
||||
end
|
||||
|
||||
defp pad_to(list, target, fill) do
|
||||
list ++ List.duplicate(fill, max(0, target - length(list)))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Renders import progress text and, when done or aborted, the import results section.
|
||||
"""
|
||||
|
|
@ -246,8 +449,10 @@ defmodule MvWeb.ImportLive.Components do
|
|||
@doc """
|
||||
Returns whether the Start Import button should be disabled.
|
||||
"""
|
||||
@spec import_button_disabled?(:idle | :running | :done | :error, [map()]) :: boolean()
|
||||
@spec import_button_disabled?(:idle | :preview | :running | :done | :error, [map()]) ::
|
||||
boolean()
|
||||
def import_button_disabled?(:running, _entries), do: true
|
||||
def import_button_disabled?(:preview, _entries), do: true
|
||||
def import_button_disabled?(_status, []), do: true
|
||||
def import_button_disabled?(_status, [entry | _]) when not entry.done?, do: true
|
||||
def import_button_disabled?(_status, _entries), do: false
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue