From f847b44067908b080c8ff13bfa6a6f71b0407182 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Zrim=C5=A1ek?= Date: Mon, 26 Jun 2017 16:50:40 +0200 Subject: [PATCH] Error is displayed and import is prevented in case repository name column is not mapped, when importing repository items from file. [SCI-1402] --- .../repositories/import/records_importer.js | 1 + app/controllers/repositories_controller.rb | 33 ++++++++++++++----- app/views/shared/_flash_errors.html.erb | 7 ++++ config/locales/en.yml | 4 ++- 4 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 app/views/shared/_flash_errors.html.erb diff --git a/app/assets/javascripts/repositories/import/records_importer.js b/app/assets/javascripts/repositories/import/records_importer.js index d4d42a142..8ccbab01e 100644 --- a/app/assets/javascripts/repositories/import/records_importer.js +++ b/app/assets/javascripts/repositories/import/records_importer.js @@ -47,6 +47,7 @@ // Populate the errors container $('#import-errors-container').html(data.responseJSON.html); + animateSpinner(null, false); } }); } diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 80ad95773..ca41b2ed4 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -226,18 +226,33 @@ class RepositoriesController < ApplicationController end def import_records - import_records = repostiory_import_actions - status = import_records.import! respond_to do |format| format.json do - if status[:status] == :ok - flash[:success] = t('repositories.import_records.success_flash', - number_of_rows: status[:nr_of_added]) - render json: {}, status: :ok + # Check if there exist mapping for repository record (it's mandatory) + if params[:mappings].value?('-1') + import_records = repostiory_import_actions + status = import_records.import! + + if status[:status] == :ok + flash[:success] = t('repositories.import_records.success_flash', + number_of_rows: status[:nr_of_added]) + render json: {}, status: :ok + else + flash[:alert] = t('repositories.import_records.error_flash', + message: status[:errors]) + render json: {}, status: :unprocessable_entity + end else - flash[:alert] = t('repositories.import_records.error_flash', - message: status[:errors]) - render json: {}, status: :unprocessable_entity + render json: { + html: render_to_string( + partial: 'shared/flash_errors.html.erb', + locals: { error_title: t('repositories.import_records'\ + '.error_message.errors_list_title'), + error: t('repositories.import_records.error_message'\ + '.no_repository_name') } + ) + }, + status: :unprocessable_entity end end end diff --git a/app/views/shared/_flash_errors.html.erb b/app/views/shared/_flash_errors.html.erb new file mode 100644 index 000000000..cf0b290ca --- /dev/null +++ b/app/views/shared/_flash_errors.html.erb @@ -0,0 +1,7 @@ +<% if error.present? %> + +<% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 75d8b3e7c..99af038b6 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -899,6 +899,8 @@ en: no_data_to_parse: "There's nothing to be parsed." no_column_name: "Name column is required!" duplicated_values: "Two or more columns have the same mapping." + errors_list_title: "Items were not imported because one or more errors were found:" + no_repository_name: "Item name is required!" edit_record: "Edit" delete_record: "Delete" save_record: "Save" @@ -1083,7 +1085,7 @@ en: empty_file: "You've selected empty file. There's not much to import." temp_file_failure: "We couldn't create temporary file. Please contact administrator." no_file_selected: "You didn't select any file." - errors_list_title: "Samples were not imported because one or more errors was found:" + errors_list_title: "Samples were not imported because one or more errors were found:" list_row: "Row %{row}" list_error: "%{key}: %{val}" import_samples: