feat(import): confirm column mapping in a preview before importing members

This commit is contained in:
Moritz 2026-06-03 02:25:50 +02:00
parent a93dd9d535
commit 68a1a9530a
8 changed files with 816 additions and 38 deletions

View file

@ -99,7 +99,12 @@ defmodule MvWeb.ImportLive do
<.form_section title={gettext("Choose CSV file")}>
<Components.custom_fields_notice {assigns} />
<Components.template_links {assigns} />
<Components.import_form {assigns} />
<%= if @import_status != :preview do %>
<Components.import_form {assigns} />
<% end %>
<%= if @import_status == :preview do %>
<Components.preview {assigns} />
<% end %>
<%= if @import_status == :running or @import_status == :done or @import_status == :error do %>
<Components.import_progress {assigns} />
<% end %>
@ -133,6 +138,29 @@ defmodule MvWeb.ImportLive do
end
end
@impl true
def handle_event("confirm_import", _params, socket) do
case socket.assigns do
%{import_state: import_state} when is_map(import_state) ->
start_import(socket, import_state)
_ ->
{:noreply,
put_flash(socket, :error, gettext("No prepared import to confirm. Please upload again."))}
end
end
@impl true
def handle_event("cancel_import", _params, socket) do
socket =
socket
|> assign(:import_state, nil)
|> assign(:import_progress, nil)
|> assign(:import_status, :idle)
{:noreply, socket}
end
# Checks if all prerequisites for starting an import are met.
#
# Validates:
@ -169,10 +197,10 @@ defmodule MvWeb.ImportLive do
end
end
# Processes CSV upload and starts import process.
# Processes CSV upload and enters the mapping preview.
#
# Reads the uploaded CSV file, prepares it for import, and initiates
# the chunked processing workflow.
# Reads the uploaded CSV file and prepares it (read-only at the DB level), then
# shows the mapping preview. No member is created until the user confirms.
@spec process_csv_upload(Phoenix.LiveView.Socket.t()) ::
{:noreply, Phoenix.LiveView.Socket.t()}
defp process_csv_upload(socket) do
@ -181,7 +209,7 @@ defmodule MvWeb.ImportLive do
with {:ok, content} <- consume_and_read_csv(socket),
{:ok, import_state} <-
MemberCSV.prepare(content, max_rows: Config.csv_import_max_rows(), actor: actor) do
start_import(socket, import_state)
enter_preview(socket, import_state)
else
{:error, reason} when is_binary(reason) ->
{:noreply,
@ -193,6 +221,19 @@ defmodule MvWeb.ImportLive do
end
end
# Shows the mapping preview without starting any processing.
@spec enter_preview(Phoenix.LiveView.Socket.t(), map()) ::
{:noreply, Phoenix.LiveView.Socket.t()}
defp enter_preview(socket, import_state) do
socket =
socket
|> assign(:import_state, import_state)
|> assign(:import_progress, nil)
|> assign(:import_status, :preview)
{:noreply, socket}
end
# Starts the import process by initializing progress tracking and scheduling the first chunk.
@spec start_import(Phoenix.LiveView.Socket.t(), map()) ::
{:noreply, Phoenix.LiveView.Socket.t()}
@ -263,7 +304,9 @@ defmodule MvWeb.ImportLive do
custom_field_lookup: import_state.custom_field_lookup,
existing_error_count: length(progress.errors),
max_errors: @max_errors,
actor: actor
actor: actor,
fee_type_map: import_state.fee_type_map,
groups_found: import_state.groups_found
]
_ =